blob: 0b50619a66a109cdcc8b912167998020283bcbe9 [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"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010029#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010033#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace x86 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050044static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Roland Levillain7cbd27f2016-08-11 23:53:33 +010050// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
51#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Andreas Gampe85b62f22015-09-09 13:15:38 -070054class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000056 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Alexandre Rames2ed20af2015-03-06 13:55:35 +000058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010059 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000061 if (instruction_->CanThrowIntoCatchBlock()) {
62 // Live registers will be restored in the catch block if caught.
63 SaveLiveRegisters(codegen, instruction_->GetLocations());
64 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010065 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010066 instruction_,
67 instruction_->GetDexPc(),
68 this);
Roland Levillain888d0672015-11-23 18:53:50 +000069 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
Alexandre Rames8158f282015-08-07 10:26:17 +010072 bool IsFatal() const OVERRIDE { return true; }
73
Alexandre Rames9931f312015-06-19 14:47:01 +010074 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
75
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
78};
79
Andreas Gampe85b62f22015-09-09 13:15:38 -070080class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000081 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000082 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000083
Alexandre Rames2ed20af2015-03-06 13:55:35 +000084 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010085 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000086 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010087 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000088 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000089 }
90
Alexandre Rames8158f282015-08-07 10:26:17 +010091 bool IsFatal() const OVERRIDE { return true; }
92
Alexandre Rames9931f312015-06-19 14:47:01 +010093 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
94
Calin Juravled0d48522014-11-04 16:40:20 +000095 private:
Calin Juravled0d48522014-11-04 16:40:20 +000096 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
97};
98
Andreas Gampe85b62f22015-09-09 13:15:38 -070099class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000100 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000101 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
102 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000103
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000104 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000106 if (is_div_) {
107 __ negl(reg_);
108 } else {
109 __ movl(reg_, Immediate(0));
110 }
Calin Juravled0d48522014-11-04 16:40:20 +0000111 __ jmp(GetExitLabel());
112 }
113
Alexandre Rames9931f312015-06-19 14:47:01 +0100114 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
115
Calin Juravled0d48522014-11-04 16:40:20 +0000116 private:
117 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000118 bool is_div_;
119 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000120};
121
Andreas Gampe85b62f22015-09-09 13:15:38 -0700122class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100123 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000124 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100125
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000126 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100127 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100128 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000130 // We're moving two locations to locations that could overlap, so we need a parallel
131 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000132 if (instruction_->CanThrowIntoCatchBlock()) {
133 // Live registers will be restored in the catch block if caught.
134 SaveLiveRegisters(codegen, instruction_->GetLocations());
135 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400136
137 // Are we using an array length from memory?
138 HInstruction* array_length = instruction_->InputAt(1);
139 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100140 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400141 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
142 // Load the array length into our temporary.
143 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
144 Location array_loc = array_length->GetLocations()->InAt(0);
145 Address array_len(array_loc.AsRegister<Register>(), len_offset);
146 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
147 // Check for conflicts with index.
148 if (length_loc.Equals(locations->InAt(0))) {
149 // We know we aren't using parameter 2.
150 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
151 }
152 __ movl(length_loc.AsRegister<Register>(), array_len);
jessicahandojo4877b792016-09-08 19:49:13 -0700153 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100154 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700155 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400156 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000157 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100158 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000159 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100160 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400161 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100162 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
163 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100164 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
165 ? kQuickThrowStringBounds
166 : kQuickThrowArrayBounds;
167 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100168 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000169 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100170 }
171
Alexandre Rames8158f282015-08-07 10:26:17 +0100172 bool IsFatal() const OVERRIDE { return true; }
173
Alexandre Rames9931f312015-06-19 14:47:01 +0100174 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
175
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100176 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
178};
179
Andreas Gampe85b62f22015-09-09 13:15:38 -0700180class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000182 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000183 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000184
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000185 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100186 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000187 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100188 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000189 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100190 if (successor_ == nullptr) {
191 __ jmp(GetReturnLabel());
192 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100193 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100194 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000195 }
196
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100197 Label* GetReturnLabel() {
198 DCHECK(successor_ == nullptr);
199 return &return_label_;
200 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000201
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100202 HBasicBlock* GetSuccessor() const {
203 return successor_;
204 }
205
Alexandre Rames9931f312015-06-19 14:47:01 +0100206 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
207
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000208 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100209 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000210 Label return_label_;
211
212 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
213};
214
Vladimir Markoaad75c62016-10-03 08:46:48 +0000215class LoadStringSlowPathX86 : public SlowPathCode {
216 public:
217 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
218
219 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
220 LocationSummary* locations = instruction_->GetLocations();
221 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
222
223 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
224 __ Bind(GetEntryLabel());
225 SaveLiveRegisters(codegen, locations);
226
227 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000228 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
229 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000230 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
231 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
232 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
233 RestoreLiveRegisters(codegen, locations);
234
235 // Store the resolved String to the BSS entry.
236 Register method_address = locations->InAt(0).AsRegister<Register>();
237 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
238 locations->Out().AsRegister<Register>());
239 Label* fixup_label = x86_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
240 __ Bind(fixup_label);
241
242 __ jmp(GetExitLabel());
243 }
244
245 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
246
247 private:
248 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
249};
250
Andreas Gampe85b62f22015-09-09 13:15:38 -0700251class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000252 public:
253 LoadClassSlowPathX86(HLoadClass* cls,
254 HInstruction* at,
255 uint32_t dex_pc,
256 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000257 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
259 }
260
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000261 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000262 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
264 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000265 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266
267 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000268 dex::TypeIndex type_index = cls_->GetTypeIndex();
269 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100270 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
271 : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000272 instruction_,
273 dex_pc_,
274 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000275 if (do_clinit_) {
276 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
277 } else {
278 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
279 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000280
281 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000282 Location out = locations->Out();
283 if (out.IsValid()) {
284 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
285 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000286 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000287 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000288 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
289 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
290 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
291 DCHECK(out.IsValid());
292 Register method_address = locations->InAt(0).AsRegister<Register>();
293 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
294 locations->Out().AsRegister<Register>());
295 Label* fixup_label = x86_codegen->NewTypeBssEntryPatch(cls_);
296 __ Bind(fixup_label);
297 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000298 __ jmp(GetExitLabel());
299 }
300
Alexandre Rames9931f312015-06-19 14:47:01 +0100301 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
302
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000303 private:
304 // The class this slow path will load.
305 HLoadClass* const cls_;
306
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000307 // The dex PC of `at_`.
308 const uint32_t dex_pc_;
309
310 // Whether to initialize the class.
311 const bool do_clinit_;
312
313 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
314};
315
Andreas Gampe85b62f22015-09-09 13:15:38 -0700316class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000318 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000319 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000321 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 DCHECK(instruction_->IsCheckCast()
324 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000325
326 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
327 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000328
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000329 if (!is_fatal_) {
330 SaveLiveRegisters(codegen, locations);
331 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
333 // We're moving two locations to locations that could overlap, so we need a parallel
334 // move resolver.
335 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800336 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800337 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
338 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800339 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800340 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
341 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000342 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100343 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100344 instruction_,
345 instruction_->GetDexPc(),
346 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800347 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000348 } else {
349 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800350 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
351 instruction_,
352 instruction_->GetDexPc(),
353 this);
354 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000355 }
356
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000357 if (!is_fatal_) {
358 if (instruction_->IsInstanceOf()) {
359 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
360 }
361 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000362
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000363 __ jmp(GetExitLabel());
364 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000365 }
366
Alexandre Rames9931f312015-06-19 14:47:01 +0100367 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000368 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100369
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000370 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000371 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000372
373 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
374};
375
Andreas Gampe85b62f22015-09-09 13:15:38 -0700376class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700377 public:
Aart Bik42249c32016-01-07 15:33:50 -0800378 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000379 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700380
381 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100382 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700383 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100384 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000385 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386 }
387
Alexandre Rames9931f312015-06-19 14:47:01 +0100388 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
389
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700390 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700391 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
392};
393
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100394class ArraySetSlowPathX86 : public SlowPathCode {
395 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000396 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100397
398 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
399 LocationSummary* locations = instruction_->GetLocations();
400 __ Bind(GetEntryLabel());
401 SaveLiveRegisters(codegen, locations);
402
403 InvokeRuntimeCallingConvention calling_convention;
404 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
405 parallel_move.AddMove(
406 locations->InAt(0),
407 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
408 Primitive::kPrimNot,
409 nullptr);
410 parallel_move.AddMove(
411 locations->InAt(1),
412 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
413 Primitive::kPrimInt,
414 nullptr);
415 parallel_move.AddMove(
416 locations->InAt(2),
417 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
418 Primitive::kPrimNot,
419 nullptr);
420 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
421
422 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100423 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000424 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100425 RestoreLiveRegisters(codegen, locations);
426 __ jmp(GetExitLabel());
427 }
428
429 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
430
431 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100432 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
433};
434
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100435// Slow path marking an object reference `ref` during a read
436// barrier. The field `obj.field` in the object `obj` holding this
437// reference does not get updated by this slow path after marking (see
438// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
439//
440// This means that after the execution of this slow path, `ref` will
441// always be up-to-date, but `obj.field` may not; i.e., after the
442// flip, `ref` will be a to-space reference, but `obj.field` will
443// probably still be a from-space reference (unless it gets updated by
444// another thread, or if another thread installed another object
445// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000446class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
447 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100448 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
449 Location ref,
450 bool unpoison_ref_before_marking)
451 : SlowPathCode(instruction),
452 ref_(ref),
453 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000454 DCHECK(kEmitCompilerReadBarrier);
455 }
456
457 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
458
459 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
460 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100461 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000462 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100463 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000464 DCHECK(instruction_->IsInstanceFieldGet() ||
465 instruction_->IsStaticFieldGet() ||
466 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100467 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000468 instruction_->IsLoadClass() ||
469 instruction_->IsLoadString() ||
470 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100471 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100472 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
473 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000474 << "Unexpected instruction in read barrier marking slow path: "
475 << instruction_->DebugName();
476
477 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100478 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000479 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100480 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000481 }
Roland Levillain4359e612016-07-20 11:32:19 +0100482 // No need to save live registers; it's taken care of by the
483 // entrypoint. Also, there is no need to update the stack mask,
484 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000485 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100486 DCHECK_NE(ref_reg, ESP);
487 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100488 // "Compact" slow path, saving two moves.
489 //
490 // Instead of using the standard runtime calling convention (input
491 // and output in EAX):
492 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100493 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100494 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100495 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100496 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100497 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100498 // of a dedicated entrypoint:
499 //
500 // rX <- ReadBarrierMarkRegX(rX)
501 //
502 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100503 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100504 // This runtime call does not require a stack map.
505 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000506 __ jmp(GetExitLabel());
507 }
508
509 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100510 // The location (register) of the marked object reference.
511 const Location ref_;
512 // Should the reference in `ref_` be unpoisoned prior to marking it?
513 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000514
515 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
516};
517
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100518// Slow path marking an object reference `ref` during a read barrier,
519// and if needed, atomically updating the field `obj.field` in the
520// object `obj` holding this reference after marking (contrary to
521// ReadBarrierMarkSlowPathX86 above, which never tries to update
522// `obj.field`).
523//
524// This means that after the execution of this slow path, both `ref`
525// and `obj.field` will be up-to-date; i.e., after the flip, both will
526// hold the same to-space reference (unless another thread installed
527// another object reference (different from `ref`) in `obj.field`).
528class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
529 public:
530 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
531 Location ref,
532 Register obj,
533 const Address& field_addr,
534 bool unpoison_ref_before_marking,
535 Register temp)
536 : SlowPathCode(instruction),
537 ref_(ref),
538 obj_(obj),
539 field_addr_(field_addr),
540 unpoison_ref_before_marking_(unpoison_ref_before_marking),
541 temp_(temp) {
542 DCHECK(kEmitCompilerReadBarrier);
543 }
544
545 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
546
547 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
548 LocationSummary* locations = instruction_->GetLocations();
549 Register ref_reg = ref_.AsRegister<Register>();
550 DCHECK(locations->CanCall());
551 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
552 // This slow path is only used by the UnsafeCASObject intrinsic.
553 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
554 << "Unexpected instruction in read barrier marking and field updating slow path: "
555 << instruction_->DebugName();
556 DCHECK(instruction_->GetLocations()->Intrinsified());
557 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
558
559 __ Bind(GetEntryLabel());
560 if (unpoison_ref_before_marking_) {
561 // Object* ref = ref_addr->AsMirrorPtr()
562 __ MaybeUnpoisonHeapReference(ref_reg);
563 }
564
565 // Save the old (unpoisoned) reference.
566 __ movl(temp_, ref_reg);
567
568 // No need to save live registers; it's taken care of by the
569 // entrypoint. Also, there is no need to update the stack mask,
570 // as this runtime call will not trigger a garbage collection.
571 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
572 DCHECK_NE(ref_reg, ESP);
573 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
574 // "Compact" slow path, saving two moves.
575 //
576 // Instead of using the standard runtime calling convention (input
577 // and output in EAX):
578 //
579 // EAX <- ref
580 // EAX <- ReadBarrierMark(EAX)
581 // ref <- EAX
582 //
583 // we just use rX (the register containing `ref`) as input and output
584 // of a dedicated entrypoint:
585 //
586 // rX <- ReadBarrierMarkRegX(rX)
587 //
588 int32_t entry_point_offset =
589 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
590 // This runtime call does not require a stack map.
591 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
592
593 // If the new reference is different from the old reference,
594 // update the field in the holder (`*field_addr`).
595 //
596 // Note that this field could also hold a different object, if
597 // another thread had concurrently changed it. In that case, the
598 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
599 // operation below would abort the CAS, leaving the field as-is.
600 NearLabel done;
601 __ cmpl(temp_, ref_reg);
602 __ j(kEqual, &done);
603
604 // Update the the holder's field atomically. This may fail if
605 // mutator updates before us, but it's OK. This is achieved
606 // using a strong compare-and-set (CAS) operation with relaxed
607 // memory synchronization ordering, where the expected value is
608 // the old reference and the desired value is the new reference.
609 // This operation is implemented with a 32-bit LOCK CMPXLCHG
610 // instruction, which requires the expected value (the old
611 // reference) to be in EAX. Save EAX beforehand, and move the
612 // expected value (stored in `temp_`) into EAX.
613 __ pushl(EAX);
614 __ movl(EAX, temp_);
615
616 // Convenience aliases.
617 Register base = obj_;
618 Register expected = EAX;
619 Register value = ref_reg;
620
621 bool base_equals_value = (base == value);
622 if (kPoisonHeapReferences) {
623 if (base_equals_value) {
624 // If `base` and `value` are the same register location, move
625 // `value` to a temporary register. This way, poisoning
626 // `value` won't invalidate `base`.
627 value = temp_;
628 __ movl(value, base);
629 }
630
631 // Check that the register allocator did not assign the location
632 // of `expected` (EAX) to `value` nor to `base`, so that heap
633 // poisoning (when enabled) works as intended below.
634 // - If `value` were equal to `expected`, both references would
635 // be poisoned twice, meaning they would not be poisoned at
636 // all, as heap poisoning uses address negation.
637 // - If `base` were equal to `expected`, poisoning `expected`
638 // would invalidate `base`.
639 DCHECK_NE(value, expected);
640 DCHECK_NE(base, expected);
641
642 __ PoisonHeapReference(expected);
643 __ PoisonHeapReference(value);
644 }
645
646 __ LockCmpxchgl(field_addr_, value);
647
648 // If heap poisoning is enabled, we need to unpoison the values
649 // that were poisoned earlier.
650 if (kPoisonHeapReferences) {
651 if (base_equals_value) {
652 // `value` has been moved to a temporary register, no need
653 // to unpoison it.
654 } else {
655 __ UnpoisonHeapReference(value);
656 }
657 // No need to unpoison `expected` (EAX), as it is be overwritten below.
658 }
659
660 // Restore EAX.
661 __ popl(EAX);
662
663 __ Bind(&done);
664 __ jmp(GetExitLabel());
665 }
666
667 private:
668 // The location (register) of the marked object reference.
669 const Location ref_;
670 // The register containing the object holding the marked object reference field.
671 const Register obj_;
672 // The address of the marked reference field. The base of this address must be `obj_`.
673 const Address field_addr_;
674
675 // Should the reference in `ref_` be unpoisoned prior to marking it?
676 const bool unpoison_ref_before_marking_;
677
678 const Register temp_;
679
680 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
681};
682
Roland Levillain0d5a2812015-11-13 10:07:31 +0000683// Slow path generating a read barrier for a heap reference.
684class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
685 public:
686 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
687 Location out,
688 Location ref,
689 Location obj,
690 uint32_t offset,
691 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000692 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000693 out_(out),
694 ref_(ref),
695 obj_(obj),
696 offset_(offset),
697 index_(index) {
698 DCHECK(kEmitCompilerReadBarrier);
699 // If `obj` is equal to `out` or `ref`, it means the initial object
700 // has been overwritten by (or after) the heap object reference load
701 // to be instrumented, e.g.:
702 //
703 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000704 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000705 //
706 // In that case, we have lost the information about the original
707 // object, and the emitted read barrier cannot work properly.
708 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
709 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
710 }
711
712 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
713 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
714 LocationSummary* locations = instruction_->GetLocations();
715 Register reg_out = out_.AsRegister<Register>();
716 DCHECK(locations->CanCall());
717 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100718 DCHECK(instruction_->IsInstanceFieldGet() ||
719 instruction_->IsStaticFieldGet() ||
720 instruction_->IsArrayGet() ||
721 instruction_->IsInstanceOf() ||
722 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100723 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000724 << "Unexpected instruction in read barrier for heap reference slow path: "
725 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000726
727 __ Bind(GetEntryLabel());
728 SaveLiveRegisters(codegen, locations);
729
730 // We may have to change the index's value, but as `index_` is a
731 // constant member (like other "inputs" of this slow path),
732 // introduce a copy of it, `index`.
733 Location index = index_;
734 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100735 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000736 if (instruction_->IsArrayGet()) {
737 // Compute the actual memory offset and store it in `index`.
738 Register index_reg = index_.AsRegister<Register>();
739 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
740 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
741 // We are about to change the value of `index_reg` (see the
742 // calls to art::x86::X86Assembler::shll and
743 // art::x86::X86Assembler::AddImmediate below), but it has
744 // not been saved by the previous call to
745 // art::SlowPathCode::SaveLiveRegisters, as it is a
746 // callee-save register --
747 // art::SlowPathCode::SaveLiveRegisters does not consider
748 // callee-save registers, as it has been designed with the
749 // assumption that callee-save registers are supposed to be
750 // handled by the called function. So, as a callee-save
751 // register, `index_reg` _would_ eventually be saved onto
752 // the stack, but it would be too late: we would have
753 // changed its value earlier. Therefore, we manually save
754 // it here into another freely available register,
755 // `free_reg`, chosen of course among the caller-save
756 // registers (as a callee-save `free_reg` register would
757 // exhibit the same problem).
758 //
759 // Note we could have requested a temporary register from
760 // the register allocator instead; but we prefer not to, as
761 // this is a slow path, and we know we can find a
762 // caller-save register that is available.
763 Register free_reg = FindAvailableCallerSaveRegister(codegen);
764 __ movl(free_reg, index_reg);
765 index_reg = free_reg;
766 index = Location::RegisterLocation(index_reg);
767 } else {
768 // The initial register stored in `index_` has already been
769 // saved in the call to art::SlowPathCode::SaveLiveRegisters
770 // (as it is not a callee-save register), so we can freely
771 // use it.
772 }
773 // Shifting the index value contained in `index_reg` by the scale
774 // factor (2) cannot overflow in practice, as the runtime is
775 // unable to allocate object arrays with a size larger than
776 // 2^26 - 1 (that is, 2^28 - 4 bytes).
777 __ shll(index_reg, Immediate(TIMES_4));
778 static_assert(
779 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
780 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
781 __ AddImmediate(index_reg, Immediate(offset_));
782 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100783 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
784 // intrinsics, `index_` is not shifted by a scale factor of 2
785 // (as in the case of ArrayGet), as it is actually an offset
786 // to an object field within an object.
787 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000788 DCHECK(instruction_->GetLocations()->Intrinsified());
789 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
790 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
791 << instruction_->AsInvoke()->GetIntrinsic();
792 DCHECK_EQ(offset_, 0U);
793 DCHECK(index_.IsRegisterPair());
794 // UnsafeGet's offset location is a register pair, the low
795 // part contains the correct offset.
796 index = index_.ToLow();
797 }
798 }
799
800 // We're moving two or three locations to locations that could
801 // overlap, so we need a parallel move resolver.
802 InvokeRuntimeCallingConvention calling_convention;
803 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
804 parallel_move.AddMove(ref_,
805 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
806 Primitive::kPrimNot,
807 nullptr);
808 parallel_move.AddMove(obj_,
809 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
810 Primitive::kPrimNot,
811 nullptr);
812 if (index.IsValid()) {
813 parallel_move.AddMove(index,
814 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
815 Primitive::kPrimInt,
816 nullptr);
817 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
818 } else {
819 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
820 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
821 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100822 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000823 CheckEntrypointTypes<
824 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
825 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
826
827 RestoreLiveRegisters(codegen, locations);
828 __ jmp(GetExitLabel());
829 }
830
831 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
832
833 private:
834 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
835 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
836 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
837 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
838 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
839 return static_cast<Register>(i);
840 }
841 }
842 // We shall never fail to find a free caller-save register, as
843 // there are more than two core caller-save registers on x86
844 // (meaning it is possible to find one which is different from
845 // `ref` and `obj`).
846 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
847 LOG(FATAL) << "Could not find a free caller-save register";
848 UNREACHABLE();
849 }
850
Roland Levillain0d5a2812015-11-13 10:07:31 +0000851 const Location out_;
852 const Location ref_;
853 const Location obj_;
854 const uint32_t offset_;
855 // An additional location containing an index to an array.
856 // Only used for HArrayGet and the UnsafeGetObject &
857 // UnsafeGetObjectVolatile intrinsics.
858 const Location index_;
859
860 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
861};
862
863// Slow path generating a read barrier for a GC root.
864class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
865 public:
866 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000867 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000868 DCHECK(kEmitCompilerReadBarrier);
869 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000870
871 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
872 LocationSummary* locations = instruction_->GetLocations();
873 Register reg_out = out_.AsRegister<Register>();
874 DCHECK(locations->CanCall());
875 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000876 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
877 << "Unexpected instruction in read barrier for GC root slow path: "
878 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000879
880 __ Bind(GetEntryLabel());
881 SaveLiveRegisters(codegen, locations);
882
883 InvokeRuntimeCallingConvention calling_convention;
884 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
885 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100886 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000887 instruction_,
888 instruction_->GetDexPc(),
889 this);
890 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
891 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
892
893 RestoreLiveRegisters(codegen, locations);
894 __ jmp(GetExitLabel());
895 }
896
897 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
898
899 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000900 const Location out_;
901 const Location root_;
902
903 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
904};
905
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100906#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100907// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
908#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100909
Aart Bike9f37602015-10-09 11:15:55 -0700910inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700911 switch (cond) {
912 case kCondEQ: return kEqual;
913 case kCondNE: return kNotEqual;
914 case kCondLT: return kLess;
915 case kCondLE: return kLessEqual;
916 case kCondGT: return kGreater;
917 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700918 case kCondB: return kBelow;
919 case kCondBE: return kBelowEqual;
920 case kCondA: return kAbove;
921 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700922 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100923 LOG(FATAL) << "Unreachable";
924 UNREACHABLE();
925}
926
Aart Bike9f37602015-10-09 11:15:55 -0700927// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100928inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
929 switch (cond) {
930 case kCondEQ: return kEqual;
931 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700932 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100933 case kCondLT: return kBelow;
934 case kCondLE: return kBelowEqual;
935 case kCondGT: return kAbove;
936 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700937 // Unsigned remain unchanged.
938 case kCondB: return kBelow;
939 case kCondBE: return kBelowEqual;
940 case kCondA: return kAbove;
941 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100942 }
943 LOG(FATAL) << "Unreachable";
944 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700945}
946
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100947void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100948 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100949}
950
951void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100952 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100953}
954
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100955size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
956 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
957 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100958}
959
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100960size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
961 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
962 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100963}
964
Mark Mendell7c8d0092015-01-26 11:21:33 -0500965size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
966 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
967 return GetFloatingPointSpillSlotSize();
968}
969
970size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
971 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
972 return GetFloatingPointSpillSlotSize();
973}
974
Calin Juravle175dc732015-08-25 15:42:32 +0100975void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
976 HInstruction* instruction,
977 uint32_t dex_pc,
978 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100979 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100980 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
981 if (EntrypointRequiresStackMap(entrypoint)) {
982 RecordPcInfo(instruction, dex_pc, slow_path);
983 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100984}
985
Roland Levillaindec8f632016-07-22 17:10:06 +0100986void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
987 HInstruction* instruction,
988 SlowPathCode* slow_path) {
989 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100990 GenerateInvokeRuntime(entry_point_offset);
991}
992
993void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +0100994 __ fs()->call(Address::Absolute(entry_point_offset));
995}
996
Mark Mendellfb8d2792015-03-31 22:16:59 -0400997CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000998 const X86InstructionSetFeatures& isa_features,
999 const CompilerOptions& compiler_options,
1000 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001001 : CodeGenerator(graph,
1002 kNumberOfCpuRegisters,
1003 kNumberOfXmmRegisters,
1004 kNumberOfRegisterPairs,
1005 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1006 arraysize(kCoreCalleeSaves))
1007 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001008 0,
1009 compiler_options,
1010 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001011 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001012 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001013 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001014 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001015 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001016 isa_features_(isa_features),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001017 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001018 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001019 boot_image_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1020 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001021 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001022 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001023 constant_area_start_(-1),
1024 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001025 method_address_offset_(std::less<uint32_t>(),
1026 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001027 // Use a fake return address register to mimic Quick.
1028 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001029}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001030
David Brazdil58282f42016-01-14 12:45:10 +00001031void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001032 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001033 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001034}
1035
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001036InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001037 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001038 assembler_(codegen->GetAssembler()),
1039 codegen_(codegen) {}
1040
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001041static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001042 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001043}
1044
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001045void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001046 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001047 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001048 bool skip_overflow_check =
1049 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001050 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001051
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001052 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001053 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001054 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001055 }
1056
Mark Mendell5f874182015-03-04 15:42:45 -05001057 if (HasEmptyFrame()) {
1058 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001059 }
Mark Mendell5f874182015-03-04 15:42:45 -05001060
1061 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1062 Register reg = kCoreCalleeSaves[i];
1063 if (allocated_registers_.ContainsCoreRegister(reg)) {
1064 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001065 __ cfi().AdjustCFAOffset(kX86WordSize);
1066 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001067 }
1068 }
1069
Mingyao Yang063fc772016-08-02 11:02:54 -07001070 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1071 // Initialize should_deoptimize flag to 0.
1072 __ movl(Address(ESP, -kShouldDeoptimizeFlagSize), Immediate(0));
1073 }
1074
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001075 int adjust = GetFrameSize() - FrameEntrySpillSize();
1076 __ subl(ESP, Immediate(adjust));
1077 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001078 // Save the current method if we need it. Note that we do not
1079 // do this in HCurrentMethod, as the instruction might have been removed
1080 // in the SSA graph.
1081 if (RequiresCurrentMethod()) {
1082 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1083 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001084}
1085
1086void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001087 __ cfi().RememberState();
1088 if (!HasEmptyFrame()) {
1089 int adjust = GetFrameSize() - FrameEntrySpillSize();
1090 __ addl(ESP, Immediate(adjust));
1091 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001092
David Srbeckyc34dc932015-04-12 09:27:43 +01001093 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1094 Register reg = kCoreCalleeSaves[i];
1095 if (allocated_registers_.ContainsCoreRegister(reg)) {
1096 __ popl(reg);
1097 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1098 __ cfi().Restore(DWARFReg(reg));
1099 }
Mark Mendell5f874182015-03-04 15:42:45 -05001100 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001101 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001102 __ ret();
1103 __ cfi().RestoreState();
1104 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001105}
1106
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001107void CodeGeneratorX86::Bind(HBasicBlock* block) {
1108 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001109}
1110
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001111Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1112 switch (type) {
1113 case Primitive::kPrimBoolean:
1114 case Primitive::kPrimByte:
1115 case Primitive::kPrimChar:
1116 case Primitive::kPrimShort:
1117 case Primitive::kPrimInt:
1118 case Primitive::kPrimNot:
1119 return Location::RegisterLocation(EAX);
1120
1121 case Primitive::kPrimLong:
1122 return Location::RegisterPairLocation(EAX, EDX);
1123
1124 case Primitive::kPrimVoid:
1125 return Location::NoLocation();
1126
1127 case Primitive::kPrimDouble:
1128 case Primitive::kPrimFloat:
1129 return Location::FpuRegisterLocation(XMM0);
1130 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001131
1132 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001133}
1134
1135Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1136 return Location::RegisterLocation(kMethodRegisterArgument);
1137}
1138
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001139Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001140 switch (type) {
1141 case Primitive::kPrimBoolean:
1142 case Primitive::kPrimByte:
1143 case Primitive::kPrimChar:
1144 case Primitive::kPrimShort:
1145 case Primitive::kPrimInt:
1146 case Primitive::kPrimNot: {
1147 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001148 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001149 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001150 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001151 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001152 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001153 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001154 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001155
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001156 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001157 uint32_t index = gp_index_;
1158 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001159 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001160 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001161 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1162 calling_convention.GetRegisterPairAt(index));
1163 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001164 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001165 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1166 }
1167 }
1168
1169 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001170 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001171 stack_index_++;
1172 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1173 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1174 } else {
1175 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1176 }
1177 }
1178
1179 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001180 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001181 stack_index_ += 2;
1182 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1183 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1184 } else {
1185 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001186 }
1187 }
1188
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001189 case Primitive::kPrimVoid:
1190 LOG(FATAL) << "Unexpected parameter type " << type;
1191 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001192 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001193 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001194}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001195
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001196void CodeGeneratorX86::Move32(Location destination, Location source) {
1197 if (source.Equals(destination)) {
1198 return;
1199 }
1200 if (destination.IsRegister()) {
1201 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001202 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001203 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001204 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001205 } else {
1206 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001207 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001208 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001209 } else if (destination.IsFpuRegister()) {
1210 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001211 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001212 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001213 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001214 } else {
1215 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001216 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001217 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001218 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001219 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001220 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001221 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001222 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001223 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001224 } else if (source.IsConstant()) {
1225 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001226 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001227 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001228 } else {
1229 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001230 __ pushl(Address(ESP, source.GetStackIndex()));
1231 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001232 }
1233 }
1234}
1235
1236void CodeGeneratorX86::Move64(Location destination, Location source) {
1237 if (source.Equals(destination)) {
1238 return;
1239 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001240 if (destination.IsRegisterPair()) {
1241 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001242 EmitParallelMoves(
1243 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1244 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001245 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001246 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001247 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1248 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001249 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001250 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1251 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1252 __ psrlq(src_reg, Immediate(32));
1253 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001254 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001255 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001256 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001257 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1258 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1260 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001261 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001262 if (source.IsFpuRegister()) {
1263 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1264 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001265 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001266 } else if (source.IsRegisterPair()) {
1267 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1268 // Create stack space for 2 elements.
1269 __ subl(ESP, Immediate(2 * elem_size));
1270 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1271 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1272 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1273 // And remove the temporary stack space we allocated.
1274 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001275 } else {
1276 LOG(FATAL) << "Unimplemented";
1277 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001278 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001279 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001280 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001281 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001282 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001283 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001284 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001285 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001286 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001287 } else if (source.IsConstant()) {
1288 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001289 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1290 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001291 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001292 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1293 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001294 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001295 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001296 EmitParallelMoves(
1297 Location::StackSlot(source.GetStackIndex()),
1298 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001299 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001300 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001301 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1302 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001303 }
1304 }
1305}
1306
Calin Juravle175dc732015-08-25 15:42:32 +01001307void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1308 DCHECK(location.IsRegister());
1309 __ movl(location.AsRegister<Register>(), Immediate(value));
1310}
1311
Calin Juravlee460d1d2015-09-29 04:52:17 +01001312void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001313 HParallelMove move(GetGraph()->GetArena());
1314 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1315 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1316 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001317 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001318 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001319 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001320 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001321}
1322
1323void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1324 if (location.IsRegister()) {
1325 locations->AddTemp(location);
1326 } else if (location.IsRegisterPair()) {
1327 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1328 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1329 } else {
1330 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1331 }
1332}
1333
David Brazdilfc6a86a2015-06-26 10:33:45 +00001334void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001335 DCHECK(!successor->IsExitBlock());
1336
1337 HBasicBlock* block = got->GetBlock();
1338 HInstruction* previous = got->GetPrevious();
1339
1340 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001341 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001342 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1343 return;
1344 }
1345
1346 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1347 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1348 }
1349 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001350 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001351 }
1352}
1353
David Brazdilfc6a86a2015-06-26 10:33:45 +00001354void LocationsBuilderX86::VisitGoto(HGoto* got) {
1355 got->SetLocations(nullptr);
1356}
1357
1358void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1359 HandleGoto(got, got->GetSuccessor());
1360}
1361
1362void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1363 try_boundary->SetLocations(nullptr);
1364}
1365
1366void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1367 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1368 if (!successor->IsExitBlock()) {
1369 HandleGoto(try_boundary, successor);
1370 }
1371}
1372
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001373void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001374 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001375}
1376
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001377void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001378}
1379
Mark Mendell152408f2015-12-31 12:28:50 -05001380template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001381void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001382 LabelType* true_label,
1383 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001384 if (cond->IsFPConditionTrueIfNaN()) {
1385 __ j(kUnordered, true_label);
1386 } else if (cond->IsFPConditionFalseIfNaN()) {
1387 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001388 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001389 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001390}
1391
Mark Mendell152408f2015-12-31 12:28:50 -05001392template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001393void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001394 LabelType* true_label,
1395 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001396 LocationSummary* locations = cond->GetLocations();
1397 Location left = locations->InAt(0);
1398 Location right = locations->InAt(1);
1399 IfCondition if_cond = cond->GetCondition();
1400
Mark Mendellc4701932015-04-10 13:18:51 -04001401 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001402 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001403 IfCondition true_high_cond = if_cond;
1404 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001405 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001406
1407 // Set the conditions for the test, remembering that == needs to be
1408 // decided using the low words.
1409 switch (if_cond) {
1410 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001411 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001412 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001413 break;
1414 case kCondLT:
1415 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001416 break;
1417 case kCondLE:
1418 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001419 break;
1420 case kCondGT:
1421 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001422 break;
1423 case kCondGE:
1424 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001425 break;
Aart Bike9f37602015-10-09 11:15:55 -07001426 case kCondB:
1427 false_high_cond = kCondA;
1428 break;
1429 case kCondBE:
1430 true_high_cond = kCondB;
1431 break;
1432 case kCondA:
1433 false_high_cond = kCondB;
1434 break;
1435 case kCondAE:
1436 true_high_cond = kCondA;
1437 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001438 }
1439
1440 if (right.IsConstant()) {
1441 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001442 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001443 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001444
Aart Bika19616e2016-02-01 18:57:58 -08001445 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001446 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001447 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001448 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001449 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001450 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001451 __ j(X86Condition(true_high_cond), true_label);
1452 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001453 }
1454 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001455 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001456 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001457 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001458 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001459
1460 __ cmpl(left_high, right_high);
1461 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001462 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001463 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001464 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001465 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001466 __ j(X86Condition(true_high_cond), true_label);
1467 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001468 }
1469 // Must be equal high, so compare the lows.
1470 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001471 } else {
1472 DCHECK(right.IsDoubleStackSlot());
1473 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1474 if (if_cond == kCondNE) {
1475 __ j(X86Condition(true_high_cond), true_label);
1476 } else if (if_cond == kCondEQ) {
1477 __ j(X86Condition(false_high_cond), false_label);
1478 } else {
1479 __ j(X86Condition(true_high_cond), true_label);
1480 __ j(X86Condition(false_high_cond), false_label);
1481 }
1482 // Must be equal high, so compare the lows.
1483 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001484 }
1485 // The last comparison might be unsigned.
1486 __ j(final_condition, true_label);
1487}
1488
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001489void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1490 Location rhs,
1491 HInstruction* insn,
1492 bool is_double) {
1493 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1494 if (is_double) {
1495 if (rhs.IsFpuRegister()) {
1496 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1497 } else if (const_area != nullptr) {
1498 DCHECK(const_area->IsEmittedAtUseSite());
1499 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1500 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001501 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1502 const_area->GetBaseMethodAddress(),
1503 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001504 } else {
1505 DCHECK(rhs.IsDoubleStackSlot());
1506 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1507 }
1508 } else {
1509 if (rhs.IsFpuRegister()) {
1510 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1511 } else if (const_area != nullptr) {
1512 DCHECK(const_area->IsEmittedAtUseSite());
1513 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1514 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001515 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1516 const_area->GetBaseMethodAddress(),
1517 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001518 } else {
1519 DCHECK(rhs.IsStackSlot());
1520 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1521 }
1522 }
1523}
1524
Mark Mendell152408f2015-12-31 12:28:50 -05001525template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001526void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001527 LabelType* true_target_in,
1528 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001529 // Generated branching requires both targets to be explicit. If either of the
1530 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001531 LabelType fallthrough_target;
1532 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1533 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001534
Mark Mendellc4701932015-04-10 13:18:51 -04001535 LocationSummary* locations = condition->GetLocations();
1536 Location left = locations->InAt(0);
1537 Location right = locations->InAt(1);
1538
Mark Mendellc4701932015-04-10 13:18:51 -04001539 Primitive::Type type = condition->InputAt(0)->GetType();
1540 switch (type) {
1541 case Primitive::kPrimLong:
1542 GenerateLongComparesAndJumps(condition, true_target, false_target);
1543 break;
1544 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001545 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001546 GenerateFPJumps(condition, true_target, false_target);
1547 break;
1548 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001549 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001550 GenerateFPJumps(condition, true_target, false_target);
1551 break;
1552 default:
1553 LOG(FATAL) << "Unexpected compare type " << type;
1554 }
1555
David Brazdil0debae72015-11-12 18:37:00 +00001556 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001557 __ jmp(false_target);
1558 }
David Brazdil0debae72015-11-12 18:37:00 +00001559
1560 if (fallthrough_target.IsLinked()) {
1561 __ Bind(&fallthrough_target);
1562 }
Mark Mendellc4701932015-04-10 13:18:51 -04001563}
1564
David Brazdil0debae72015-11-12 18:37:00 +00001565static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1566 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1567 // are set only strictly before `branch`. We can't use the eflags on long/FP
1568 // conditions if they are materialized due to the complex branching.
1569 return cond->IsCondition() &&
1570 cond->GetNext() == branch &&
1571 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1572 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1573}
1574
Mark Mendell152408f2015-12-31 12:28:50 -05001575template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001576void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001577 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001578 LabelType* true_target,
1579 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001580 HInstruction* cond = instruction->InputAt(condition_input_index);
1581
1582 if (true_target == nullptr && false_target == nullptr) {
1583 // Nothing to do. The code always falls through.
1584 return;
1585 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001586 // Constant condition, statically compared against "true" (integer value 1).
1587 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001588 if (true_target != nullptr) {
1589 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001590 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001591 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001592 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001593 if (false_target != nullptr) {
1594 __ jmp(false_target);
1595 }
1596 }
1597 return;
1598 }
1599
1600 // The following code generates these patterns:
1601 // (1) true_target == nullptr && false_target != nullptr
1602 // - opposite condition true => branch to false_target
1603 // (2) true_target != nullptr && false_target == nullptr
1604 // - condition true => branch to true_target
1605 // (3) true_target != nullptr && false_target != nullptr
1606 // - condition true => branch to true_target
1607 // - branch to false_target
1608 if (IsBooleanValueOrMaterializedCondition(cond)) {
1609 if (AreEflagsSetFrom(cond, instruction)) {
1610 if (true_target == nullptr) {
1611 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1612 } else {
1613 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1614 }
1615 } else {
1616 // Materialized condition, compare against 0.
1617 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1618 if (lhs.IsRegister()) {
1619 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1620 } else {
1621 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1622 }
1623 if (true_target == nullptr) {
1624 __ j(kEqual, false_target);
1625 } else {
1626 __ j(kNotEqual, true_target);
1627 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001628 }
1629 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001630 // Condition has not been materialized, use its inputs as the comparison and
1631 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001632 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001633
1634 // If this is a long or FP comparison that has been folded into
1635 // the HCondition, generate the comparison directly.
1636 Primitive::Type type = condition->InputAt(0)->GetType();
1637 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1638 GenerateCompareTestAndBranch(condition, true_target, false_target);
1639 return;
1640 }
1641
1642 Location lhs = condition->GetLocations()->InAt(0);
1643 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001644 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001645 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001646 if (true_target == nullptr) {
1647 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1648 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001649 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001650 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001651 }
David Brazdil0debae72015-11-12 18:37:00 +00001652
1653 // If neither branch falls through (case 3), the conditional branch to `true_target`
1654 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1655 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001656 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001657 }
1658}
1659
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001660void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001661 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1662 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001663 locations->SetInAt(0, Location::Any());
1664 }
1665}
1666
1667void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001668 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1669 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1670 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1671 nullptr : codegen_->GetLabelOf(true_successor);
1672 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1673 nullptr : codegen_->GetLabelOf(false_successor);
1674 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001675}
1676
1677void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1678 LocationSummary* locations = new (GetGraph()->GetArena())
1679 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001680 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001681 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001682 locations->SetInAt(0, Location::Any());
1683 }
1684}
1685
1686void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001687 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001688 GenerateTestAndBranch<Label>(deoptimize,
1689 /* condition_input_index */ 0,
1690 slow_path->GetEntryLabel(),
1691 /* false_target */ nullptr);
1692}
1693
Mingyao Yang063fc772016-08-02 11:02:54 -07001694void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1695 LocationSummary* locations = new (GetGraph()->GetArena())
1696 LocationSummary(flag, LocationSummary::kNoCall);
1697 locations->SetOut(Location::RequiresRegister());
1698}
1699
1700void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1701 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1702 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1703}
1704
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001705static bool SelectCanUseCMOV(HSelect* select) {
1706 // There are no conditional move instructions for XMMs.
1707 if (Primitive::IsFloatingPointType(select->GetType())) {
1708 return false;
1709 }
1710
1711 // A FP condition doesn't generate the single CC that we need.
1712 // In 32 bit mode, a long condition doesn't generate a single CC either.
1713 HInstruction* condition = select->GetCondition();
1714 if (condition->IsCondition()) {
1715 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1716 if (compare_type == Primitive::kPrimLong ||
1717 Primitive::IsFloatingPointType(compare_type)) {
1718 return false;
1719 }
1720 }
1721
1722 // We can generate a CMOV for this Select.
1723 return true;
1724}
1725
David Brazdil74eb1b22015-12-14 11:44:01 +00001726void LocationsBuilderX86::VisitSelect(HSelect* select) {
1727 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001728 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001729 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001730 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001731 } else {
1732 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001733 if (SelectCanUseCMOV(select)) {
1734 if (select->InputAt(1)->IsConstant()) {
1735 // Cmov can't handle a constant value.
1736 locations->SetInAt(1, Location::RequiresRegister());
1737 } else {
1738 locations->SetInAt(1, Location::Any());
1739 }
1740 } else {
1741 locations->SetInAt(1, Location::Any());
1742 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001743 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001744 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1745 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001746 }
1747 locations->SetOut(Location::SameAsFirstInput());
1748}
1749
1750void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1751 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001752 DCHECK(locations->InAt(0).Equals(locations->Out()));
1753 if (SelectCanUseCMOV(select)) {
1754 // If both the condition and the source types are integer, we can generate
1755 // a CMOV to implement Select.
1756
1757 HInstruction* select_condition = select->GetCondition();
1758 Condition cond = kNotEqual;
1759
1760 // Figure out how to test the 'condition'.
1761 if (select_condition->IsCondition()) {
1762 HCondition* condition = select_condition->AsCondition();
1763 if (!condition->IsEmittedAtUseSite()) {
1764 // This was a previously materialized condition.
1765 // Can we use the existing condition code?
1766 if (AreEflagsSetFrom(condition, select)) {
1767 // Materialization was the previous instruction. Condition codes are right.
1768 cond = X86Condition(condition->GetCondition());
1769 } else {
1770 // No, we have to recreate the condition code.
1771 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1772 __ testl(cond_reg, cond_reg);
1773 }
1774 } else {
1775 // We can't handle FP or long here.
1776 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1777 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1778 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001779 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001780 cond = X86Condition(condition->GetCondition());
1781 }
1782 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001783 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001784 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1785 __ testl(cond_reg, cond_reg);
1786 }
1787
1788 // If the condition is true, overwrite the output, which already contains false.
1789 Location false_loc = locations->InAt(0);
1790 Location true_loc = locations->InAt(1);
1791 if (select->GetType() == Primitive::kPrimLong) {
1792 // 64 bit conditional move.
1793 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1794 Register false_low = false_loc.AsRegisterPairLow<Register>();
1795 if (true_loc.IsRegisterPair()) {
1796 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1797 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1798 } else {
1799 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1800 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1801 }
1802 } else {
1803 // 32 bit conditional move.
1804 Register false_reg = false_loc.AsRegister<Register>();
1805 if (true_loc.IsRegister()) {
1806 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1807 } else {
1808 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1809 }
1810 }
1811 } else {
1812 NearLabel false_target;
1813 GenerateTestAndBranch<NearLabel>(
1814 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1815 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1816 __ Bind(&false_target);
1817 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001818}
1819
David Srbecky0cf44932015-12-09 14:09:59 +00001820void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1821 new (GetGraph()->GetArena()) LocationSummary(info);
1822}
1823
David Srbeckyd28f4a02016-03-14 17:14:24 +00001824void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1825 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001826}
1827
1828void CodeGeneratorX86::GenerateNop() {
1829 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001830}
1831
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001833 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001834 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001835 // Handle the long/FP comparisons made in instruction simplification.
1836 switch (cond->InputAt(0)->GetType()) {
1837 case Primitive::kPrimLong: {
1838 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001839 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001840 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001841 locations->SetOut(Location::RequiresRegister());
1842 }
1843 break;
1844 }
1845 case Primitive::kPrimFloat:
1846 case Primitive::kPrimDouble: {
1847 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001848 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1849 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1850 } else if (cond->InputAt(1)->IsConstant()) {
1851 locations->SetInAt(1, Location::RequiresFpuRegister());
1852 } else {
1853 locations->SetInAt(1, Location::Any());
1854 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001855 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001856 locations->SetOut(Location::RequiresRegister());
1857 }
1858 break;
1859 }
1860 default:
1861 locations->SetInAt(0, Location::RequiresRegister());
1862 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001863 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001864 // We need a byte register.
1865 locations->SetOut(Location::RegisterLocation(ECX));
1866 }
1867 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001868 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001869}
1870
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001871void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001872 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001873 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001874 }
Mark Mendellc4701932015-04-10 13:18:51 -04001875
1876 LocationSummary* locations = cond->GetLocations();
1877 Location lhs = locations->InAt(0);
1878 Location rhs = locations->InAt(1);
1879 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001880 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001881
1882 switch (cond->InputAt(0)->GetType()) {
1883 default: {
1884 // Integer case.
1885
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001886 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001887 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001888 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001889 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001890 return;
1891 }
1892 case Primitive::kPrimLong:
1893 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1894 break;
1895 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001896 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001897 GenerateFPJumps(cond, &true_label, &false_label);
1898 break;
1899 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001900 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001901 GenerateFPJumps(cond, &true_label, &false_label);
1902 break;
1903 }
1904
1905 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001906 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001907
Roland Levillain4fa13f62015-07-06 18:11:54 +01001908 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001909 __ Bind(&false_label);
1910 __ xorl(reg, reg);
1911 __ jmp(&done_label);
1912
Roland Levillain4fa13f62015-07-06 18:11:54 +01001913 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001914 __ Bind(&true_label);
1915 __ movl(reg, Immediate(1));
1916 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001917}
1918
1919void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001920 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001921}
1922
1923void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001924 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001925}
1926
1927void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001928 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001929}
1930
1931void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001932 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001933}
1934
1935void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001936 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001937}
1938
1939void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001940 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001941}
1942
1943void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001944 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001945}
1946
1947void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001948 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001949}
1950
1951void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001952 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001953}
1954
1955void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001956 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001957}
1958
1959void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001960 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001961}
1962
1963void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001964 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001965}
1966
Aart Bike9f37602015-10-09 11:15:55 -07001967void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001968 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001969}
1970
1971void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001972 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001973}
1974
1975void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001976 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001977}
1978
1979void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001980 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001981}
1982
1983void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001985}
1986
1987void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001988 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001989}
1990
1991void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001992 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001993}
1994
1995void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001997}
1998
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001999void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002000 LocationSummary* locations =
2001 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002002 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002003}
2004
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002005void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002006 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002007}
2008
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002009void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2010 LocationSummary* locations =
2011 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2012 locations->SetOut(Location::ConstantLocation(constant));
2013}
2014
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002015void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002016 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002017}
2018
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002019void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002020 LocationSummary* locations =
2021 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002022 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002023}
2024
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002025void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002026 // Will be generated at use site.
2027}
2028
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002029void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2030 LocationSummary* locations =
2031 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2032 locations->SetOut(Location::ConstantLocation(constant));
2033}
2034
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002035void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002036 // Will be generated at use site.
2037}
2038
2039void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2040 LocationSummary* locations =
2041 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2042 locations->SetOut(Location::ConstantLocation(constant));
2043}
2044
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002045void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002046 // Will be generated at use site.
2047}
2048
Calin Juravle27df7582015-04-17 19:12:31 +01002049void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2050 memory_barrier->SetLocations(nullptr);
2051}
2052
2053void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002054 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002055}
2056
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002057void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002058 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002059}
2060
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002061void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002062 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002063}
2064
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002065void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002066 LocationSummary* locations =
2067 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002068 switch (ret->InputAt(0)->GetType()) {
2069 case Primitive::kPrimBoolean:
2070 case Primitive::kPrimByte:
2071 case Primitive::kPrimChar:
2072 case Primitive::kPrimShort:
2073 case Primitive::kPrimInt:
2074 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002075 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002076 break;
2077
2078 case Primitive::kPrimLong:
2079 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002080 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002081 break;
2082
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002083 case Primitive::kPrimFloat:
2084 case Primitive::kPrimDouble:
2085 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002086 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002087 break;
2088
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002089 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002090 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002091 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002092}
2093
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002094void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002095 if (kIsDebugBuild) {
2096 switch (ret->InputAt(0)->GetType()) {
2097 case Primitive::kPrimBoolean:
2098 case Primitive::kPrimByte:
2099 case Primitive::kPrimChar:
2100 case Primitive::kPrimShort:
2101 case Primitive::kPrimInt:
2102 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002103 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002104 break;
2105
2106 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002107 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2108 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002109 break;
2110
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002111 case Primitive::kPrimFloat:
2112 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002113 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002114 break;
2115
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002116 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002117 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002118 }
2119 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002120 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002121}
2122
Calin Juravle175dc732015-08-25 15:42:32 +01002123void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2124 // The trampoline uses the same calling convention as dex calling conventions,
2125 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2126 // the method_idx.
2127 HandleInvoke(invoke);
2128}
2129
2130void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2131 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2132}
2133
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002134void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002135 // Explicit clinit checks triggered by static invokes must have been pruned by
2136 // art::PrepareForRegisterAllocation.
2137 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002138
Mark Mendellfb8d2792015-03-31 22:16:59 -04002139 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002140 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002141 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002142 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002143 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002144 return;
2145 }
2146
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002147 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002148
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002149 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2150 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002151 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002152 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002153}
2154
Mark Mendell09ed1a32015-03-25 08:30:06 -04002155static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2156 if (invoke->GetLocations()->Intrinsified()) {
2157 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2158 intrinsic.Dispatch(invoke);
2159 return true;
2160 }
2161 return false;
2162}
2163
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002164void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002165 // Explicit clinit checks triggered by static invokes must have been pruned by
2166 // art::PrepareForRegisterAllocation.
2167 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002168
Mark Mendell09ed1a32015-03-25 08:30:06 -04002169 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2170 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002171 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002172
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002173 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002174 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002175 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002176 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002177}
2178
2179void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002180 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2181 if (intrinsic.TryDispatch(invoke)) {
2182 return;
2183 }
2184
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002185 HandleInvoke(invoke);
2186}
2187
2188void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002189 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002190 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002191}
2192
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002193void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002194 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2195 return;
2196 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002197
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002198 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002199 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002200 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002201}
2202
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002203void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002204 // This call to HandleInvoke allocates a temporary (core) register
2205 // which is also used to transfer the hidden argument from FP to
2206 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002207 HandleInvoke(invoke);
2208 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002209 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002210}
2211
2212void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2213 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002214 LocationSummary* locations = invoke->GetLocations();
2215 Register temp = locations->GetTemp(0).AsRegister<Register>();
2216 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002217 Location receiver = locations->InAt(0);
2218 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2219
Roland Levillain0d5a2812015-11-13 10:07:31 +00002220 // Set the hidden argument. This is safe to do this here, as XMM7
2221 // won't be modified thereafter, before the `call` instruction.
2222 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002223 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002224 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002225
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002226 if (receiver.IsStackSlot()) {
2227 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002228 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002229 __ movl(temp, Address(temp, class_offset));
2230 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002231 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002232 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002233 }
Roland Levillain4d027112015-07-01 15:41:14 +01002234 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002235 // Instead of simply (possibly) unpoisoning `temp` here, we should
2236 // emit a read barrier for the previous class reference load.
2237 // However this is not required in practice, as this is an
2238 // intermediate/temporary reference and because the current
2239 // concurrent copying collector keeps the from-space memory
2240 // intact/accessible until the end of the marking phase (the
2241 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002242 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002243 // temp = temp->GetAddressOfIMT()
2244 __ movl(temp,
2245 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002246 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002247 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002248 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002249 __ movl(temp, Address(temp, method_offset));
2250 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002251 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002252 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002253
2254 DCHECK(!codegen_->IsLeafMethod());
2255 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2256}
2257
Orion Hodsonac141392017-01-13 11:53:47 +00002258void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2259 HandleInvoke(invoke);
2260}
2261
2262void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2263 codegen_->GenerateInvokePolymorphicCall(invoke);
2264}
2265
Roland Levillain88cb1752014-10-20 16:36:47 +01002266void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2267 LocationSummary* locations =
2268 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2269 switch (neg->GetResultType()) {
2270 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002271 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002272 locations->SetInAt(0, Location::RequiresRegister());
2273 locations->SetOut(Location::SameAsFirstInput());
2274 break;
2275
Roland Levillain88cb1752014-10-20 16:36:47 +01002276 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002277 locations->SetInAt(0, Location::RequiresFpuRegister());
2278 locations->SetOut(Location::SameAsFirstInput());
2279 locations->AddTemp(Location::RequiresRegister());
2280 locations->AddTemp(Location::RequiresFpuRegister());
2281 break;
2282
Roland Levillain88cb1752014-10-20 16:36:47 +01002283 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002284 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002285 locations->SetOut(Location::SameAsFirstInput());
2286 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002287 break;
2288
2289 default:
2290 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2291 }
2292}
2293
2294void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2295 LocationSummary* locations = neg->GetLocations();
2296 Location out = locations->Out();
2297 Location in = locations->InAt(0);
2298 switch (neg->GetResultType()) {
2299 case Primitive::kPrimInt:
2300 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002301 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002302 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002303 break;
2304
2305 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002306 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002307 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002308 __ negl(out.AsRegisterPairLow<Register>());
2309 // Negation is similar to subtraction from zero. The least
2310 // significant byte triggers a borrow when it is different from
2311 // zero; to take it into account, add 1 to the most significant
2312 // byte if the carry flag (CF) is set to 1 after the first NEGL
2313 // operation.
2314 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2315 __ negl(out.AsRegisterPairHigh<Register>());
2316 break;
2317
Roland Levillain5368c212014-11-27 15:03:41 +00002318 case Primitive::kPrimFloat: {
2319 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002320 Register constant = locations->GetTemp(0).AsRegister<Register>();
2321 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002322 // Implement float negation with an exclusive or with value
2323 // 0x80000000 (mask for bit 31, representing the sign of a
2324 // single-precision floating-point number).
2325 __ movl(constant, Immediate(INT32_C(0x80000000)));
2326 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002327 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002328 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002329 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002330
Roland Levillain5368c212014-11-27 15:03:41 +00002331 case Primitive::kPrimDouble: {
2332 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002333 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002334 // Implement double negation with an exclusive or with value
2335 // 0x8000000000000000 (mask for bit 63, representing the sign of
2336 // a double-precision floating-point number).
2337 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002338 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002339 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002340 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002341
2342 default:
2343 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2344 }
2345}
2346
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002347void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2348 LocationSummary* locations =
2349 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2350 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2351 locations->SetInAt(0, Location::RequiresFpuRegister());
2352 locations->SetInAt(1, Location::RequiresRegister());
2353 locations->SetOut(Location::SameAsFirstInput());
2354 locations->AddTemp(Location::RequiresFpuRegister());
2355}
2356
2357void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2358 LocationSummary* locations = neg->GetLocations();
2359 Location out = locations->Out();
2360 DCHECK(locations->InAt(0).Equals(out));
2361
2362 Register constant_area = locations->InAt(1).AsRegister<Register>();
2363 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2364 if (neg->GetType() == Primitive::kPrimFloat) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002365 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2366 neg->GetBaseMethodAddress(),
2367 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002368 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2369 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002370 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2371 neg->GetBaseMethodAddress(),
2372 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002373 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2374 }
2375}
2376
Roland Levillaindff1f282014-11-05 14:15:05 +00002377void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002378 Primitive::Type result_type = conversion->GetResultType();
2379 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002380 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002381
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002382 // The float-to-long and double-to-long type conversions rely on a
2383 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002384 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002385 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2386 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002387 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002388 : LocationSummary::kNoCall;
2389 LocationSummary* locations =
2390 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2391
David Brazdilb2bd1c52015-03-25 11:17:37 +00002392 // The Java language does not allow treating boolean as an integral type but
2393 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002394
Roland Levillaindff1f282014-11-05 14:15:05 +00002395 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002396 case Primitive::kPrimByte:
2397 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002398 case Primitive::kPrimLong: {
2399 // Type conversion from long to byte is a result of code transformations.
2400 HInstruction* input = conversion->InputAt(0);
2401 Location input_location = input->IsConstant()
2402 ? Location::ConstantLocation(input->AsConstant())
2403 : Location::RegisterPairLocation(EAX, EDX);
2404 locations->SetInAt(0, input_location);
2405 // Make the output overlap to please the register allocator. This greatly simplifies
2406 // the validation of the linear scan implementation
2407 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2408 break;
2409 }
David Brazdil46e2a392015-03-16 17:31:52 +00002410 case Primitive::kPrimBoolean:
2411 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002412 case Primitive::kPrimShort:
2413 case Primitive::kPrimInt:
2414 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002415 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002416 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2417 // Make the output overlap to please the register allocator. This greatly simplifies
2418 // the validation of the linear scan implementation
2419 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002420 break;
2421
2422 default:
2423 LOG(FATAL) << "Unexpected type conversion from " << input_type
2424 << " to " << result_type;
2425 }
2426 break;
2427
Roland Levillain01a8d712014-11-14 16:27:39 +00002428 case Primitive::kPrimShort:
2429 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002430 case Primitive::kPrimLong:
2431 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002432 case Primitive::kPrimBoolean:
2433 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002434 case Primitive::kPrimByte:
2435 case Primitive::kPrimInt:
2436 case Primitive::kPrimChar:
2437 // Processing a Dex `int-to-short' instruction.
2438 locations->SetInAt(0, Location::Any());
2439 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2440 break;
2441
2442 default:
2443 LOG(FATAL) << "Unexpected type conversion from " << input_type
2444 << " to " << result_type;
2445 }
2446 break;
2447
Roland Levillain946e1432014-11-11 17:35:19 +00002448 case Primitive::kPrimInt:
2449 switch (input_type) {
2450 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002451 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002452 locations->SetInAt(0, Location::Any());
2453 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2454 break;
2455
2456 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002457 // Processing a Dex `float-to-int' instruction.
2458 locations->SetInAt(0, Location::RequiresFpuRegister());
2459 locations->SetOut(Location::RequiresRegister());
2460 locations->AddTemp(Location::RequiresFpuRegister());
2461 break;
2462
Roland Levillain946e1432014-11-11 17:35:19 +00002463 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002464 // Processing a Dex `double-to-int' instruction.
2465 locations->SetInAt(0, Location::RequiresFpuRegister());
2466 locations->SetOut(Location::RequiresRegister());
2467 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002468 break;
2469
2470 default:
2471 LOG(FATAL) << "Unexpected type conversion from " << input_type
2472 << " to " << result_type;
2473 }
2474 break;
2475
Roland Levillaindff1f282014-11-05 14:15:05 +00002476 case Primitive::kPrimLong:
2477 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002478 case Primitive::kPrimBoolean:
2479 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002480 case Primitive::kPrimByte:
2481 case Primitive::kPrimShort:
2482 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002483 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002484 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002485 locations->SetInAt(0, Location::RegisterLocation(EAX));
2486 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2487 break;
2488
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002489 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002490 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002491 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002492 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002493 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2494 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2495
Vladimir Marko949c91f2015-01-27 10:48:44 +00002496 // The runtime helper puts the result in EAX, EDX.
2497 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002498 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002499 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002500
2501 default:
2502 LOG(FATAL) << "Unexpected type conversion from " << input_type
2503 << " to " << result_type;
2504 }
2505 break;
2506
Roland Levillain981e4542014-11-14 11:47:14 +00002507 case Primitive::kPrimChar:
2508 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002509 case Primitive::kPrimLong:
2510 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002511 case Primitive::kPrimBoolean:
2512 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002513 case Primitive::kPrimByte:
2514 case Primitive::kPrimShort:
2515 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002516 // Processing a Dex `int-to-char' instruction.
2517 locations->SetInAt(0, Location::Any());
2518 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2519 break;
2520
2521 default:
2522 LOG(FATAL) << "Unexpected type conversion from " << input_type
2523 << " to " << result_type;
2524 }
2525 break;
2526
Roland Levillaindff1f282014-11-05 14:15:05 +00002527 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002528 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002529 case Primitive::kPrimBoolean:
2530 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002531 case Primitive::kPrimByte:
2532 case Primitive::kPrimShort:
2533 case Primitive::kPrimInt:
2534 case Primitive::kPrimChar:
2535 // Processing a Dex `int-to-float' instruction.
2536 locations->SetInAt(0, Location::RequiresRegister());
2537 locations->SetOut(Location::RequiresFpuRegister());
2538 break;
2539
2540 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002541 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002542 locations->SetInAt(0, Location::Any());
2543 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002544 break;
2545
Roland Levillaincff13742014-11-17 14:32:17 +00002546 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002547 // Processing a Dex `double-to-float' instruction.
2548 locations->SetInAt(0, Location::RequiresFpuRegister());
2549 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002550 break;
2551
2552 default:
2553 LOG(FATAL) << "Unexpected type conversion from " << input_type
2554 << " to " << result_type;
2555 };
2556 break;
2557
Roland Levillaindff1f282014-11-05 14:15:05 +00002558 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002559 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002560 case Primitive::kPrimBoolean:
2561 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002562 case Primitive::kPrimByte:
2563 case Primitive::kPrimShort:
2564 case Primitive::kPrimInt:
2565 case Primitive::kPrimChar:
2566 // Processing a Dex `int-to-double' instruction.
2567 locations->SetInAt(0, Location::RequiresRegister());
2568 locations->SetOut(Location::RequiresFpuRegister());
2569 break;
2570
2571 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002572 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002573 locations->SetInAt(0, Location::Any());
2574 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002575 break;
2576
Roland Levillaincff13742014-11-17 14:32:17 +00002577 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002578 // Processing a Dex `float-to-double' instruction.
2579 locations->SetInAt(0, Location::RequiresFpuRegister());
2580 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002581 break;
2582
2583 default:
2584 LOG(FATAL) << "Unexpected type conversion from " << input_type
2585 << " to " << result_type;
2586 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002587 break;
2588
2589 default:
2590 LOG(FATAL) << "Unexpected type conversion from " << input_type
2591 << " to " << result_type;
2592 }
2593}
2594
2595void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2596 LocationSummary* locations = conversion->GetLocations();
2597 Location out = locations->Out();
2598 Location in = locations->InAt(0);
2599 Primitive::Type result_type = conversion->GetResultType();
2600 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002601 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002602 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002603 case Primitive::kPrimByte:
2604 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002605 case Primitive::kPrimLong:
2606 // Type conversion from long to byte is a result of code transformations.
2607 if (in.IsRegisterPair()) {
2608 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2609 } else {
2610 DCHECK(in.GetConstant()->IsLongConstant());
2611 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2612 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2613 }
2614 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002615 case Primitive::kPrimBoolean:
2616 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002617 case Primitive::kPrimShort:
2618 case Primitive::kPrimInt:
2619 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002620 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002621 if (in.IsRegister()) {
2622 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002623 } else {
2624 DCHECK(in.GetConstant()->IsIntConstant());
2625 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2626 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2627 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002628 break;
2629
2630 default:
2631 LOG(FATAL) << "Unexpected type conversion from " << input_type
2632 << " to " << result_type;
2633 }
2634 break;
2635
Roland Levillain01a8d712014-11-14 16:27:39 +00002636 case Primitive::kPrimShort:
2637 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002638 case Primitive::kPrimLong:
2639 // Type conversion from long to short is a result of code transformations.
2640 if (in.IsRegisterPair()) {
2641 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2642 } else if (in.IsDoubleStackSlot()) {
2643 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2644 } else {
2645 DCHECK(in.GetConstant()->IsLongConstant());
2646 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2647 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2648 }
2649 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002650 case Primitive::kPrimBoolean:
2651 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002652 case Primitive::kPrimByte:
2653 case Primitive::kPrimInt:
2654 case Primitive::kPrimChar:
2655 // Processing a Dex `int-to-short' instruction.
2656 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002657 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002658 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002659 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002660 } else {
2661 DCHECK(in.GetConstant()->IsIntConstant());
2662 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002663 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002664 }
2665 break;
2666
2667 default:
2668 LOG(FATAL) << "Unexpected type conversion from " << input_type
2669 << " to " << result_type;
2670 }
2671 break;
2672
Roland Levillain946e1432014-11-11 17:35:19 +00002673 case Primitive::kPrimInt:
2674 switch (input_type) {
2675 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002676 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002677 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002678 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002679 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002680 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002681 } else {
2682 DCHECK(in.IsConstant());
2683 DCHECK(in.GetConstant()->IsLongConstant());
2684 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002685 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002686 }
2687 break;
2688
Roland Levillain3f8f9362014-12-02 17:45:01 +00002689 case Primitive::kPrimFloat: {
2690 // Processing a Dex `float-to-int' instruction.
2691 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2692 Register output = out.AsRegister<Register>();
2693 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002694 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002695
2696 __ movl(output, Immediate(kPrimIntMax));
2697 // temp = int-to-float(output)
2698 __ cvtsi2ss(temp, output);
2699 // if input >= temp goto done
2700 __ comiss(input, temp);
2701 __ j(kAboveEqual, &done);
2702 // if input == NaN goto nan
2703 __ j(kUnordered, &nan);
2704 // output = float-to-int-truncate(input)
2705 __ cvttss2si(output, input);
2706 __ jmp(&done);
2707 __ Bind(&nan);
2708 // output = 0
2709 __ xorl(output, output);
2710 __ Bind(&done);
2711 break;
2712 }
2713
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002714 case Primitive::kPrimDouble: {
2715 // Processing a Dex `double-to-int' instruction.
2716 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2717 Register output = out.AsRegister<Register>();
2718 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002719 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002720
2721 __ movl(output, Immediate(kPrimIntMax));
2722 // temp = int-to-double(output)
2723 __ cvtsi2sd(temp, output);
2724 // if input >= temp goto done
2725 __ comisd(input, temp);
2726 __ j(kAboveEqual, &done);
2727 // if input == NaN goto nan
2728 __ j(kUnordered, &nan);
2729 // output = double-to-int-truncate(input)
2730 __ cvttsd2si(output, input);
2731 __ jmp(&done);
2732 __ Bind(&nan);
2733 // output = 0
2734 __ xorl(output, output);
2735 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002736 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002737 }
Roland Levillain946e1432014-11-11 17:35:19 +00002738
2739 default:
2740 LOG(FATAL) << "Unexpected type conversion from " << input_type
2741 << " to " << result_type;
2742 }
2743 break;
2744
Roland Levillaindff1f282014-11-05 14:15:05 +00002745 case Primitive::kPrimLong:
2746 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002747 case Primitive::kPrimBoolean:
2748 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002749 case Primitive::kPrimByte:
2750 case Primitive::kPrimShort:
2751 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002752 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002753 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002754 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2755 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002756 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002757 __ cdq();
2758 break;
2759
2760 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002761 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002762 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002763 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002764 break;
2765
Roland Levillaindff1f282014-11-05 14:15:05 +00002766 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002767 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002768 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002769 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002770 break;
2771
2772 default:
2773 LOG(FATAL) << "Unexpected type conversion from " << input_type
2774 << " to " << result_type;
2775 }
2776 break;
2777
Roland Levillain981e4542014-11-14 11:47:14 +00002778 case Primitive::kPrimChar:
2779 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002780 case Primitive::kPrimLong:
2781 // Type conversion from long to short is a result of code transformations.
2782 if (in.IsRegisterPair()) {
2783 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2784 } else if (in.IsDoubleStackSlot()) {
2785 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2786 } else {
2787 DCHECK(in.GetConstant()->IsLongConstant());
2788 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2789 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2790 }
2791 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002792 case Primitive::kPrimBoolean:
2793 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002794 case Primitive::kPrimByte:
2795 case Primitive::kPrimShort:
2796 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002797 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2798 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002799 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002800 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002801 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002802 } else {
2803 DCHECK(in.GetConstant()->IsIntConstant());
2804 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002805 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002806 }
2807 break;
2808
2809 default:
2810 LOG(FATAL) << "Unexpected type conversion from " << input_type
2811 << " to " << result_type;
2812 }
2813 break;
2814
Roland Levillaindff1f282014-11-05 14:15:05 +00002815 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002816 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002817 case Primitive::kPrimBoolean:
2818 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002819 case Primitive::kPrimByte:
2820 case Primitive::kPrimShort:
2821 case Primitive::kPrimInt:
2822 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002823 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002824 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002825 break;
2826
Roland Levillain6d0e4832014-11-27 18:31:21 +00002827 case Primitive::kPrimLong: {
2828 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002829 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002830
Roland Levillain232ade02015-04-20 15:14:36 +01002831 // Create stack space for the call to
2832 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2833 // TODO: enhance register allocator to ask for stack temporaries.
2834 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2835 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2836 __ subl(ESP, Immediate(adjustment));
2837 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002838
Roland Levillain232ade02015-04-20 15:14:36 +01002839 // Load the value to the FP stack, using temporaries if needed.
2840 PushOntoFPStack(in, 0, adjustment, false, true);
2841
2842 if (out.IsStackSlot()) {
2843 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2844 } else {
2845 __ fstps(Address(ESP, 0));
2846 Location stack_temp = Location::StackSlot(0);
2847 codegen_->Move32(out, stack_temp);
2848 }
2849
2850 // Remove the temporary stack space we allocated.
2851 if (adjustment != 0) {
2852 __ addl(ESP, Immediate(adjustment));
2853 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002854 break;
2855 }
2856
Roland Levillaincff13742014-11-17 14:32:17 +00002857 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002858 // Processing a Dex `double-to-float' instruction.
2859 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002860 break;
2861
2862 default:
2863 LOG(FATAL) << "Unexpected type conversion from " << input_type
2864 << " to " << result_type;
2865 };
2866 break;
2867
Roland Levillaindff1f282014-11-05 14:15:05 +00002868 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002869 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002870 case Primitive::kPrimBoolean:
2871 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002872 case Primitive::kPrimByte:
2873 case Primitive::kPrimShort:
2874 case Primitive::kPrimInt:
2875 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002876 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002877 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002878 break;
2879
Roland Levillain647b9ed2014-11-27 12:06:00 +00002880 case Primitive::kPrimLong: {
2881 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002882 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002883
Roland Levillain232ade02015-04-20 15:14:36 +01002884 // Create stack space for the call to
2885 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2886 // TODO: enhance register allocator to ask for stack temporaries.
2887 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2888 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2889 __ subl(ESP, Immediate(adjustment));
2890 }
2891
2892 // Load the value to the FP stack, using temporaries if needed.
2893 PushOntoFPStack(in, 0, adjustment, false, true);
2894
2895 if (out.IsDoubleStackSlot()) {
2896 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2897 } else {
2898 __ fstpl(Address(ESP, 0));
2899 Location stack_temp = Location::DoubleStackSlot(0);
2900 codegen_->Move64(out, stack_temp);
2901 }
2902
2903 // Remove the temporary stack space we allocated.
2904 if (adjustment != 0) {
2905 __ addl(ESP, Immediate(adjustment));
2906 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002907 break;
2908 }
2909
Roland Levillaincff13742014-11-17 14:32:17 +00002910 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002911 // Processing a Dex `float-to-double' instruction.
2912 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002913 break;
2914
2915 default:
2916 LOG(FATAL) << "Unexpected type conversion from " << input_type
2917 << " to " << result_type;
2918 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002919 break;
2920
2921 default:
2922 LOG(FATAL) << "Unexpected type conversion from " << input_type
2923 << " to " << result_type;
2924 }
2925}
2926
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002927void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002928 LocationSummary* locations =
2929 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002930 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002931 case Primitive::kPrimInt: {
2932 locations->SetInAt(0, Location::RequiresRegister());
2933 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2934 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2935 break;
2936 }
2937
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002938 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002939 locations->SetInAt(0, Location::RequiresRegister());
2940 locations->SetInAt(1, Location::Any());
2941 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002942 break;
2943 }
2944
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002945 case Primitive::kPrimFloat:
2946 case Primitive::kPrimDouble: {
2947 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002948 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2949 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002950 } else if (add->InputAt(1)->IsConstant()) {
2951 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002952 } else {
2953 locations->SetInAt(1, Location::Any());
2954 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002955 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002956 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002957 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002958
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002959 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002960 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2961 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002962 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002963}
2964
2965void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2966 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002967 Location first = locations->InAt(0);
2968 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002969 Location out = locations->Out();
2970
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002971 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002972 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002973 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002974 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2975 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002976 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2977 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002978 } else {
2979 __ leal(out.AsRegister<Register>(), Address(
2980 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2981 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002982 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002983 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2984 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2985 __ addl(out.AsRegister<Register>(), Immediate(value));
2986 } else {
2987 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2988 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002989 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002990 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002991 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002992 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002993 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002994 }
2995
2996 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002997 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002998 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2999 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003000 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003001 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3002 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003003 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003004 } else {
3005 DCHECK(second.IsConstant()) << second;
3006 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3007 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3008 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003009 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003010 break;
3011 }
3012
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003013 case Primitive::kPrimFloat: {
3014 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003015 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003016 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3017 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003018 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003019 __ addss(first.AsFpuRegister<XmmRegister>(),
3020 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003021 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3022 const_area->GetBaseMethodAddress(),
3023 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003024 } else {
3025 DCHECK(second.IsStackSlot());
3026 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003027 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003028 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003029 }
3030
3031 case Primitive::kPrimDouble: {
3032 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003033 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003034 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3035 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003036 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003037 __ addsd(first.AsFpuRegister<XmmRegister>(),
3038 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003039 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3040 const_area->GetBaseMethodAddress(),
3041 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003042 } else {
3043 DCHECK(second.IsDoubleStackSlot());
3044 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003045 }
3046 break;
3047 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003048
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003049 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003050 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003051 }
3052}
3053
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003054void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003055 LocationSummary* locations =
3056 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003057 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003058 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003059 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003060 locations->SetInAt(0, Location::RequiresRegister());
3061 locations->SetInAt(1, Location::Any());
3062 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003063 break;
3064 }
Calin Juravle11351682014-10-23 15:38:15 +01003065 case Primitive::kPrimFloat:
3066 case Primitive::kPrimDouble: {
3067 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003068 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3069 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003070 } else if (sub->InputAt(1)->IsConstant()) {
3071 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003072 } else {
3073 locations->SetInAt(1, Location::Any());
3074 }
Calin Juravle11351682014-10-23 15:38:15 +01003075 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003076 break;
Calin Juravle11351682014-10-23 15:38:15 +01003077 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003078
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003079 default:
Calin Juravle11351682014-10-23 15:38:15 +01003080 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003081 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003082}
3083
3084void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3085 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003086 Location first = locations->InAt(0);
3087 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003088 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003089 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003090 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003091 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003092 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003093 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003094 __ subl(first.AsRegister<Register>(),
3095 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003096 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003097 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003098 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003099 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003100 }
3101
3102 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003103 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003104 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3105 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003106 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003107 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003108 __ sbbl(first.AsRegisterPairHigh<Register>(),
3109 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003110 } else {
3111 DCHECK(second.IsConstant()) << second;
3112 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3113 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3114 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003115 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003116 break;
3117 }
3118
Calin Juravle11351682014-10-23 15:38:15 +01003119 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003120 if (second.IsFpuRegister()) {
3121 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3122 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3123 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003124 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003125 __ subss(first.AsFpuRegister<XmmRegister>(),
3126 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003127 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3128 const_area->GetBaseMethodAddress(),
3129 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003130 } else {
3131 DCHECK(second.IsStackSlot());
3132 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3133 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003134 break;
Calin Juravle11351682014-10-23 15:38:15 +01003135 }
3136
3137 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003138 if (second.IsFpuRegister()) {
3139 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3140 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3141 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003142 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003143 __ subsd(first.AsFpuRegister<XmmRegister>(),
3144 codegen_->LiteralDoubleAddress(
3145 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003146 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003147 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3148 } else {
3149 DCHECK(second.IsDoubleStackSlot());
3150 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3151 }
Calin Juravle11351682014-10-23 15:38:15 +01003152 break;
3153 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003154
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003155 default:
Calin Juravle11351682014-10-23 15:38:15 +01003156 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003157 }
3158}
3159
Calin Juravle34bacdf2014-10-07 20:23:36 +01003160void LocationsBuilderX86::VisitMul(HMul* mul) {
3161 LocationSummary* locations =
3162 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3163 switch (mul->GetResultType()) {
3164 case Primitive::kPrimInt:
3165 locations->SetInAt(0, Location::RequiresRegister());
3166 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003167 if (mul->InputAt(1)->IsIntConstant()) {
3168 // Can use 3 operand multiply.
3169 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3170 } else {
3171 locations->SetOut(Location::SameAsFirstInput());
3172 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003173 break;
3174 case Primitive::kPrimLong: {
3175 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003176 locations->SetInAt(1, Location::Any());
3177 locations->SetOut(Location::SameAsFirstInput());
3178 // Needed for imul on 32bits with 64bits output.
3179 locations->AddTemp(Location::RegisterLocation(EAX));
3180 locations->AddTemp(Location::RegisterLocation(EDX));
3181 break;
3182 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003183 case Primitive::kPrimFloat:
3184 case Primitive::kPrimDouble: {
3185 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003186 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3187 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003188 } else if (mul->InputAt(1)->IsConstant()) {
3189 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003190 } else {
3191 locations->SetInAt(1, Location::Any());
3192 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003193 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003194 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003195 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003196
3197 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003198 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003199 }
3200}
3201
3202void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3203 LocationSummary* locations = mul->GetLocations();
3204 Location first = locations->InAt(0);
3205 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003206 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003207
3208 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003209 case Primitive::kPrimInt:
3210 // The constant may have ended up in a register, so test explicitly to avoid
3211 // problems where the output may not be the same as the first operand.
3212 if (mul->InputAt(1)->IsIntConstant()) {
3213 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3214 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3215 } else if (second.IsRegister()) {
3216 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003217 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003218 } else {
3219 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003220 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003221 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003222 }
3223 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003224
3225 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003226 Register in1_hi = first.AsRegisterPairHigh<Register>();
3227 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003228 Register eax = locations->GetTemp(0).AsRegister<Register>();
3229 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003230
3231 DCHECK_EQ(EAX, eax);
3232 DCHECK_EQ(EDX, edx);
3233
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003234 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003235 // output: in1
3236 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3237 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3238 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003239 if (second.IsConstant()) {
3240 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003241
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003242 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3243 int32_t low_value = Low32Bits(value);
3244 int32_t high_value = High32Bits(value);
3245 Immediate low(low_value);
3246 Immediate high(high_value);
3247
3248 __ movl(eax, high);
3249 // eax <- in1.lo * in2.hi
3250 __ imull(eax, in1_lo);
3251 // in1.hi <- in1.hi * in2.lo
3252 __ imull(in1_hi, low);
3253 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3254 __ addl(in1_hi, eax);
3255 // move in2_lo to eax to prepare for double precision
3256 __ movl(eax, low);
3257 // edx:eax <- in1.lo * in2.lo
3258 __ mull(in1_lo);
3259 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3260 __ addl(in1_hi, edx);
3261 // in1.lo <- (in1.lo * in2.lo)[31:0];
3262 __ movl(in1_lo, eax);
3263 } else if (second.IsRegisterPair()) {
3264 Register in2_hi = second.AsRegisterPairHigh<Register>();
3265 Register in2_lo = second.AsRegisterPairLow<Register>();
3266
3267 __ movl(eax, in2_hi);
3268 // eax <- in1.lo * in2.hi
3269 __ imull(eax, in1_lo);
3270 // in1.hi <- in1.hi * in2.lo
3271 __ imull(in1_hi, in2_lo);
3272 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3273 __ addl(in1_hi, eax);
3274 // move in1_lo to eax to prepare for double precision
3275 __ movl(eax, in1_lo);
3276 // edx:eax <- in1.lo * in2.lo
3277 __ mull(in2_lo);
3278 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3279 __ addl(in1_hi, edx);
3280 // in1.lo <- (in1.lo * in2.lo)[31:0];
3281 __ movl(in1_lo, eax);
3282 } else {
3283 DCHECK(second.IsDoubleStackSlot()) << second;
3284 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3285 Address in2_lo(ESP, second.GetStackIndex());
3286
3287 __ movl(eax, in2_hi);
3288 // eax <- in1.lo * in2.hi
3289 __ imull(eax, in1_lo);
3290 // in1.hi <- in1.hi * in2.lo
3291 __ imull(in1_hi, in2_lo);
3292 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3293 __ addl(in1_hi, eax);
3294 // move in1_lo to eax to prepare for double precision
3295 __ movl(eax, in1_lo);
3296 // edx:eax <- in1.lo * in2.lo
3297 __ mull(in2_lo);
3298 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3299 __ addl(in1_hi, edx);
3300 // in1.lo <- (in1.lo * in2.lo)[31:0];
3301 __ movl(in1_lo, eax);
3302 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003303
3304 break;
3305 }
3306
Calin Juravleb5bfa962014-10-21 18:02:24 +01003307 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003308 DCHECK(first.Equals(locations->Out()));
3309 if (second.IsFpuRegister()) {
3310 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3311 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3312 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003313 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003314 __ mulss(first.AsFpuRegister<XmmRegister>(),
3315 codegen_->LiteralFloatAddress(
3316 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003317 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003318 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3319 } else {
3320 DCHECK(second.IsStackSlot());
3321 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3322 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003323 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003324 }
3325
3326 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003327 DCHECK(first.Equals(locations->Out()));
3328 if (second.IsFpuRegister()) {
3329 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3330 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3331 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003332 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003333 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3334 codegen_->LiteralDoubleAddress(
3335 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003336 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003337 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3338 } else {
3339 DCHECK(second.IsDoubleStackSlot());
3340 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3341 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003342 break;
3343 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003344
3345 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003346 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003347 }
3348}
3349
Roland Levillain232ade02015-04-20 15:14:36 +01003350void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3351 uint32_t temp_offset,
3352 uint32_t stack_adjustment,
3353 bool is_fp,
3354 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003355 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003356 DCHECK(!is_wide);
3357 if (is_fp) {
3358 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3359 } else {
3360 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3361 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003362 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003363 DCHECK(is_wide);
3364 if (is_fp) {
3365 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3366 } else {
3367 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3368 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003369 } else {
3370 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003371 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003372 Location stack_temp = Location::StackSlot(temp_offset);
3373 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003374 if (is_fp) {
3375 __ flds(Address(ESP, temp_offset));
3376 } else {
3377 __ filds(Address(ESP, temp_offset));
3378 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003379 } else {
3380 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3381 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003382 if (is_fp) {
3383 __ fldl(Address(ESP, temp_offset));
3384 } else {
3385 __ fildl(Address(ESP, temp_offset));
3386 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003387 }
3388 }
3389}
3390
3391void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3392 Primitive::Type type = rem->GetResultType();
3393 bool is_float = type == Primitive::kPrimFloat;
3394 size_t elem_size = Primitive::ComponentSize(type);
3395 LocationSummary* locations = rem->GetLocations();
3396 Location first = locations->InAt(0);
3397 Location second = locations->InAt(1);
3398 Location out = locations->Out();
3399
3400 // Create stack space for 2 elements.
3401 // TODO: enhance register allocator to ask for stack temporaries.
3402 __ subl(ESP, Immediate(2 * elem_size));
3403
3404 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003405 const bool is_wide = !is_float;
3406 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3407 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003408
3409 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003410 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003411 __ Bind(&retry);
3412 __ fprem();
3413
3414 // Move FP status to AX.
3415 __ fstsw();
3416
3417 // And see if the argument reduction is complete. This is signaled by the
3418 // C2 FPU flag bit set to 0.
3419 __ andl(EAX, Immediate(kC2ConditionMask));
3420 __ j(kNotEqual, &retry);
3421
3422 // We have settled on the final value. Retrieve it into an XMM register.
3423 // Store FP top of stack to real stack.
3424 if (is_float) {
3425 __ fsts(Address(ESP, 0));
3426 } else {
3427 __ fstl(Address(ESP, 0));
3428 }
3429
3430 // Pop the 2 items from the FP stack.
3431 __ fucompp();
3432
3433 // Load the value from the stack into an XMM register.
3434 DCHECK(out.IsFpuRegister()) << out;
3435 if (is_float) {
3436 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3437 } else {
3438 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3439 }
3440
3441 // And remove the temporary stack space we allocated.
3442 __ addl(ESP, Immediate(2 * elem_size));
3443}
3444
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003445
3446void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3447 DCHECK(instruction->IsDiv() || instruction->IsRem());
3448
3449 LocationSummary* locations = instruction->GetLocations();
3450 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003451 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003452
3453 Register out_register = locations->Out().AsRegister<Register>();
3454 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003455 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003456
3457 DCHECK(imm == 1 || imm == -1);
3458
3459 if (instruction->IsRem()) {
3460 __ xorl(out_register, out_register);
3461 } else {
3462 __ movl(out_register, input_register);
3463 if (imm == -1) {
3464 __ negl(out_register);
3465 }
3466 }
3467}
3468
3469
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003470void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003471 LocationSummary* locations = instruction->GetLocations();
3472
3473 Register out_register = locations->Out().AsRegister<Register>();
3474 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003475 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003476 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3477 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003478
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003479 Register num = locations->GetTemp(0).AsRegister<Register>();
3480
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003481 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003482 __ testl(input_register, input_register);
3483 __ cmovl(kGreaterEqual, num, input_register);
3484 int shift = CTZ(imm);
3485 __ sarl(num, Immediate(shift));
3486
3487 if (imm < 0) {
3488 __ negl(num);
3489 }
3490
3491 __ movl(out_register, num);
3492}
3493
3494void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3495 DCHECK(instruction->IsDiv() || instruction->IsRem());
3496
3497 LocationSummary* locations = instruction->GetLocations();
3498 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3499
3500 Register eax = locations->InAt(0).AsRegister<Register>();
3501 Register out = locations->Out().AsRegister<Register>();
3502 Register num;
3503 Register edx;
3504
3505 if (instruction->IsDiv()) {
3506 edx = locations->GetTemp(0).AsRegister<Register>();
3507 num = locations->GetTemp(1).AsRegister<Register>();
3508 } else {
3509 edx = locations->Out().AsRegister<Register>();
3510 num = locations->GetTemp(0).AsRegister<Register>();
3511 }
3512
3513 DCHECK_EQ(EAX, eax);
3514 DCHECK_EQ(EDX, edx);
3515 if (instruction->IsDiv()) {
3516 DCHECK_EQ(EAX, out);
3517 } else {
3518 DCHECK_EQ(EDX, out);
3519 }
3520
3521 int64_t magic;
3522 int shift;
3523 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3524
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003525 // Save the numerator.
3526 __ movl(num, eax);
3527
3528 // EAX = magic
3529 __ movl(eax, Immediate(magic));
3530
3531 // EDX:EAX = magic * numerator
3532 __ imull(num);
3533
3534 if (imm > 0 && magic < 0) {
3535 // EDX += num
3536 __ addl(edx, num);
3537 } else if (imm < 0 && magic > 0) {
3538 __ subl(edx, num);
3539 }
3540
3541 // Shift if needed.
3542 if (shift != 0) {
3543 __ sarl(edx, Immediate(shift));
3544 }
3545
3546 // EDX += 1 if EDX < 0
3547 __ movl(eax, edx);
3548 __ shrl(edx, Immediate(31));
3549 __ addl(edx, eax);
3550
3551 if (instruction->IsRem()) {
3552 __ movl(eax, num);
3553 __ imull(edx, Immediate(imm));
3554 __ subl(eax, edx);
3555 __ movl(edx, eax);
3556 } else {
3557 __ movl(eax, edx);
3558 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003559}
3560
Calin Juravlebacfec32014-11-14 15:54:36 +00003561void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3562 DCHECK(instruction->IsDiv() || instruction->IsRem());
3563
3564 LocationSummary* locations = instruction->GetLocations();
3565 Location out = locations->Out();
3566 Location first = locations->InAt(0);
3567 Location second = locations->InAt(1);
3568 bool is_div = instruction->IsDiv();
3569
3570 switch (instruction->GetResultType()) {
3571 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003572 DCHECK_EQ(EAX, first.AsRegister<Register>());
3573 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003574
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003575 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003576 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003577
3578 if (imm == 0) {
3579 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3580 } else if (imm == 1 || imm == -1) {
3581 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003582 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003583 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003584 } else {
3585 DCHECK(imm <= -2 || imm >= 2);
3586 GenerateDivRemWithAnyConstant(instruction);
3587 }
3588 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003589 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3590 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003591 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003592
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003593 Register second_reg = second.AsRegister<Register>();
3594 // 0x80000000/-1 triggers an arithmetic exception!
3595 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3596 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003597
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003598 __ cmpl(second_reg, Immediate(-1));
3599 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003600
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003601 // edx:eax <- sign-extended of eax
3602 __ cdq();
3603 // eax = quotient, edx = remainder
3604 __ idivl(second_reg);
3605 __ Bind(slow_path->GetExitLabel());
3606 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003607 break;
3608 }
3609
3610 case Primitive::kPrimLong: {
3611 InvokeRuntimeCallingConvention calling_convention;
3612 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3613 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3614 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3615 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3616 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3617 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3618
3619 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003620 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003621 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003622 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003623 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003624 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003625 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003626 break;
3627 }
3628
3629 default:
3630 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3631 }
3632}
3633
Calin Juravle7c4954d2014-10-28 16:57:40 +00003634void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003635 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003636 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003637 : LocationSummary::kNoCall;
3638 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3639
Calin Juravle7c4954d2014-10-28 16:57:40 +00003640 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003641 case Primitive::kPrimInt: {
3642 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003643 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003644 locations->SetOut(Location::SameAsFirstInput());
3645 // Intel uses edx:eax as the dividend.
3646 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003647 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3648 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3649 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003650 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003651 locations->AddTemp(Location::RequiresRegister());
3652 }
Calin Juravled0d48522014-11-04 16:40:20 +00003653 break;
3654 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003655 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003656 InvokeRuntimeCallingConvention calling_convention;
3657 locations->SetInAt(0, Location::RegisterPairLocation(
3658 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3659 locations->SetInAt(1, Location::RegisterPairLocation(
3660 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3661 // Runtime helper puts the result in EAX, EDX.
3662 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003663 break;
3664 }
3665 case Primitive::kPrimFloat:
3666 case Primitive::kPrimDouble: {
3667 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003668 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3669 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003670 } else if (div->InputAt(1)->IsConstant()) {
3671 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003672 } else {
3673 locations->SetInAt(1, Location::Any());
3674 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003675 locations->SetOut(Location::SameAsFirstInput());
3676 break;
3677 }
3678
3679 default:
3680 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3681 }
3682}
3683
3684void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3685 LocationSummary* locations = div->GetLocations();
3686 Location first = locations->InAt(0);
3687 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003688
3689 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003690 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003691 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003692 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003693 break;
3694 }
3695
3696 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003697 if (second.IsFpuRegister()) {
3698 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3699 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3700 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003701 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003702 __ divss(first.AsFpuRegister<XmmRegister>(),
3703 codegen_->LiteralFloatAddress(
3704 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003705 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003706 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3707 } else {
3708 DCHECK(second.IsStackSlot());
3709 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3710 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003711 break;
3712 }
3713
3714 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003715 if (second.IsFpuRegister()) {
3716 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3717 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3718 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003719 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003720 __ divsd(first.AsFpuRegister<XmmRegister>(),
3721 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003722 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3723 const_area->GetBaseMethodAddress(),
3724 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003725 } else {
3726 DCHECK(second.IsDoubleStackSlot());
3727 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3728 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003729 break;
3730 }
3731
3732 default:
3733 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3734 }
3735}
3736
Calin Juravlebacfec32014-11-14 15:54:36 +00003737void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003738 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003739
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003740 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003741 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003742 : LocationSummary::kNoCall;
3743 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003744
Calin Juravled2ec87d2014-12-08 14:24:46 +00003745 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003746 case Primitive::kPrimInt: {
3747 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003748 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003749 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003750 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3751 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3752 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003753 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003754 locations->AddTemp(Location::RequiresRegister());
3755 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003756 break;
3757 }
3758 case Primitive::kPrimLong: {
3759 InvokeRuntimeCallingConvention calling_convention;
3760 locations->SetInAt(0, Location::RegisterPairLocation(
3761 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3762 locations->SetInAt(1, Location::RegisterPairLocation(
3763 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3764 // Runtime helper puts the result in EAX, EDX.
3765 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3766 break;
3767 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003768 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003769 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003770 locations->SetInAt(0, Location::Any());
3771 locations->SetInAt(1, Location::Any());
3772 locations->SetOut(Location::RequiresFpuRegister());
3773 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003774 break;
3775 }
3776
3777 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003778 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003779 }
3780}
3781
3782void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3783 Primitive::Type type = rem->GetResultType();
3784 switch (type) {
3785 case Primitive::kPrimInt:
3786 case Primitive::kPrimLong: {
3787 GenerateDivRemIntegral(rem);
3788 break;
3789 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003790 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003791 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003792 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003793 break;
3794 }
3795 default:
3796 LOG(FATAL) << "Unexpected rem type " << type;
3797 }
3798}
3799
Calin Juravled0d48522014-11-04 16:40:20 +00003800void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003801 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003802 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003803 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003804 case Primitive::kPrimByte:
3805 case Primitive::kPrimChar:
3806 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003807 case Primitive::kPrimInt: {
3808 locations->SetInAt(0, Location::Any());
3809 break;
3810 }
3811 case Primitive::kPrimLong: {
3812 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3813 if (!instruction->IsConstant()) {
3814 locations->AddTemp(Location::RequiresRegister());
3815 }
3816 break;
3817 }
3818 default:
3819 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3820 }
Calin Juravled0d48522014-11-04 16:40:20 +00003821}
3822
3823void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003824 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003825 codegen_->AddSlowPath(slow_path);
3826
3827 LocationSummary* locations = instruction->GetLocations();
3828 Location value = locations->InAt(0);
3829
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003830 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003831 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003832 case Primitive::kPrimByte:
3833 case Primitive::kPrimChar:
3834 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003835 case Primitive::kPrimInt: {
3836 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003837 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003838 __ j(kEqual, slow_path->GetEntryLabel());
3839 } else if (value.IsStackSlot()) {
3840 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3841 __ j(kEqual, slow_path->GetEntryLabel());
3842 } else {
3843 DCHECK(value.IsConstant()) << value;
3844 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003845 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003846 }
3847 }
3848 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003849 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003850 case Primitive::kPrimLong: {
3851 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003852 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003853 __ movl(temp, value.AsRegisterPairLow<Register>());
3854 __ orl(temp, value.AsRegisterPairHigh<Register>());
3855 __ j(kEqual, slow_path->GetEntryLabel());
3856 } else {
3857 DCHECK(value.IsConstant()) << value;
3858 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3859 __ jmp(slow_path->GetEntryLabel());
3860 }
3861 }
3862 break;
3863 }
3864 default:
3865 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003866 }
Calin Juravled0d48522014-11-04 16:40:20 +00003867}
3868
Calin Juravle9aec02f2014-11-18 23:06:35 +00003869void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3870 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3871
3872 LocationSummary* locations =
3873 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3874
3875 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003876 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003877 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003878 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003879 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003880 // The shift count needs to be in CL or a constant.
3881 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003882 locations->SetOut(Location::SameAsFirstInput());
3883 break;
3884 }
3885 default:
3886 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3887 }
3888}
3889
3890void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3891 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3892
3893 LocationSummary* locations = op->GetLocations();
3894 Location first = locations->InAt(0);
3895 Location second = locations->InAt(1);
3896 DCHECK(first.Equals(locations->Out()));
3897
3898 switch (op->GetResultType()) {
3899 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003900 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003901 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003902 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003903 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003904 DCHECK_EQ(ECX, second_reg);
3905 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003906 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003907 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003908 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003909 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003910 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003911 }
3912 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003913 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003914 if (shift == 0) {
3915 return;
3916 }
3917 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003918 if (op->IsShl()) {
3919 __ shll(first_reg, imm);
3920 } else if (op->IsShr()) {
3921 __ sarl(first_reg, imm);
3922 } else {
3923 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003924 }
3925 }
3926 break;
3927 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003928 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003929 if (second.IsRegister()) {
3930 Register second_reg = second.AsRegister<Register>();
3931 DCHECK_EQ(ECX, second_reg);
3932 if (op->IsShl()) {
3933 GenerateShlLong(first, second_reg);
3934 } else if (op->IsShr()) {
3935 GenerateShrLong(first, second_reg);
3936 } else {
3937 GenerateUShrLong(first, second_reg);
3938 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003939 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003940 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003941 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003942 // Nothing to do if the shift is 0, as the input is already the output.
3943 if (shift != 0) {
3944 if (op->IsShl()) {
3945 GenerateShlLong(first, shift);
3946 } else if (op->IsShr()) {
3947 GenerateShrLong(first, shift);
3948 } else {
3949 GenerateUShrLong(first, shift);
3950 }
3951 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003952 }
3953 break;
3954 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003955 default:
3956 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3957 }
3958}
3959
Mark P Mendell73945692015-04-29 14:56:17 +00003960void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3961 Register low = loc.AsRegisterPairLow<Register>();
3962 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003963 if (shift == 1) {
3964 // This is just an addition.
3965 __ addl(low, low);
3966 __ adcl(high, high);
3967 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003968 // Shift by 32 is easy. High gets low, and low gets 0.
3969 codegen_->EmitParallelMoves(
3970 loc.ToLow(),
3971 loc.ToHigh(),
3972 Primitive::kPrimInt,
3973 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3974 loc.ToLow(),
3975 Primitive::kPrimInt);
3976 } else if (shift > 32) {
3977 // Low part becomes 0. High part is low part << (shift-32).
3978 __ movl(high, low);
3979 __ shll(high, Immediate(shift - 32));
3980 __ xorl(low, low);
3981 } else {
3982 // Between 1 and 31.
3983 __ shld(high, low, Immediate(shift));
3984 __ shll(low, Immediate(shift));
3985 }
3986}
3987
Calin Juravle9aec02f2014-11-18 23:06:35 +00003988void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003989 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003990 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3991 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3992 __ testl(shifter, Immediate(32));
3993 __ j(kEqual, &done);
3994 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3995 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3996 __ Bind(&done);
3997}
3998
Mark P Mendell73945692015-04-29 14:56:17 +00003999void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4000 Register low = loc.AsRegisterPairLow<Register>();
4001 Register high = loc.AsRegisterPairHigh<Register>();
4002 if (shift == 32) {
4003 // Need to copy the sign.
4004 DCHECK_NE(low, high);
4005 __ movl(low, high);
4006 __ sarl(high, Immediate(31));
4007 } else if (shift > 32) {
4008 DCHECK_NE(low, high);
4009 // High part becomes sign. Low part is shifted by shift - 32.
4010 __ movl(low, high);
4011 __ sarl(high, Immediate(31));
4012 __ sarl(low, Immediate(shift - 32));
4013 } else {
4014 // Between 1 and 31.
4015 __ shrd(low, high, Immediate(shift));
4016 __ sarl(high, Immediate(shift));
4017 }
4018}
4019
Calin Juravle9aec02f2014-11-18 23:06:35 +00004020void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004021 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004022 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4023 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4024 __ testl(shifter, Immediate(32));
4025 __ j(kEqual, &done);
4026 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4027 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4028 __ Bind(&done);
4029}
4030
Mark P Mendell73945692015-04-29 14:56:17 +00004031void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4032 Register low = loc.AsRegisterPairLow<Register>();
4033 Register high = loc.AsRegisterPairHigh<Register>();
4034 if (shift == 32) {
4035 // Shift by 32 is easy. Low gets high, and high gets 0.
4036 codegen_->EmitParallelMoves(
4037 loc.ToHigh(),
4038 loc.ToLow(),
4039 Primitive::kPrimInt,
4040 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4041 loc.ToHigh(),
4042 Primitive::kPrimInt);
4043 } else if (shift > 32) {
4044 // Low part is high >> (shift - 32). High part becomes 0.
4045 __ movl(low, high);
4046 __ shrl(low, Immediate(shift - 32));
4047 __ xorl(high, high);
4048 } else {
4049 // Between 1 and 31.
4050 __ shrd(low, high, Immediate(shift));
4051 __ shrl(high, Immediate(shift));
4052 }
4053}
4054
Calin Juravle9aec02f2014-11-18 23:06:35 +00004055void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004056 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004057 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4058 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4059 __ testl(shifter, Immediate(32));
4060 __ j(kEqual, &done);
4061 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4062 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4063 __ Bind(&done);
4064}
4065
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004066void LocationsBuilderX86::VisitRor(HRor* ror) {
4067 LocationSummary* locations =
4068 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4069
4070 switch (ror->GetResultType()) {
4071 case Primitive::kPrimLong:
4072 // Add the temporary needed.
4073 locations->AddTemp(Location::RequiresRegister());
4074 FALLTHROUGH_INTENDED;
4075 case Primitive::kPrimInt:
4076 locations->SetInAt(0, Location::RequiresRegister());
4077 // The shift count needs to be in CL (unless it is a constant).
4078 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4079 locations->SetOut(Location::SameAsFirstInput());
4080 break;
4081 default:
4082 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4083 UNREACHABLE();
4084 }
4085}
4086
4087void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4088 LocationSummary* locations = ror->GetLocations();
4089 Location first = locations->InAt(0);
4090 Location second = locations->InAt(1);
4091
4092 if (ror->GetResultType() == Primitive::kPrimInt) {
4093 Register first_reg = first.AsRegister<Register>();
4094 if (second.IsRegister()) {
4095 Register second_reg = second.AsRegister<Register>();
4096 __ rorl(first_reg, second_reg);
4097 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004098 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004099 __ rorl(first_reg, imm);
4100 }
4101 return;
4102 }
4103
4104 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4105 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4106 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4107 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4108 if (second.IsRegister()) {
4109 Register second_reg = second.AsRegister<Register>();
4110 DCHECK_EQ(second_reg, ECX);
4111 __ movl(temp_reg, first_reg_hi);
4112 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4113 __ shrd(first_reg_lo, temp_reg, second_reg);
4114 __ movl(temp_reg, first_reg_hi);
4115 __ testl(second_reg, Immediate(32));
4116 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4117 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4118 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004119 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004120 if (shift_amt == 0) {
4121 // Already fine.
4122 return;
4123 }
4124 if (shift_amt == 32) {
4125 // Just swap.
4126 __ movl(temp_reg, first_reg_lo);
4127 __ movl(first_reg_lo, first_reg_hi);
4128 __ movl(first_reg_hi, temp_reg);
4129 return;
4130 }
4131
4132 Immediate imm(shift_amt);
4133 // Save the constents of the low value.
4134 __ movl(temp_reg, first_reg_lo);
4135
4136 // Shift right into low, feeding bits from high.
4137 __ shrd(first_reg_lo, first_reg_hi, imm);
4138
4139 // Shift right into high, feeding bits from the original low.
4140 __ shrd(first_reg_hi, temp_reg, imm);
4141
4142 // Swap if needed.
4143 if (shift_amt > 32) {
4144 __ movl(temp_reg, first_reg_lo);
4145 __ movl(first_reg_lo, first_reg_hi);
4146 __ movl(first_reg_hi, temp_reg);
4147 }
4148 }
4149}
4150
Calin Juravle9aec02f2014-11-18 23:06:35 +00004151void LocationsBuilderX86::VisitShl(HShl* shl) {
4152 HandleShift(shl);
4153}
4154
4155void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4156 HandleShift(shl);
4157}
4158
4159void LocationsBuilderX86::VisitShr(HShr* shr) {
4160 HandleShift(shr);
4161}
4162
4163void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4164 HandleShift(shr);
4165}
4166
4167void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4168 HandleShift(ushr);
4169}
4170
4171void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4172 HandleShift(ushr);
4173}
4174
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004175void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004176 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004177 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004178 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004179 if (instruction->IsStringAlloc()) {
4180 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4181 } else {
4182 InvokeRuntimeCallingConvention calling_convention;
4183 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004184 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004185}
4186
4187void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004188 // Note: if heap poisoning is enabled, the entry point takes cares
4189 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004190 if (instruction->IsStringAlloc()) {
4191 // String is allocated through StringFactory. Call NewEmptyString entry point.
4192 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004193 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004194 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4195 __ call(Address(temp, code_offset.Int32Value()));
4196 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4197 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004198 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004199 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004200 DCHECK(!codegen_->IsLeafMethod());
4201 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004202}
4203
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004204void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4205 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004206 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004207 locations->SetOut(Location::RegisterLocation(EAX));
4208 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004209 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4210 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004211}
4212
4213void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004214 // Note: if heap poisoning is enabled, the entry point takes cares
4215 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004216 QuickEntrypointEnum entrypoint =
4217 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4218 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004219 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004220 DCHECK(!codegen_->IsLeafMethod());
4221}
4222
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004223void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004224 LocationSummary* locations =
4225 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004226 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4227 if (location.IsStackSlot()) {
4228 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4229 } else if (location.IsDoubleStackSlot()) {
4230 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004231 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004232 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004233}
4234
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004235void InstructionCodeGeneratorX86::VisitParameterValue(
4236 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4237}
4238
4239void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4240 LocationSummary* locations =
4241 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4242 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4243}
4244
4245void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004246}
4247
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004248void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4249 LocationSummary* locations =
4250 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4251 locations->SetInAt(0, Location::RequiresRegister());
4252 locations->SetOut(Location::RequiresRegister());
4253}
4254
4255void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4256 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004257 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004258 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004259 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004260 __ movl(locations->Out().AsRegister<Register>(),
4261 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004262 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004263 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004264 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004265 __ movl(locations->Out().AsRegister<Register>(),
4266 Address(locations->InAt(0).AsRegister<Register>(),
4267 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4268 // temp = temp->GetImtEntryAt(method_offset);
4269 __ movl(locations->Out().AsRegister<Register>(),
4270 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004271 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004272}
4273
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004274void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004275 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004276 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004277 locations->SetInAt(0, Location::RequiresRegister());
4278 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004279}
4280
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004281void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4282 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004283 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004284 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004285 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004286 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004287 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004288 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004289 break;
4290
4291 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004292 __ notl(out.AsRegisterPairLow<Register>());
4293 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004294 break;
4295
4296 default:
4297 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4298 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004299}
4300
David Brazdil66d126e2015-04-03 16:02:44 +01004301void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4302 LocationSummary* locations =
4303 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4304 locations->SetInAt(0, Location::RequiresRegister());
4305 locations->SetOut(Location::SameAsFirstInput());
4306}
4307
4308void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004309 LocationSummary* locations = bool_not->GetLocations();
4310 Location in = locations->InAt(0);
4311 Location out = locations->Out();
4312 DCHECK(in.Equals(out));
4313 __ xorl(out.AsRegister<Register>(), Immediate(1));
4314}
4315
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004316void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004317 LocationSummary* locations =
4318 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004319 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004320 case Primitive::kPrimBoolean:
4321 case Primitive::kPrimByte:
4322 case Primitive::kPrimShort:
4323 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004324 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004325 case Primitive::kPrimLong: {
4326 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004327 locations->SetInAt(1, Location::Any());
4328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4329 break;
4330 }
4331 case Primitive::kPrimFloat:
4332 case Primitive::kPrimDouble: {
4333 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004334 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4335 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4336 } else if (compare->InputAt(1)->IsConstant()) {
4337 locations->SetInAt(1, Location::RequiresFpuRegister());
4338 } else {
4339 locations->SetInAt(1, Location::Any());
4340 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004341 locations->SetOut(Location::RequiresRegister());
4342 break;
4343 }
4344 default:
4345 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4346 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004347}
4348
4349void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004350 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004351 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004352 Location left = locations->InAt(0);
4353 Location right = locations->InAt(1);
4354
Mark Mendell0c9497d2015-08-21 09:30:05 -04004355 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004356 Condition less_cond = kLess;
4357
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004358 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004359 case Primitive::kPrimBoolean:
4360 case Primitive::kPrimByte:
4361 case Primitive::kPrimShort:
4362 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004363 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004364 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004365 break;
4366 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004367 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004368 Register left_low = left.AsRegisterPairLow<Register>();
4369 Register left_high = left.AsRegisterPairHigh<Register>();
4370 int32_t val_low = 0;
4371 int32_t val_high = 0;
4372 bool right_is_const = false;
4373
4374 if (right.IsConstant()) {
4375 DCHECK(right.GetConstant()->IsLongConstant());
4376 right_is_const = true;
4377 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4378 val_low = Low32Bits(val);
4379 val_high = High32Bits(val);
4380 }
4381
Calin Juravleddb7df22014-11-25 20:56:51 +00004382 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004383 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004384 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004385 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004386 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004387 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004388 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004389 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004390 __ j(kLess, &less); // Signed compare.
4391 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004392 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004393 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004394 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004395 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004396 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004397 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004398 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004399 }
Aart Bika19616e2016-02-01 18:57:58 -08004400 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004401 break;
4402 }
4403 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004404 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004405 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004406 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004407 break;
4408 }
4409 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004410 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004411 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004412 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004413 break;
4414 }
4415 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004416 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004417 }
Aart Bika19616e2016-02-01 18:57:58 -08004418
Calin Juravleddb7df22014-11-25 20:56:51 +00004419 __ movl(out, Immediate(0));
4420 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004421 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004422
4423 __ Bind(&greater);
4424 __ movl(out, Immediate(1));
4425 __ jmp(&done);
4426
4427 __ Bind(&less);
4428 __ movl(out, Immediate(-1));
4429
4430 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004431}
4432
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004433void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004434 LocationSummary* locations =
4435 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004436 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004437 locations->SetInAt(i, Location::Any());
4438 }
4439 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004440}
4441
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004442void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004443 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004444}
4445
Roland Levillain7c1559a2015-12-15 10:55:36 +00004446void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004447 /*
4448 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4449 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4450 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4451 */
4452 switch (kind) {
4453 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004454 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004455 break;
4456 }
4457 case MemBarrierKind::kAnyStore:
4458 case MemBarrierKind::kLoadAny:
4459 case MemBarrierKind::kStoreStore: {
4460 // nop
4461 break;
4462 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004463 case MemBarrierKind::kNTStoreStore:
4464 // Non-Temporal Store/Store needs an explicit fence.
4465 MemoryFence(/* non-temporal */ true);
4466 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004467 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004468}
4469
Vladimir Markodc151b22015-10-15 18:02:30 +01004470HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4471 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004472 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004473 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004474}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004475
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004476Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4477 Register temp) {
4478 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004479 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004480 if (!invoke->GetLocations()->Intrinsified()) {
4481 return location.AsRegister<Register>();
4482 }
4483 // For intrinsics we allow any location, so it may be on the stack.
4484 if (!location.IsRegister()) {
4485 __ movl(temp, Address(ESP, location.GetStackIndex()));
4486 return temp;
4487 }
4488 // For register locations, check if the register was saved. If so, get it from the stack.
4489 // Note: There is a chance that the register was saved but not overwritten, so we could
4490 // save one load. However, since this is just an intrinsic slow path we prefer this
4491 // simple and more robust approach rather that trying to determine if that's the case.
4492 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004493 if (slow_path != nullptr) {
4494 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4495 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4496 __ movl(temp, Address(ESP, stack_offset));
4497 return temp;
4498 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004499 }
4500 return location.AsRegister<Register>();
4501}
4502
Serguei Katkov288c7a82016-05-16 11:53:15 +06004503Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4504 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004505 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4506 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004507 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004508 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004509 uint32_t offset =
4510 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4511 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004512 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004513 }
Vladimir Marko58155012015-08-19 12:49:41 +00004514 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004515 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004516 break;
4517 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4518 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4519 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004520 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4521 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4522 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004523 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004524 // Bind a new fixup label at the end of the "movl" insn.
4525 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004526 __ Bind(NewPcRelativeDexCacheArrayPatch(
4527 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
4528 invoke->GetDexFileForPcRelativeDexCache(),
4529 offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004530 break;
4531 }
Vladimir Marko58155012015-08-19 12:49:41 +00004532 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004533 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004534 Register method_reg;
4535 Register reg = temp.AsRegister<Register>();
4536 if (current_method.IsRegister()) {
4537 method_reg = current_method.AsRegister<Register>();
4538 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004539 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004540 DCHECK(!current_method.IsValid());
4541 method_reg = reg;
4542 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4543 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004544 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004545 __ movl(reg, Address(method_reg,
4546 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004547 // temp = temp[index_in_cache];
4548 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4549 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004550 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4551 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004552 }
Vladimir Marko58155012015-08-19 12:49:41 +00004553 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004554 return callee_method;
4555}
4556
4557void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4558 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004559
4560 switch (invoke->GetCodePtrLocation()) {
4561 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4562 __ call(GetFrameEntryLabel());
4563 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004564 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4565 // (callee_method + offset_of_quick_compiled_code)()
4566 __ call(Address(callee_method.AsRegister<Register>(),
4567 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004568 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004569 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004570 }
4571
4572 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004573}
4574
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004575void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4576 Register temp = temp_in.AsRegister<Register>();
4577 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4578 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004579
4580 // Use the calling convention instead of the location of the receiver, as
4581 // intrinsics may have put the receiver in a different register. In the intrinsics
4582 // slow path, the arguments have been moved to the right place, so here we are
4583 // guaranteed that the receiver is the first register of the calling convention.
4584 InvokeDexCallingConvention calling_convention;
4585 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004586 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004587 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004588 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004589 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004590 // Instead of simply (possibly) unpoisoning `temp` here, we should
4591 // emit a read barrier for the previous class reference load.
4592 // However this is not required in practice, as this is an
4593 // intermediate/temporary reference and because the current
4594 // concurrent copying collector keeps the from-space memory
4595 // intact/accessible until the end of the marking phase (the
4596 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004597 __ MaybeUnpoisonHeapReference(temp);
4598 // temp = temp->GetMethodAt(method_offset);
4599 __ movl(temp, Address(temp, method_offset));
4600 // call temp->GetEntryPoint();
4601 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004602 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004603}
4604
Vladimir Markoaad75c62016-10-03 08:46:48 +00004605void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4606 DCHECK(GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004607 HX86ComputeBaseMethodAddress* address = nullptr;
4608 if (GetCompilerOptions().GetCompilePic()) {
4609 address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4610 } else {
4611 DCHECK_EQ(load_string->InputCount(), 0u);
4612 }
4613 string_patches_.emplace_back(address,
4614 load_string->GetDexFile(),
4615 load_string->GetStringIndex().index_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004616 __ Bind(&string_patches_.back().label);
4617}
4618
Vladimir Marko1998cd02017-01-13 13:02:58 +00004619void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004620 HX86ComputeBaseMethodAddress* address = nullptr;
4621 if (GetCompilerOptions().GetCompilePic()) {
4622 address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4623 } else {
4624 DCHECK_EQ(load_class->InputCount(), 0u);
4625 }
4626 boot_image_type_patches_.emplace_back(address,
4627 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004628 load_class->GetTypeIndex().index_);
4629 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004630}
4631
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004632Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004633 HX86ComputeBaseMethodAddress* address =
4634 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4635 type_bss_entry_patches_.emplace_back(
4636 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004637 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004638}
4639
Vladimir Markoaad75c62016-10-03 08:46:48 +00004640Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4641 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004642 HX86ComputeBaseMethodAddress* address =
4643 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4644 string_patches_.emplace_back(
4645 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004646 return &string_patches_.back().label;
4647}
4648
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004649Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(
4650 HX86ComputeBaseMethodAddress* method_address,
4651 const DexFile& dex_file,
4652 uint32_t element_offset) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004653 // Add the patch entry and bind its label at the end of the instruction.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004654 pc_relative_dex_cache_patches_.emplace_back(method_address, dex_file, element_offset);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004655 return &pc_relative_dex_cache_patches_.back().label;
4656}
4657
Vladimir Markoaad75c62016-10-03 08:46:48 +00004658// The label points to the end of the "movl" or another instruction but the literal offset
4659// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4660constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4661
4662template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4663inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004664 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004665 ArenaVector<LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004666 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004667 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004668 linker_patches->push_back(Factory(
4669 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004670 }
4671}
4672
Vladimir Marko58155012015-08-19 12:49:41 +00004673void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4674 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004675 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004676 pc_relative_dex_cache_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004677 string_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004678 boot_image_type_patches_.size() +
4679 type_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004680 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004681 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4682 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004683 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004684 DCHECK(boot_image_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004685 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4686 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004687 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(boot_image_type_patches_,
4688 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004689 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004690 } else {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004691 for (const PatchInfo<Label>& info : boot_image_type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004692 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004693 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004694 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004695 for (const PatchInfo<Label>& info : string_patches_) {
4696 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4697 linker_patches->push_back(
4698 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
4699 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004700 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004701 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4702 linker_patches);
4703 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004704}
4705
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004706void CodeGeneratorX86::MarkGCCard(Register temp,
4707 Register card,
4708 Register object,
4709 Register value,
4710 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004711 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004712 if (value_can_be_null) {
4713 __ testl(value, value);
4714 __ j(kEqual, &is_null);
4715 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004716 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004717 __ movl(temp, object);
4718 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004719 __ movb(Address(temp, card, TIMES_1, 0),
4720 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004721 if (value_can_be_null) {
4722 __ Bind(&is_null);
4723 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004724}
4725
Calin Juravle52c48962014-12-16 17:02:57 +00004726void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4727 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004728
4729 bool object_field_get_with_read_barrier =
4730 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004731 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004732 new (GetGraph()->GetArena()) LocationSummary(instruction,
4733 kEmitCompilerReadBarrier ?
4734 LocationSummary::kCallOnSlowPath :
4735 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004736 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004737 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004738 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004739 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004740
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004741 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4742 locations->SetOut(Location::RequiresFpuRegister());
4743 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004744 // The output overlaps in case of long: we don't want the low move
4745 // to overwrite the object's location. Likewise, in the case of
4746 // an object field get with read barriers enabled, we do not want
4747 // the move to overwrite the object's location, as we need it to emit
4748 // the read barrier.
4749 locations->SetOut(
4750 Location::RequiresRegister(),
4751 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4752 Location::kOutputOverlap :
4753 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004754 }
Calin Juravle52c48962014-12-16 17:02:57 +00004755
4756 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4757 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004758 // So we use an XMM register as a temp to achieve atomicity (first
4759 // load the temp into the XMM and then copy the XMM into the
4760 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004761 locations->AddTemp(Location::RequiresFpuRegister());
4762 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004763}
4764
Calin Juravle52c48962014-12-16 17:02:57 +00004765void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4766 const FieldInfo& field_info) {
4767 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004768
Calin Juravle52c48962014-12-16 17:02:57 +00004769 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004770 Location base_loc = locations->InAt(0);
4771 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004772 Location out = locations->Out();
4773 bool is_volatile = field_info.IsVolatile();
4774 Primitive::Type field_type = field_info.GetFieldType();
4775 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4776
4777 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004778 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004779 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004780 break;
4781 }
4782
4783 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004784 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004785 break;
4786 }
4787
4788 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004789 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004790 break;
4791 }
4792
4793 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004794 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004795 break;
4796 }
4797
4798 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004799 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004800 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004801
4802 case Primitive::kPrimNot: {
4803 // /* HeapReference<Object> */ out = *(base + offset)
4804 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004805 // Note that a potential implicit null check is handled in this
4806 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4807 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004808 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004809 if (is_volatile) {
4810 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4811 }
4812 } else {
4813 __ movl(out.AsRegister<Register>(), Address(base, offset));
4814 codegen_->MaybeRecordImplicitNullCheck(instruction);
4815 if (is_volatile) {
4816 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4817 }
4818 // If read barriers are enabled, emit read barriers other than
4819 // Baker's using a slow path (and also unpoison the loaded
4820 // reference, if heap poisoning is enabled).
4821 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4822 }
4823 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004824 }
4825
4826 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004827 if (is_volatile) {
4828 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4829 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004830 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004831 __ movd(out.AsRegisterPairLow<Register>(), temp);
4832 __ psrlq(temp, Immediate(32));
4833 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4834 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004835 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004836 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004837 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004838 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4839 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004840 break;
4841 }
4842
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004843 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004844 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004845 break;
4846 }
4847
4848 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004849 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004850 break;
4851 }
4852
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004853 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004854 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004855 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004856 }
Calin Juravle52c48962014-12-16 17:02:57 +00004857
Roland Levillain7c1559a2015-12-15 10:55:36 +00004858 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4859 // Potential implicit null checks, in the case of reference or
4860 // long fields, are handled in the previous switch statement.
4861 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004862 codegen_->MaybeRecordImplicitNullCheck(instruction);
4863 }
4864
Calin Juravle52c48962014-12-16 17:02:57 +00004865 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004866 if (field_type == Primitive::kPrimNot) {
4867 // Memory barriers, in the case of references, are also handled
4868 // in the previous switch statement.
4869 } else {
4870 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4871 }
Roland Levillain4d027112015-07-01 15:41:14 +01004872 }
Calin Juravle52c48962014-12-16 17:02:57 +00004873}
4874
4875void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4876 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4877
4878 LocationSummary* locations =
4879 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4880 locations->SetInAt(0, Location::RequiresRegister());
4881 bool is_volatile = field_info.IsVolatile();
4882 Primitive::Type field_type = field_info.GetFieldType();
4883 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4884 || (field_type == Primitive::kPrimByte);
4885
4886 // The register allocator does not support multiple
4887 // inputs that die at entry with one in a specific register.
4888 if (is_byte_type) {
4889 // Ensure the value is in a byte register.
4890 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004891 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004892 if (is_volatile && field_type == Primitive::kPrimDouble) {
4893 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4894 locations->SetInAt(1, Location::RequiresFpuRegister());
4895 } else {
4896 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4897 }
4898 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4899 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004900 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004901
Calin Juravle52c48962014-12-16 17:02:57 +00004902 // 64bits value can be atomically written to an address with movsd and an XMM register.
4903 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4904 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4905 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4906 // isolated cases when we need this it isn't worth adding the extra complexity.
4907 locations->AddTemp(Location::RequiresFpuRegister());
4908 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004909 } else {
4910 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4911
4912 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4913 // Temporary registers for the write barrier.
4914 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4915 // Ensure the card is in a byte register.
4916 locations->AddTemp(Location::RegisterLocation(ECX));
4917 }
Calin Juravle52c48962014-12-16 17:02:57 +00004918 }
4919}
4920
4921void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004922 const FieldInfo& field_info,
4923 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004924 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4925
4926 LocationSummary* locations = instruction->GetLocations();
4927 Register base = locations->InAt(0).AsRegister<Register>();
4928 Location value = locations->InAt(1);
4929 bool is_volatile = field_info.IsVolatile();
4930 Primitive::Type field_type = field_info.GetFieldType();
4931 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004932 bool needs_write_barrier =
4933 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004934
4935 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004936 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004937 }
4938
Mark Mendell81489372015-11-04 11:30:41 -05004939 bool maybe_record_implicit_null_check_done = false;
4940
Calin Juravle52c48962014-12-16 17:02:57 +00004941 switch (field_type) {
4942 case Primitive::kPrimBoolean:
4943 case Primitive::kPrimByte: {
4944 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4945 break;
4946 }
4947
4948 case Primitive::kPrimShort:
4949 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004950 if (value.IsConstant()) {
4951 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4952 __ movw(Address(base, offset), Immediate(v));
4953 } else {
4954 __ movw(Address(base, offset), value.AsRegister<Register>());
4955 }
Calin Juravle52c48962014-12-16 17:02:57 +00004956 break;
4957 }
4958
4959 case Primitive::kPrimInt:
4960 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004961 if (kPoisonHeapReferences && needs_write_barrier) {
4962 // Note that in the case where `value` is a null reference,
4963 // we do not enter this block, as the reference does not
4964 // need poisoning.
4965 DCHECK_EQ(field_type, Primitive::kPrimNot);
4966 Register temp = locations->GetTemp(0).AsRegister<Register>();
4967 __ movl(temp, value.AsRegister<Register>());
4968 __ PoisonHeapReference(temp);
4969 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004970 } else if (value.IsConstant()) {
4971 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4972 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004973 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004974 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004975 __ movl(Address(base, offset), value.AsRegister<Register>());
4976 }
Calin Juravle52c48962014-12-16 17:02:57 +00004977 break;
4978 }
4979
4980 case Primitive::kPrimLong: {
4981 if (is_volatile) {
4982 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4983 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4984 __ movd(temp1, value.AsRegisterPairLow<Register>());
4985 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4986 __ punpckldq(temp1, temp2);
4987 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004988 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004989 } else if (value.IsConstant()) {
4990 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4991 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4992 codegen_->MaybeRecordImplicitNullCheck(instruction);
4993 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004994 } else {
4995 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004996 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004997 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4998 }
Mark Mendell81489372015-11-04 11:30:41 -05004999 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005000 break;
5001 }
5002
5003 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05005004 if (value.IsConstant()) {
5005 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5006 __ movl(Address(base, offset), Immediate(v));
5007 } else {
5008 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5009 }
Calin Juravle52c48962014-12-16 17:02:57 +00005010 break;
5011 }
5012
5013 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005014 if (value.IsConstant()) {
5015 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5016 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5017 codegen_->MaybeRecordImplicitNullCheck(instruction);
5018 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5019 maybe_record_implicit_null_check_done = true;
5020 } else {
5021 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5022 }
Calin Juravle52c48962014-12-16 17:02:57 +00005023 break;
5024 }
5025
5026 case Primitive::kPrimVoid:
5027 LOG(FATAL) << "Unreachable type " << field_type;
5028 UNREACHABLE();
5029 }
5030
Mark Mendell81489372015-11-04 11:30:41 -05005031 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005032 codegen_->MaybeRecordImplicitNullCheck(instruction);
5033 }
5034
Roland Levillain4d027112015-07-01 15:41:14 +01005035 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005036 Register temp = locations->GetTemp(0).AsRegister<Register>();
5037 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005038 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005039 }
5040
Calin Juravle52c48962014-12-16 17:02:57 +00005041 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005042 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005043 }
5044}
5045
5046void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5047 HandleFieldGet(instruction, instruction->GetFieldInfo());
5048}
5049
5050void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5051 HandleFieldGet(instruction, instruction->GetFieldInfo());
5052}
5053
5054void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5055 HandleFieldSet(instruction, instruction->GetFieldInfo());
5056}
5057
5058void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005059 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005060}
5061
5062void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5063 HandleFieldSet(instruction, instruction->GetFieldInfo());
5064}
5065
5066void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005067 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005068}
5069
5070void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5071 HandleFieldGet(instruction, instruction->GetFieldInfo());
5072}
5073
5074void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5075 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005076}
5077
Calin Juravlee460d1d2015-09-29 04:52:17 +01005078void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5079 HUnresolvedInstanceFieldGet* instruction) {
5080 FieldAccessCallingConventionX86 calling_convention;
5081 codegen_->CreateUnresolvedFieldLocationSummary(
5082 instruction, instruction->GetFieldType(), calling_convention);
5083}
5084
5085void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5086 HUnresolvedInstanceFieldGet* instruction) {
5087 FieldAccessCallingConventionX86 calling_convention;
5088 codegen_->GenerateUnresolvedFieldAccess(instruction,
5089 instruction->GetFieldType(),
5090 instruction->GetFieldIndex(),
5091 instruction->GetDexPc(),
5092 calling_convention);
5093}
5094
5095void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5096 HUnresolvedInstanceFieldSet* instruction) {
5097 FieldAccessCallingConventionX86 calling_convention;
5098 codegen_->CreateUnresolvedFieldLocationSummary(
5099 instruction, instruction->GetFieldType(), calling_convention);
5100}
5101
5102void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5103 HUnresolvedInstanceFieldSet* instruction) {
5104 FieldAccessCallingConventionX86 calling_convention;
5105 codegen_->GenerateUnresolvedFieldAccess(instruction,
5106 instruction->GetFieldType(),
5107 instruction->GetFieldIndex(),
5108 instruction->GetDexPc(),
5109 calling_convention);
5110}
5111
5112void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5113 HUnresolvedStaticFieldGet* instruction) {
5114 FieldAccessCallingConventionX86 calling_convention;
5115 codegen_->CreateUnresolvedFieldLocationSummary(
5116 instruction, instruction->GetFieldType(), calling_convention);
5117}
5118
5119void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5120 HUnresolvedStaticFieldGet* instruction) {
5121 FieldAccessCallingConventionX86 calling_convention;
5122 codegen_->GenerateUnresolvedFieldAccess(instruction,
5123 instruction->GetFieldType(),
5124 instruction->GetFieldIndex(),
5125 instruction->GetDexPc(),
5126 calling_convention);
5127}
5128
5129void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5130 HUnresolvedStaticFieldSet* instruction) {
5131 FieldAccessCallingConventionX86 calling_convention;
5132 codegen_->CreateUnresolvedFieldLocationSummary(
5133 instruction, instruction->GetFieldType(), calling_convention);
5134}
5135
5136void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5137 HUnresolvedStaticFieldSet* instruction) {
5138 FieldAccessCallingConventionX86 calling_convention;
5139 codegen_->GenerateUnresolvedFieldAccess(instruction,
5140 instruction->GetFieldType(),
5141 instruction->GetFieldIndex(),
5142 instruction->GetDexPc(),
5143 calling_convention);
5144}
5145
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005146void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005147 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5148 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5149 ? Location::RequiresRegister()
5150 : Location::Any();
5151 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005152}
5153
Calin Juravle2ae48182016-03-16 14:05:09 +00005154void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5155 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005156 return;
5157 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005158 LocationSummary* locations = instruction->GetLocations();
5159 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005160
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005161 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005162 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005163}
5164
Calin Juravle2ae48182016-03-16 14:05:09 +00005165void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005166 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005167 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005168
5169 LocationSummary* locations = instruction->GetLocations();
5170 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005171
5172 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005173 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005174 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005175 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005176 } else {
5177 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005178 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005179 __ jmp(slow_path->GetEntryLabel());
5180 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005181 }
5182 __ j(kEqual, slow_path->GetEntryLabel());
5183}
5184
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005185void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005186 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005187}
5188
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005189void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005190 bool object_array_get_with_read_barrier =
5191 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005192 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005193 new (GetGraph()->GetArena()) LocationSummary(instruction,
5194 object_array_get_with_read_barrier ?
5195 LocationSummary::kCallOnSlowPath :
5196 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005197 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005198 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005199 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005200 locations->SetInAt(0, Location::RequiresRegister());
5201 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005202 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5203 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5204 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005205 // The output overlaps in case of long: we don't want the low move
5206 // to overwrite the array's location. Likewise, in the case of an
5207 // object array get with read barriers enabled, we do not want the
5208 // move to overwrite the array's location, as we need it to emit
5209 // the read barrier.
5210 locations->SetOut(
5211 Location::RequiresRegister(),
5212 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5213 Location::kOutputOverlap :
5214 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005215 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216}
5217
5218void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5219 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005220 Location obj_loc = locations->InAt(0);
5221 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005222 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005223 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005224 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005225
Calin Juravle77520bc2015-01-12 18:45:46 +00005226 Primitive::Type type = instruction->GetType();
5227 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005228 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005229 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005230 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 break;
5232 }
5233
5234 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005235 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005236 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237 break;
5238 }
5239
5240 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005241 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005242 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005243 break;
5244 }
5245
5246 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005247 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005248 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5249 // Branch cases into compressed and uncompressed for each index's type.
5250 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5251 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005252 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005253 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005254 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5255 "Expecting 0=compressed, 1=uncompressed");
5256 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005257 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5258 __ jmp(&done);
5259 __ Bind(&not_compressed);
5260 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5261 __ Bind(&done);
5262 } else {
5263 // Common case for charAt of array of char or when string compression's
5264 // feature is turned off.
5265 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5266 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005267 break;
5268 }
5269
Roland Levillain7c1559a2015-12-15 10:55:36 +00005270 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005271 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005272 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005273 break;
5274 }
5275
Roland Levillain7c1559a2015-12-15 10:55:36 +00005276 case Primitive::kPrimNot: {
5277 static_assert(
5278 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5279 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005280 // /* HeapReference<Object> */ out =
5281 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5282 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005283 // Note that a potential implicit null check is handled in this
5284 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5285 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005286 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005287 } else {
5288 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005289 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5290 codegen_->MaybeRecordImplicitNullCheck(instruction);
5291 // If read barriers are enabled, emit read barriers other than
5292 // Baker's using a slow path (and also unpoison the loaded
5293 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005294 if (index.IsConstant()) {
5295 uint32_t offset =
5296 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005297 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5298 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005299 codegen_->MaybeGenerateReadBarrierSlow(
5300 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5301 }
5302 }
5303 break;
5304 }
5305
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005306 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005307 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005308 __ movl(out_loc.AsRegisterPairLow<Register>(),
5309 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5310 codegen_->MaybeRecordImplicitNullCheck(instruction);
5311 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5312 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005313 break;
5314 }
5315
Mark Mendell7c8d0092015-01-26 11:21:33 -05005316 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005317 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005318 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005319 break;
5320 }
5321
5322 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005323 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005324 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005325 break;
5326 }
5327
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005328 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005329 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005330 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005331 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005332
Roland Levillain7c1559a2015-12-15 10:55:36 +00005333 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5334 // Potential implicit null checks, in the case of reference or
5335 // long arrays, are handled in the previous switch statement.
5336 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005337 codegen_->MaybeRecordImplicitNullCheck(instruction);
5338 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005339}
5340
5341void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005342 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005343
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005344 bool needs_write_barrier =
5345 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005346 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005347
Nicolas Geoffray39468442014-09-02 15:17:15 +01005348 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5349 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005350 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005351 LocationSummary::kCallOnSlowPath :
5352 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005353
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005354 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5355 || (value_type == Primitive::kPrimByte);
5356 // We need the inputs to be different than the output in case of long operation.
5357 // In case of a byte operation, the register allocator does not support multiple
5358 // inputs that die at entry with one in a specific register.
5359 locations->SetInAt(0, Location::RequiresRegister());
5360 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5361 if (is_byte_type) {
5362 // Ensure the value is in a byte register.
5363 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5364 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005365 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005366 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005367 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5368 }
5369 if (needs_write_barrier) {
5370 // Temporary registers for the write barrier.
5371 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5372 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005373 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005375}
5376
5377void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5378 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005379 Location array_loc = locations->InAt(0);
5380 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005381 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005382 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005383 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005384 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5385 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5386 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005387 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005388 bool needs_write_barrier =
5389 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390
5391 switch (value_type) {
5392 case Primitive::kPrimBoolean:
5393 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005394 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005395 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005396 if (value.IsRegister()) {
5397 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005398 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005399 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005400 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005401 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005402 break;
5403 }
5404
5405 case Primitive::kPrimShort:
5406 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005407 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005408 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005409 if (value.IsRegister()) {
5410 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005411 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005412 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005413 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005414 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005415 break;
5416 }
5417
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005418 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005419 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005420 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005421
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005422 if (!value.IsRegister()) {
5423 // Just setting null.
5424 DCHECK(instruction->InputAt(2)->IsNullConstant());
5425 DCHECK(value.IsConstant()) << value;
5426 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005427 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005428 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005429 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005430 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005431 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005432
5433 DCHECK(needs_write_barrier);
5434 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005435 // We cannot use a NearLabel for `done`, as its range may be too
5436 // short when Baker read barriers are enabled.
5437 Label done;
5438 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005439 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005440 Location temp_loc = locations->GetTemp(0);
5441 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005442 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005443 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5444 codegen_->AddSlowPath(slow_path);
5445 if (instruction->GetValueCanBeNull()) {
5446 __ testl(register_value, register_value);
5447 __ j(kNotEqual, &not_null);
5448 __ movl(address, Immediate(0));
5449 codegen_->MaybeRecordImplicitNullCheck(instruction);
5450 __ jmp(&done);
5451 __ Bind(&not_null);
5452 }
5453
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005454 // Note that when Baker read barriers are enabled, the type
5455 // checks are performed without read barriers. This is fine,
5456 // even in the case where a class object is in the from-space
5457 // after the flip, as a comparison involving such a type would
5458 // not produce a false positive; it may of course produce a
5459 // false negative, in which case we would take the ArraySet
5460 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005461
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005462 // /* HeapReference<Class> */ temp = array->klass_
5463 __ movl(temp, Address(array, class_offset));
5464 codegen_->MaybeRecordImplicitNullCheck(instruction);
5465 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005466
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005467 // /* HeapReference<Class> */ temp = temp->component_type_
5468 __ movl(temp, Address(temp, component_offset));
5469 // If heap poisoning is enabled, no need to unpoison `temp`
5470 // nor the object reference in `register_value->klass`, as
5471 // we are comparing two poisoned references.
5472 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005473
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005474 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5475 __ j(kEqual, &do_put);
5476 // If heap poisoning is enabled, the `temp` reference has
5477 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005478 __ MaybeUnpoisonHeapReference(temp);
5479
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005480 // If heap poisoning is enabled, no need to unpoison the
5481 // heap reference loaded below, as it is only used for a
5482 // comparison with null.
5483 __ cmpl(Address(temp, super_offset), Immediate(0));
5484 __ j(kNotEqual, slow_path->GetEntryLabel());
5485 __ Bind(&do_put);
5486 } else {
5487 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005488 }
5489 }
5490
5491 if (kPoisonHeapReferences) {
5492 __ movl(temp, register_value);
5493 __ PoisonHeapReference(temp);
5494 __ movl(address, temp);
5495 } else {
5496 __ movl(address, register_value);
5497 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005498 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005499 codegen_->MaybeRecordImplicitNullCheck(instruction);
5500 }
5501
5502 Register card = locations->GetTemp(1).AsRegister<Register>();
5503 codegen_->MarkGCCard(
5504 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5505 __ Bind(&done);
5506
5507 if (slow_path != nullptr) {
5508 __ Bind(slow_path->GetExitLabel());
5509 }
5510
5511 break;
5512 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005513
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005514 case Primitive::kPrimInt: {
5515 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005516 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005517 if (value.IsRegister()) {
5518 __ movl(address, value.AsRegister<Register>());
5519 } else {
5520 DCHECK(value.IsConstant()) << value;
5521 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5522 __ movl(address, Immediate(v));
5523 }
5524 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005525 break;
5526 }
5527
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005528 case Primitive::kPrimLong: {
5529 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005530 if (value.IsRegisterPair()) {
5531 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5532 value.AsRegisterPairLow<Register>());
5533 codegen_->MaybeRecordImplicitNullCheck(instruction);
5534 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5535 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005536 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005537 DCHECK(value.IsConstant());
5538 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5539 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5540 Immediate(Low32Bits(val)));
5541 codegen_->MaybeRecordImplicitNullCheck(instruction);
5542 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5543 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005544 }
5545 break;
5546 }
5547
Mark Mendell7c8d0092015-01-26 11:21:33 -05005548 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005549 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005550 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005551 if (value.IsFpuRegister()) {
5552 __ movss(address, value.AsFpuRegister<XmmRegister>());
5553 } else {
5554 DCHECK(value.IsConstant());
5555 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5556 __ movl(address, Immediate(v));
5557 }
5558 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005559 break;
5560 }
5561
5562 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005563 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005564 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005565 if (value.IsFpuRegister()) {
5566 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5567 } else {
5568 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005569 Address address_hi =
5570 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005571 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5572 __ movl(address, Immediate(Low32Bits(v)));
5573 codegen_->MaybeRecordImplicitNullCheck(instruction);
5574 __ movl(address_hi, Immediate(High32Bits(v)));
5575 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005576 break;
5577 }
5578
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005579 case Primitive::kPrimVoid:
5580 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005581 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005582 }
5583}
5584
5585void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5586 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005587 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005588 if (!instruction->IsEmittedAtUseSite()) {
5589 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5590 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005591}
5592
5593void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005594 if (instruction->IsEmittedAtUseSite()) {
5595 return;
5596 }
5597
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005598 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005599 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005600 Register obj = locations->InAt(0).AsRegister<Register>();
5601 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005602 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005603 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005604 // Mask out most significant bit in case the array is String's array of char.
5605 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005606 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005607 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005608}
5609
5610void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005611 RegisterSet caller_saves = RegisterSet::Empty();
5612 InvokeRuntimeCallingConvention calling_convention;
5613 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5614 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5615 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005616 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005617 HInstruction* length = instruction->InputAt(1);
5618 if (!length->IsEmittedAtUseSite()) {
5619 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5620 }
jessicahandojo4877b792016-09-08 19:49:13 -07005621 // Need register to see array's length.
5622 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5623 locations->AddTemp(Location::RequiresRegister());
5624 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005625}
5626
5627void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005628 const bool is_string_compressed_char_at =
5629 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005630 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005631 Location index_loc = locations->InAt(0);
5632 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005633 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005634 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005635
Mark Mendell99dbd682015-04-22 16:18:52 -04005636 if (length_loc.IsConstant()) {
5637 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5638 if (index_loc.IsConstant()) {
5639 // BCE will remove the bounds check if we are guarenteed to pass.
5640 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5641 if (index < 0 || index >= length) {
5642 codegen_->AddSlowPath(slow_path);
5643 __ jmp(slow_path->GetEntryLabel());
5644 } else {
5645 // Some optimization after BCE may have generated this, and we should not
5646 // generate a bounds check if it is a valid range.
5647 }
5648 return;
5649 }
5650
5651 // We have to reverse the jump condition because the length is the constant.
5652 Register index_reg = index_loc.AsRegister<Register>();
5653 __ cmpl(index_reg, Immediate(length));
5654 codegen_->AddSlowPath(slow_path);
5655 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005656 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005657 HInstruction* array_length = instruction->InputAt(1);
5658 if (array_length->IsEmittedAtUseSite()) {
5659 // Address the length field in the array.
5660 DCHECK(array_length->IsArrayLength());
5661 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5662 Location array_loc = array_length->GetLocations()->InAt(0);
5663 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005664 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005665 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5666 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005667 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5668 __ movl(length_reg, array_len);
5669 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005670 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005671 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005672 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005673 // Checking bounds for general case:
5674 // Array of char or string's array with feature compression off.
5675 if (index_loc.IsConstant()) {
5676 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5677 __ cmpl(array_len, Immediate(value));
5678 } else {
5679 __ cmpl(array_len, index_loc.AsRegister<Register>());
5680 }
5681 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005682 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005683 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005684 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005685 }
5686 codegen_->AddSlowPath(slow_path);
5687 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005688 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005689}
5690
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005691void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005692 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005693}
5694
5695void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005696 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5697}
5698
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005699void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005700 LocationSummary* locations =
5701 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005702 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005703}
5704
5705void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005706 HBasicBlock* block = instruction->GetBlock();
5707 if (block->GetLoopInformation() != nullptr) {
5708 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5709 // The back edge will generate the suspend check.
5710 return;
5711 }
5712 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5713 // The goto will generate the suspend check.
5714 return;
5715 }
5716 GenerateSuspendCheck(instruction, nullptr);
5717}
5718
5719void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5720 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005721 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005722 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5723 if (slow_path == nullptr) {
5724 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5725 instruction->SetSlowPath(slow_path);
5726 codegen_->AddSlowPath(slow_path);
5727 if (successor != nullptr) {
5728 DCHECK(successor->IsLoopHeader());
5729 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5730 }
5731 } else {
5732 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5733 }
5734
Andreas Gampe542451c2016-07-26 09:02:02 -07005735 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005736 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005737 if (successor == nullptr) {
5738 __ j(kNotEqual, slow_path->GetEntryLabel());
5739 __ Bind(slow_path->GetReturnLabel());
5740 } else {
5741 __ j(kEqual, codegen_->GetLabelOf(successor));
5742 __ jmp(slow_path->GetEntryLabel());
5743 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005744}
5745
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005746X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5747 return codegen_->GetAssembler();
5748}
5749
Mark Mendell7c8d0092015-01-26 11:21:33 -05005750void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005751 ScratchRegisterScope ensure_scratch(
5752 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5753 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5754 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5755 __ movl(temp_reg, Address(ESP, src + stack_offset));
5756 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005757}
5758
5759void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005760 ScratchRegisterScope ensure_scratch(
5761 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5762 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5763 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5764 __ movl(temp_reg, Address(ESP, src + stack_offset));
5765 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5766 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5767 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005768}
5769
5770void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005771 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005772 Location source = move->GetSource();
5773 Location destination = move->GetDestination();
5774
5775 if (source.IsRegister()) {
5776 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005777 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005778 } else if (destination.IsFpuRegister()) {
5779 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005780 } else {
5781 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005782 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005783 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005784 } else if (source.IsRegisterPair()) {
5785 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5786 // Create stack space for 2 elements.
5787 __ subl(ESP, Immediate(2 * elem_size));
5788 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5789 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5790 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5791 // And remove the temporary stack space we allocated.
5792 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005793 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005794 if (destination.IsRegister()) {
5795 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5796 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005797 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005798 } else if (destination.IsRegisterPair()) {
5799 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5800 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5801 __ psrlq(src_reg, Immediate(32));
5802 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005803 } else if (destination.IsStackSlot()) {
5804 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5805 } else {
5806 DCHECK(destination.IsDoubleStackSlot());
5807 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5808 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005809 } else if (source.IsStackSlot()) {
5810 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005811 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005812 } else if (destination.IsFpuRegister()) {
5813 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005814 } else {
5815 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005816 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5817 }
5818 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005819 if (destination.IsRegisterPair()) {
5820 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5821 __ movl(destination.AsRegisterPairHigh<Register>(),
5822 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5823 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005824 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5825 } else {
5826 DCHECK(destination.IsDoubleStackSlot()) << destination;
5827 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005828 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005829 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005830 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005831 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005832 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005833 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005834 if (value == 0) {
5835 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5836 } else {
5837 __ movl(destination.AsRegister<Register>(), Immediate(value));
5838 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005839 } else {
5840 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005841 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005842 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005843 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005844 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005845 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005846 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005847 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005848 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5849 if (value == 0) {
5850 // Easy handling of 0.0.
5851 __ xorps(dest, dest);
5852 } else {
5853 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005854 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5855 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5856 __ movl(temp, Immediate(value));
5857 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005858 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005859 } else {
5860 DCHECK(destination.IsStackSlot()) << destination;
5861 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5862 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005863 } else if (constant->IsLongConstant()) {
5864 int64_t value = constant->AsLongConstant()->GetValue();
5865 int32_t low_value = Low32Bits(value);
5866 int32_t high_value = High32Bits(value);
5867 Immediate low(low_value);
5868 Immediate high(high_value);
5869 if (destination.IsDoubleStackSlot()) {
5870 __ movl(Address(ESP, destination.GetStackIndex()), low);
5871 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5872 } else {
5873 __ movl(destination.AsRegisterPairLow<Register>(), low);
5874 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5875 }
5876 } else {
5877 DCHECK(constant->IsDoubleConstant());
5878 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005879 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005880 int32_t low_value = Low32Bits(value);
5881 int32_t high_value = High32Bits(value);
5882 Immediate low(low_value);
5883 Immediate high(high_value);
5884 if (destination.IsFpuRegister()) {
5885 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5886 if (value == 0) {
5887 // Easy handling of 0.0.
5888 __ xorpd(dest, dest);
5889 } else {
5890 __ pushl(high);
5891 __ pushl(low);
5892 __ movsd(dest, Address(ESP, 0));
5893 __ addl(ESP, Immediate(8));
5894 }
5895 } else {
5896 DCHECK(destination.IsDoubleStackSlot()) << destination;
5897 __ movl(Address(ESP, destination.GetStackIndex()), low);
5898 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5899 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005900 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005901 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005902 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005903 }
5904}
5905
Mark Mendella5c19ce2015-04-01 12:51:05 -04005906void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005907 Register suggested_scratch = reg == EAX ? EBX : EAX;
5908 ScratchRegisterScope ensure_scratch(
5909 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5910
5911 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5912 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5913 __ movl(Address(ESP, mem + stack_offset), reg);
5914 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005915}
5916
Mark Mendell7c8d0092015-01-26 11:21:33 -05005917void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005918 ScratchRegisterScope ensure_scratch(
5919 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5920
5921 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5922 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5923 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5924 __ movss(Address(ESP, mem + stack_offset), reg);
5925 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005926}
5927
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005928void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005929 ScratchRegisterScope ensure_scratch1(
5930 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005931
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005932 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5933 ScratchRegisterScope ensure_scratch2(
5934 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005935
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005936 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5937 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5938 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5939 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5940 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5941 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005942}
5943
5944void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005945 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005946 Location source = move->GetSource();
5947 Location destination = move->GetDestination();
5948
5949 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005950 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5951 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5952 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5953 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5954 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005955 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005956 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005957 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005958 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005959 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5960 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005961 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5962 // Use XOR Swap algorithm to avoid a temporary.
5963 DCHECK_NE(source.reg(), destination.reg());
5964 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5965 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5966 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5967 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5968 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5969 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5970 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005971 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5972 // Take advantage of the 16 bytes in the XMM register.
5973 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5974 Address stack(ESP, destination.GetStackIndex());
5975 // Load the double into the high doubleword.
5976 __ movhpd(reg, stack);
5977
5978 // Store the low double into the destination.
5979 __ movsd(stack, reg);
5980
5981 // Move the high double to the low double.
5982 __ psrldq(reg, Immediate(8));
5983 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5984 // Take advantage of the 16 bytes in the XMM register.
5985 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5986 Address stack(ESP, source.GetStackIndex());
5987 // Load the double into the high doubleword.
5988 __ movhpd(reg, stack);
5989
5990 // Store the low double into the destination.
5991 __ movsd(stack, reg);
5992
5993 // Move the high double to the low double.
5994 __ psrldq(reg, Immediate(8));
5995 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5996 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5997 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005998 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005999 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006000 }
6001}
6002
6003void ParallelMoveResolverX86::SpillScratch(int reg) {
6004 __ pushl(static_cast<Register>(reg));
6005}
6006
6007void ParallelMoveResolverX86::RestoreScratch(int reg) {
6008 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006009}
6010
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006011HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6012 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006013 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006014 case HLoadClass::LoadKind::kInvalid:
6015 LOG(FATAL) << "UNREACHABLE";
6016 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006017 case HLoadClass::LoadKind::kReferrersClass:
6018 break;
6019 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6020 DCHECK(!GetCompilerOptions().GetCompilePic());
6021 break;
6022 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6023 DCHECK(GetCompilerOptions().GetCompilePic());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006024 FALLTHROUGH_INTENDED;
6025 case HLoadClass::LoadKind::kBssEntry:
6026 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006027 break;
6028 case HLoadClass::LoadKind::kBootImageAddress:
6029 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006030 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006031 DCHECK(Runtime::Current()->UseJitCompilation());
6032 break;
6033 case HLoadClass::LoadKind::kDexCacheViaMethod:
6034 break;
6035 }
6036 return desired_class_load_kind;
6037}
6038
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006039void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006040 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6041 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006042 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006043 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006044 cls,
6045 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006046 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006047 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006048 return;
6049 }
Vladimir Marko41559982017-01-06 14:04:23 +00006050 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006051
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006052 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6053 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006054 ? LocationSummary::kCallOnSlowPath
6055 : LocationSummary::kNoCall;
6056 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006057 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006058 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006059 }
6060
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006061 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006062 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6063 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006064 locations->SetInAt(0, Location::RequiresRegister());
6065 }
6066 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006067 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6068 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6069 // Rely on the type resolution and/or initialization to save everything.
6070 RegisterSet caller_saves = RegisterSet::Empty();
6071 InvokeRuntimeCallingConvention calling_convention;
6072 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6073 locations->SetCustomSlowPathCallerSaves(caller_saves);
6074 } else {
6075 // For non-Baker read barrier we have a temp-clobbering call.
6076 }
6077 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006078}
6079
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006080Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6081 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006082 Handle<mirror::Class> handle) {
6083 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
6084 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006085 // Add a patch entry and return the label.
6086 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6087 PatchInfo<Label>* info = &jit_class_patches_.back();
6088 return &info->label;
6089}
6090
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006091// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6092// move.
6093void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006094 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6095 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6096 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006097 return;
6098 }
Vladimir Marko41559982017-01-06 14:04:23 +00006099 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006100
Vladimir Marko41559982017-01-06 14:04:23 +00006101 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006102 Location out_loc = locations->Out();
6103 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006105 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006106 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6107 ? kWithoutReadBarrier
6108 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006109 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006110 case HLoadClass::LoadKind::kReferrersClass: {
6111 DCHECK(!cls->CanCallRuntime());
6112 DCHECK(!cls->MustGenerateClinitCheck());
6113 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6114 Register current_method = locations->InAt(0).AsRegister<Register>();
6115 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006116 cls,
6117 out_loc,
6118 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006119 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006120 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006121 break;
6122 }
6123 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006124 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006125 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006126 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006127 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006128 break;
6129 }
6130 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006131 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006132 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006133 Register method_address = locations->InAt(0).AsRegister<Register>();
6134 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006135 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006136 break;
6137 }
6138 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006139 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006140 uint32_t address = dchecked_integral_cast<uint32_t>(
6141 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6142 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006143 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006144 break;
6145 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006146 case HLoadClass::LoadKind::kBssEntry: {
6147 Register method_address = locations->InAt(0).AsRegister<Register>();
6148 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6149 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6150 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6151 generate_null_check = true;
6152 break;
6153 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006154 case HLoadClass::LoadKind::kJitTableAddress: {
6155 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6156 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006157 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006158 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006159 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006160 break;
6161 }
Vladimir Marko41559982017-01-06 14:04:23 +00006162 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006163 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006164 LOG(FATAL) << "UNREACHABLE";
6165 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006166 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006167
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006168 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6169 DCHECK(cls->CanCallRuntime());
6170 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6171 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6172 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006173
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006174 if (generate_null_check) {
6175 __ testl(out, out);
6176 __ j(kEqual, slow_path->GetEntryLabel());
6177 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006178
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006179 if (cls->MustGenerateClinitCheck()) {
6180 GenerateClassInitializationCheck(slow_path, out);
6181 } else {
6182 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006183 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006184 }
6185}
6186
6187void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6188 LocationSummary* locations =
6189 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6190 locations->SetInAt(0, Location::RequiresRegister());
6191 if (check->HasUses()) {
6192 locations->SetOut(Location::SameAsFirstInput());
6193 }
6194}
6195
6196void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006197 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006198 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006199 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006200 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006201 GenerateClassInitializationCheck(slow_path,
6202 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006203}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006204
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006205void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006206 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006207 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6208 Immediate(mirror::Class::kStatusInitialized));
6209 __ j(kLess, slow_path->GetEntryLabel());
6210 __ Bind(slow_path->GetExitLabel());
6211 // No need for memory fence, thanks to the X86 memory model.
6212}
6213
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006214HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6215 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006216 switch (desired_string_load_kind) {
6217 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6218 DCHECK(!GetCompilerOptions().GetCompilePic());
6219 break;
6220 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6221 DCHECK(GetCompilerOptions().GetCompilePic());
6222 FALLTHROUGH_INTENDED;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006223 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006224 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006225 break;
6226 case HLoadString::LoadKind::kBootImageAddress:
6227 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006228 case HLoadString::LoadKind::kJitTableAddress:
6229 DCHECK(Runtime::Current()->UseJitCompilation());
6230 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006231 case HLoadString::LoadKind::kDexCacheViaMethod:
6232 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006233 }
6234 return desired_string_load_kind;
6235}
6236
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006237void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006238 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006239 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006240 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006241 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006242 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006243 locations->SetInAt(0, Location::RequiresRegister());
6244 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006245 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6246 locations->SetOut(Location::RegisterLocation(EAX));
6247 } else {
6248 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006249 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6250 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006251 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006252 RegisterSet caller_saves = RegisterSet::Empty();
6253 InvokeRuntimeCallingConvention calling_convention;
6254 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6255 locations->SetCustomSlowPathCallerSaves(caller_saves);
6256 } else {
6257 // For non-Baker read barrier we have a temp-clobbering call.
6258 }
6259 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006260 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006261}
6262
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006263Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006264 dex::StringIndex dex_index,
6265 Handle<mirror::String> handle) {
6266 jit_string_roots_.Overwrite(
6267 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006268 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006269 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006270 PatchInfo<Label>* info = &jit_string_patches_.back();
6271 return &info->label;
6272}
6273
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006274// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6275// move.
6276void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006277 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006278 Location out_loc = locations->Out();
6279 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006280
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006281 switch (load->GetLoadKind()) {
6282 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006283 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006284 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006285 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006286 return; // No dex cache slow path.
6287 }
6288 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006289 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006290 Register method_address = locations->InAt(0).AsRegister<Register>();
6291 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006292 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006293 return; // No dex cache slow path.
6294 }
6295 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006296 uint32_t address = dchecked_integral_cast<uint32_t>(
6297 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6298 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006299 __ movl(out, Immediate(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006300 return; // No dex cache slow path.
6301 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006302 case HLoadString::LoadKind::kBssEntry: {
6303 Register method_address = locations->InAt(0).AsRegister<Register>();
6304 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6305 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006306 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006307 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006308 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6309 codegen_->AddSlowPath(slow_path);
6310 __ testl(out, out);
6311 __ j(kEqual, slow_path->GetEntryLabel());
6312 __ Bind(slow_path->GetExitLabel());
6313 return;
6314 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006315 case HLoadString::LoadKind::kJitTableAddress: {
6316 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6317 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006318 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006319 // /* GcRoot<mirror::String> */ out = *address
6320 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6321 return;
6322 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006323 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006324 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006325 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006326
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006327 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006328 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006329 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006330 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006331 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6332 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006333}
6334
David Brazdilcb1c0552015-08-04 16:22:25 +01006335static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006336 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006337}
6338
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006339void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6340 LocationSummary* locations =
6341 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6342 locations->SetOut(Location::RequiresRegister());
6343}
6344
6345void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006346 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6347}
6348
6349void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6350 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6351}
6352
6353void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6354 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006355}
6356
6357void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6358 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006359 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006360 InvokeRuntimeCallingConvention calling_convention;
6361 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6362}
6363
6364void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006365 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006366 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006367}
6368
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006369// Temp is used for read barrier.
6370static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6371 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006372 !kUseBakerReadBarrier &&
6373 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006374 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006375 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6376 return 1;
6377 }
6378 return 0;
6379}
6380
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006381// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006382// interface pointer, one for loading the current interface.
6383// The other checks have one temp for loading the object's class.
6384static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6385 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6386 return 2;
6387 }
6388 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006389}
6390
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006391void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006392 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006393 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006394 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006395 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006396 case TypeCheckKind::kExactCheck:
6397 case TypeCheckKind::kAbstractClassCheck:
6398 case TypeCheckKind::kClassHierarchyCheck:
6399 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400 call_kind =
6401 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006402 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006403 break;
6404 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006405 case TypeCheckKind::kUnresolvedCheck:
6406 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006407 call_kind = LocationSummary::kCallOnSlowPath;
6408 break;
6409 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006410
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006411 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006412 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006413 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006414 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006415 locations->SetInAt(0, Location::RequiresRegister());
6416 locations->SetInAt(1, Location::Any());
6417 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6418 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006419 // When read barriers are enabled, we need a temporary register for some cases.
6420 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006421}
6422
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006423void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006424 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006425 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006426 Location obj_loc = locations->InAt(0);
6427 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006428 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006429 Location out_loc = locations->Out();
6430 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006431 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6432 DCHECK_LE(num_temps, 1u);
6433 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006434 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006435 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6436 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6437 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006438 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006439 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006440
6441 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006442 // Avoid null check if we know obj is not null.
6443 if (instruction->MustDoNullCheck()) {
6444 __ testl(obj, obj);
6445 __ j(kEqual, &zero);
6446 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006447
Roland Levillain7c1559a2015-12-15 10:55:36 +00006448 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006449 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006450 // /* HeapReference<Class> */ out = obj->klass_
6451 GenerateReferenceLoadTwoRegisters(instruction,
6452 out_loc,
6453 obj_loc,
6454 class_offset,
6455 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006456 if (cls.IsRegister()) {
6457 __ cmpl(out, cls.AsRegister<Register>());
6458 } else {
6459 DCHECK(cls.IsStackSlot()) << cls;
6460 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6461 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006462
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006463 // Classes must be equal for the instanceof to succeed.
6464 __ j(kNotEqual, &zero);
6465 __ movl(out, Immediate(1));
6466 __ jmp(&done);
6467 break;
6468 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006469
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006470 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006471 // /* HeapReference<Class> */ out = obj->klass_
6472 GenerateReferenceLoadTwoRegisters(instruction,
6473 out_loc,
6474 obj_loc,
6475 class_offset,
6476 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006477 // If the class is abstract, we eagerly fetch the super class of the
6478 // object to avoid doing a comparison we know will fail.
6479 NearLabel loop;
6480 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006481 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006482 GenerateReferenceLoadOneRegister(instruction,
6483 out_loc,
6484 super_offset,
6485 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006486 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006487 __ testl(out, out);
6488 // If `out` is null, we use it for the result, and jump to `done`.
6489 __ j(kEqual, &done);
6490 if (cls.IsRegister()) {
6491 __ cmpl(out, cls.AsRegister<Register>());
6492 } else {
6493 DCHECK(cls.IsStackSlot()) << cls;
6494 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6495 }
6496 __ j(kNotEqual, &loop);
6497 __ movl(out, Immediate(1));
6498 if (zero.IsLinked()) {
6499 __ jmp(&done);
6500 }
6501 break;
6502 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006503
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006504 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006505 // /* HeapReference<Class> */ out = obj->klass_
6506 GenerateReferenceLoadTwoRegisters(instruction,
6507 out_loc,
6508 obj_loc,
6509 class_offset,
6510 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006511 // Walk over the class hierarchy to find a match.
6512 NearLabel loop, success;
6513 __ Bind(&loop);
6514 if (cls.IsRegister()) {
6515 __ cmpl(out, cls.AsRegister<Register>());
6516 } else {
6517 DCHECK(cls.IsStackSlot()) << cls;
6518 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6519 }
6520 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006521 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006522 GenerateReferenceLoadOneRegister(instruction,
6523 out_loc,
6524 super_offset,
6525 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006526 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006527 __ testl(out, out);
6528 __ j(kNotEqual, &loop);
6529 // If `out` is null, we use it for the result, and jump to `done`.
6530 __ jmp(&done);
6531 __ Bind(&success);
6532 __ movl(out, Immediate(1));
6533 if (zero.IsLinked()) {
6534 __ jmp(&done);
6535 }
6536 break;
6537 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006538
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006539 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006540 // /* HeapReference<Class> */ out = obj->klass_
6541 GenerateReferenceLoadTwoRegisters(instruction,
6542 out_loc,
6543 obj_loc,
6544 class_offset,
6545 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006546 // Do an exact check.
6547 NearLabel exact_check;
6548 if (cls.IsRegister()) {
6549 __ cmpl(out, cls.AsRegister<Register>());
6550 } else {
6551 DCHECK(cls.IsStackSlot()) << cls;
6552 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6553 }
6554 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006555 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006556 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006557 GenerateReferenceLoadOneRegister(instruction,
6558 out_loc,
6559 component_offset,
6560 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006561 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006562 __ testl(out, out);
6563 // If `out` is null, we use it for the result, and jump to `done`.
6564 __ j(kEqual, &done);
6565 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6566 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006567 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006568 __ movl(out, Immediate(1));
6569 __ jmp(&done);
6570 break;
6571 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006572
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006573 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006574 // No read barrier since the slow path will retry upon failure.
6575 // /* HeapReference<Class> */ out = obj->klass_
6576 GenerateReferenceLoadTwoRegisters(instruction,
6577 out_loc,
6578 obj_loc,
6579 class_offset,
6580 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006581 if (cls.IsRegister()) {
6582 __ cmpl(out, cls.AsRegister<Register>());
6583 } else {
6584 DCHECK(cls.IsStackSlot()) << cls;
6585 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6586 }
6587 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006588 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6589 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006590 codegen_->AddSlowPath(slow_path);
6591 __ j(kNotEqual, slow_path->GetEntryLabel());
6592 __ movl(out, Immediate(1));
6593 if (zero.IsLinked()) {
6594 __ jmp(&done);
6595 }
6596 break;
6597 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006598
Calin Juravle98893e12015-10-02 21:05:03 +01006599 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006600 case TypeCheckKind::kInterfaceCheck: {
6601 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006602 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006603 // cases.
6604 //
6605 // We cannot directly call the InstanceofNonTrivial runtime
6606 // entry point without resorting to a type checking slow path
6607 // here (i.e. by calling InvokeRuntime directly), as it would
6608 // require to assign fixed registers for the inputs of this
6609 // HInstanceOf instruction (following the runtime calling
6610 // convention), which might be cluttered by the potential first
6611 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006612 //
6613 // TODO: Introduce a new runtime entry point taking the object
6614 // to test (instead of its class) as argument, and let it deal
6615 // with the read barrier issues. This will let us refactor this
6616 // case of the `switch` code as it was previously (with a direct
6617 // call to the runtime not using a type checking slow path).
6618 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006619 DCHECK(locations->OnlyCallsOnSlowPath());
6620 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6621 /* is_fatal */ false);
6622 codegen_->AddSlowPath(slow_path);
6623 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006624 if (zero.IsLinked()) {
6625 __ jmp(&done);
6626 }
6627 break;
6628 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006629 }
6630
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006631 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006632 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006633 __ xorl(out, out);
6634 }
6635
6636 if (done.IsLinked()) {
6637 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006638 }
6639
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006640 if (slow_path != nullptr) {
6641 __ Bind(slow_path->GetExitLabel());
6642 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006643}
6644
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006645static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6646 switch (type_check_kind) {
6647 case TypeCheckKind::kExactCheck:
6648 case TypeCheckKind::kAbstractClassCheck:
6649 case TypeCheckKind::kClassHierarchyCheck:
6650 case TypeCheckKind::kArrayObjectCheck:
6651 return !throws_into_catch && !kEmitCompilerReadBarrier;
6652 case TypeCheckKind::kInterfaceCheck:
6653 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6654 case TypeCheckKind::kArrayCheck:
6655 case TypeCheckKind::kUnresolvedCheck:
6656 return false;
6657 }
6658 LOG(FATAL) << "Unreachable";
6659 UNREACHABLE();
6660}
6661
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006662void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006663 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006664 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006665 LocationSummary::CallKind call_kind =
6666 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6667 ? LocationSummary::kNoCall
6668 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006669 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6670 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006671 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6672 // Require a register for the interface check since there is a loop that compares the class to
6673 // a memory address.
6674 locations->SetInAt(1, Location::RequiresRegister());
6675 } else {
6676 locations->SetInAt(1, Location::Any());
6677 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006678 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6679 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006680 // When read barriers are enabled, we need an additional temporary register for some cases.
6681 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6682}
6683
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006684void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006685 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006686 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006687 Location obj_loc = locations->InAt(0);
6688 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006689 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006690 Location temp_loc = locations->GetTemp(0);
6691 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006692 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6693 DCHECK_GE(num_temps, 1u);
6694 DCHECK_LE(num_temps, 2u);
6695 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6696 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6697 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6698 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6699 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6700 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6701 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6702 const uint32_t object_array_data_offset =
6703 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006704
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006705 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6706 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6707 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006708 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006709 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6710
Roland Levillain0d5a2812015-11-13 10:07:31 +00006711 SlowPathCode* type_check_slow_path =
6712 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6713 is_type_check_slow_path_fatal);
6714 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006715
Roland Levillain0d5a2812015-11-13 10:07:31 +00006716 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006717 // Avoid null check if we know obj is not null.
6718 if (instruction->MustDoNullCheck()) {
6719 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006720 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006721 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006722
Roland Levillain0d5a2812015-11-13 10:07:31 +00006723 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006724 case TypeCheckKind::kExactCheck:
6725 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006726 // /* HeapReference<Class> */ temp = obj->klass_
6727 GenerateReferenceLoadTwoRegisters(instruction,
6728 temp_loc,
6729 obj_loc,
6730 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006731 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006732
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006733 if (cls.IsRegister()) {
6734 __ cmpl(temp, cls.AsRegister<Register>());
6735 } else {
6736 DCHECK(cls.IsStackSlot()) << cls;
6737 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6738 }
6739 // Jump to slow path for throwing the exception or doing a
6740 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006741 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006742 break;
6743 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006744
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006745 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006746 // /* HeapReference<Class> */ temp = obj->klass_
6747 GenerateReferenceLoadTwoRegisters(instruction,
6748 temp_loc,
6749 obj_loc,
6750 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006751 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006752
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006753 // If the class is abstract, we eagerly fetch the super class of the
6754 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006755 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006756 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006757 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006758 GenerateReferenceLoadOneRegister(instruction,
6759 temp_loc,
6760 super_offset,
6761 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006762 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006763
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006764 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6765 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006766 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006767 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006768
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006769 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006770 if (cls.IsRegister()) {
6771 __ cmpl(temp, cls.AsRegister<Register>());
6772 } else {
6773 DCHECK(cls.IsStackSlot()) << cls;
6774 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6775 }
6776 __ j(kNotEqual, &loop);
6777 break;
6778 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006779
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006780 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006781 // /* HeapReference<Class> */ temp = obj->klass_
6782 GenerateReferenceLoadTwoRegisters(instruction,
6783 temp_loc,
6784 obj_loc,
6785 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006786 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006787
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006788 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006789 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006790 __ Bind(&loop);
6791 if (cls.IsRegister()) {
6792 __ cmpl(temp, cls.AsRegister<Register>());
6793 } else {
6794 DCHECK(cls.IsStackSlot()) << cls;
6795 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6796 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006797 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006798
Roland Levillain0d5a2812015-11-13 10:07:31 +00006799 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006800 GenerateReferenceLoadOneRegister(instruction,
6801 temp_loc,
6802 super_offset,
6803 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006804 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006805
6806 // If the class reference currently in `temp` is not null, jump
6807 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006808 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006809 __ j(kNotZero, &loop);
6810 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006811 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006812 break;
6813 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006814
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006815 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006816 // /* HeapReference<Class> */ temp = obj->klass_
6817 GenerateReferenceLoadTwoRegisters(instruction,
6818 temp_loc,
6819 obj_loc,
6820 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006821 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006822
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006823 // Do an exact check.
6824 if (cls.IsRegister()) {
6825 __ cmpl(temp, cls.AsRegister<Register>());
6826 } else {
6827 DCHECK(cls.IsStackSlot()) << cls;
6828 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6829 }
6830 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006831
6832 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006833 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006834 GenerateReferenceLoadOneRegister(instruction,
6835 temp_loc,
6836 component_offset,
6837 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006838 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006839
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006840 // If the component type is null (i.e. the object not an array), jump to the slow path to
6841 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006842 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006843 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006844
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006845 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006846 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006847 break;
6848 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006849
Calin Juravle98893e12015-10-02 21:05:03 +01006850 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006851 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006852 // We cannot directly call the CheckCast runtime entry point
6853 // without resorting to a type checking slow path here (i.e. by
6854 // calling InvokeRuntime directly), as it would require to
6855 // assign fixed registers for the inputs of this HInstanceOf
6856 // instruction (following the runtime calling convention), which
6857 // might be cluttered by the potential first read barrier
6858 // emission at the beginning of this method.
6859 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006860 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006861
6862 case TypeCheckKind::kInterfaceCheck: {
6863 // Fast path for the interface check. Since we compare with a memory location in the inner
6864 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6865 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006866 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006867 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006868 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6869 // doing this.
6870 // /* HeapReference<Class> */ temp = obj->klass_
6871 GenerateReferenceLoadTwoRegisters(instruction,
6872 temp_loc,
6873 obj_loc,
6874 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006875 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006876
6877 // /* HeapReference<Class> */ temp = temp->iftable_
6878 GenerateReferenceLoadTwoRegisters(instruction,
6879 temp_loc,
6880 temp_loc,
6881 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006882 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006883 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006884 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006885 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006886 NearLabel start_loop;
6887 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006888 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006889 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006890 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6891 // Go to next interface if the classes do not match.
6892 __ cmpl(cls.AsRegister<Register>(),
6893 CodeGeneratorX86::ArrayAddress(temp,
6894 maybe_temp2_loc,
6895 TIMES_4,
6896 object_array_data_offset));
6897 __ j(kNotEqual, &start_loop);
6898 } else {
6899 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006900 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006901 break;
6902 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006903 }
6904 __ Bind(&done);
6905
Roland Levillain0d5a2812015-11-13 10:07:31 +00006906 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006907}
6908
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006909void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6910 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006911 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006912 InvokeRuntimeCallingConvention calling_convention;
6913 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6914}
6915
6916void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006917 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6918 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006919 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006920 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006921 if (instruction->IsEnter()) {
6922 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6923 } else {
6924 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6925 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006926}
6927
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006928void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6929void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6930void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6931
6932void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6933 LocationSummary* locations =
6934 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6935 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6936 || instruction->GetResultType() == Primitive::kPrimLong);
6937 locations->SetInAt(0, Location::RequiresRegister());
6938 locations->SetInAt(1, Location::Any());
6939 locations->SetOut(Location::SameAsFirstInput());
6940}
6941
6942void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6943 HandleBitwiseOperation(instruction);
6944}
6945
6946void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6947 HandleBitwiseOperation(instruction);
6948}
6949
6950void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6951 HandleBitwiseOperation(instruction);
6952}
6953
6954void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6955 LocationSummary* locations = instruction->GetLocations();
6956 Location first = locations->InAt(0);
6957 Location second = locations->InAt(1);
6958 DCHECK(first.Equals(locations->Out()));
6959
6960 if (instruction->GetResultType() == Primitive::kPrimInt) {
6961 if (second.IsRegister()) {
6962 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006963 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006964 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006965 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006966 } else {
6967 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006968 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006969 }
6970 } else if (second.IsConstant()) {
6971 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006972 __ andl(first.AsRegister<Register>(),
6973 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006974 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006975 __ orl(first.AsRegister<Register>(),
6976 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006977 } else {
6978 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006979 __ xorl(first.AsRegister<Register>(),
6980 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006981 }
6982 } else {
6983 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006984 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006985 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006986 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006987 } else {
6988 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006989 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006990 }
6991 }
6992 } else {
6993 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6994 if (second.IsRegisterPair()) {
6995 if (instruction->IsAnd()) {
6996 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6997 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6998 } else if (instruction->IsOr()) {
6999 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7000 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7001 } else {
7002 DCHECK(instruction->IsXor());
7003 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7004 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7005 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007006 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007007 if (instruction->IsAnd()) {
7008 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7009 __ andl(first.AsRegisterPairHigh<Register>(),
7010 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7011 } else if (instruction->IsOr()) {
7012 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7013 __ orl(first.AsRegisterPairHigh<Register>(),
7014 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7015 } else {
7016 DCHECK(instruction->IsXor());
7017 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7018 __ xorl(first.AsRegisterPairHigh<Register>(),
7019 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7020 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007021 } else {
7022 DCHECK(second.IsConstant()) << second;
7023 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007024 int32_t low_value = Low32Bits(value);
7025 int32_t high_value = High32Bits(value);
7026 Immediate low(low_value);
7027 Immediate high(high_value);
7028 Register first_low = first.AsRegisterPairLow<Register>();
7029 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007030 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007031 if (low_value == 0) {
7032 __ xorl(first_low, first_low);
7033 } else if (low_value != -1) {
7034 __ andl(first_low, low);
7035 }
7036 if (high_value == 0) {
7037 __ xorl(first_high, first_high);
7038 } else if (high_value != -1) {
7039 __ andl(first_high, high);
7040 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007041 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007042 if (low_value != 0) {
7043 __ orl(first_low, low);
7044 }
7045 if (high_value != 0) {
7046 __ orl(first_high, high);
7047 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007048 } else {
7049 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007050 if (low_value != 0) {
7051 __ xorl(first_low, low);
7052 }
7053 if (high_value != 0) {
7054 __ xorl(first_high, high);
7055 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007056 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007057 }
7058 }
7059}
7060
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007061void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7062 HInstruction* instruction,
7063 Location out,
7064 uint32_t offset,
7065 Location maybe_temp,
7066 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007067 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007068 if (read_barrier_option == kWithReadBarrier) {
7069 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007070 if (kUseBakerReadBarrier) {
7071 // Load with fast path based Baker's read barrier.
7072 // /* HeapReference<Object> */ out = *(out + offset)
7073 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007074 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007075 } else {
7076 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007077 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007078 // in the following move operation, as we will need it for the
7079 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007080 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007081 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007082 // /* HeapReference<Object> */ out = *(out + offset)
7083 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007084 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007085 }
7086 } else {
7087 // Plain load with no read barrier.
7088 // /* HeapReference<Object> */ out = *(out + offset)
7089 __ movl(out_reg, Address(out_reg, offset));
7090 __ MaybeUnpoisonHeapReference(out_reg);
7091 }
7092}
7093
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007094void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7095 HInstruction* instruction,
7096 Location out,
7097 Location obj,
7098 uint32_t offset,
7099 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007100 Register out_reg = out.AsRegister<Register>();
7101 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007102 if (read_barrier_option == kWithReadBarrier) {
7103 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007104 if (kUseBakerReadBarrier) {
7105 // Load with fast path based Baker's read barrier.
7106 // /* HeapReference<Object> */ out = *(obj + offset)
7107 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007108 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007109 } else {
7110 // Load with slow path based read barrier.
7111 // /* HeapReference<Object> */ out = *(obj + offset)
7112 __ movl(out_reg, Address(obj_reg, offset));
7113 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7114 }
7115 } else {
7116 // Plain load with no read barrier.
7117 // /* HeapReference<Object> */ out = *(obj + offset)
7118 __ movl(out_reg, Address(obj_reg, offset));
7119 __ MaybeUnpoisonHeapReference(out_reg);
7120 }
7121}
7122
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007123void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7124 HInstruction* instruction,
7125 Location root,
7126 const Address& address,
7127 Label* fixup_label,
7128 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007129 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007130 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007131 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007132 if (kUseBakerReadBarrier) {
7133 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7134 // Baker's read barrier are used:
7135 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007136 // root = obj.field;
7137 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7138 // if (temp != null) {
7139 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007140 // }
7141
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007142 // /* GcRoot<mirror::Object> */ root = *address
7143 __ movl(root_reg, address);
7144 if (fixup_label != nullptr) {
7145 __ Bind(fixup_label);
7146 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007147 static_assert(
7148 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7149 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7150 "have different sizes.");
7151 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7152 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7153 "have different sizes.");
7154
Vladimir Marko953437b2016-08-24 08:30:46 +00007155 // Slow path marking the GC root `root`.
7156 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007157 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007158 codegen_->AddSlowPath(slow_path);
7159
Roland Levillaind966ce72017-02-09 16:20:14 +00007160 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7161 const int32_t entry_point_offset =
7162 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
7163 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7164 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007165 __ j(kNotEqual, slow_path->GetEntryLabel());
7166 __ Bind(slow_path->GetExitLabel());
7167 } else {
7168 // GC root loaded through a slow path for read barriers other
7169 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007170 // /* GcRoot<mirror::Object>* */ root = address
7171 __ leal(root_reg, address);
7172 if (fixup_label != nullptr) {
7173 __ Bind(fixup_label);
7174 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007175 // /* mirror::Object* */ root = root->Read()
7176 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7177 }
7178 } else {
7179 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007180 // /* GcRoot<mirror::Object> */ root = *address
7181 __ movl(root_reg, address);
7182 if (fixup_label != nullptr) {
7183 __ Bind(fixup_label);
7184 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007185 // Note that GC roots are not affected by heap poisoning, thus we
7186 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007187 }
7188}
7189
7190void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7191 Location ref,
7192 Register obj,
7193 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007194 bool needs_null_check) {
7195 DCHECK(kEmitCompilerReadBarrier);
7196 DCHECK(kUseBakerReadBarrier);
7197
7198 // /* HeapReference<Object> */ ref = *(obj + offset)
7199 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007200 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007201}
7202
7203void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7204 Location ref,
7205 Register obj,
7206 uint32_t data_offset,
7207 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007208 bool needs_null_check) {
7209 DCHECK(kEmitCompilerReadBarrier);
7210 DCHECK(kUseBakerReadBarrier);
7211
Roland Levillain3d312422016-06-23 13:53:42 +01007212 static_assert(
7213 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7214 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007215 // /* HeapReference<Object> */ ref =
7216 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007217 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007218 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007219}
7220
7221void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7222 Location ref,
7223 Register obj,
7224 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007225 bool needs_null_check,
7226 bool always_update_field,
7227 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007228 DCHECK(kEmitCompilerReadBarrier);
7229 DCHECK(kUseBakerReadBarrier);
7230
7231 // In slow path based read barriers, the read barrier call is
7232 // inserted after the original load. However, in fast path based
7233 // Baker's read barriers, we need to perform the load of
7234 // mirror::Object::monitor_ *before* the original reference load.
7235 // This load-load ordering is required by the read barrier.
7236 // The fast path/slow path (for Baker's algorithm) should look like:
7237 //
7238 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7239 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7240 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007241 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007242 // if (is_gray) {
7243 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7244 // }
7245 //
7246 // Note: the original implementation in ReadBarrier::Barrier is
7247 // slightly more complex as:
7248 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007249 // the high-bits of rb_state, which are expected to be all zeroes
7250 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7251 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007252 // - it performs additional checks that we do not do here for
7253 // performance reasons.
7254
7255 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007256 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7257
Vladimir Marko953437b2016-08-24 08:30:46 +00007258 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007259 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7260 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007261 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7262 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7263 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7264
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007265 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007266 // ref = ReadBarrier::Mark(ref);
7267 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7268 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007269 if (needs_null_check) {
7270 MaybeRecordImplicitNullCheck(instruction);
7271 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007272
7273 // Load fence to prevent load-load reordering.
7274 // Note that this is a no-op, thanks to the x86 memory model.
7275 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7276
7277 // The actual reference load.
7278 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007279 __ movl(ref_reg, src); // Flags are unaffected.
7280
7281 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7282 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007283 SlowPathCode* slow_path;
7284 if (always_update_field) {
7285 DCHECK(temp != nullptr);
7286 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7287 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7288 } else {
7289 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7290 instruction, ref, /* unpoison_ref_before_marking */ true);
7291 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007292 AddSlowPath(slow_path);
7293
7294 // We have done the "if" of the gray bit check above, now branch based on the flags.
7295 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007296
7297 // Object* ref = ref_addr->AsMirrorPtr()
7298 __ MaybeUnpoisonHeapReference(ref_reg);
7299
Roland Levillain7c1559a2015-12-15 10:55:36 +00007300 __ Bind(slow_path->GetExitLabel());
7301}
7302
7303void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7304 Location out,
7305 Location ref,
7306 Location obj,
7307 uint32_t offset,
7308 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007309 DCHECK(kEmitCompilerReadBarrier);
7310
Roland Levillain7c1559a2015-12-15 10:55:36 +00007311 // Insert a slow path based read barrier *after* the reference load.
7312 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007313 // If heap poisoning is enabled, the unpoisoning of the loaded
7314 // reference will be carried out by the runtime within the slow
7315 // path.
7316 //
7317 // Note that `ref` currently does not get unpoisoned (when heap
7318 // poisoning is enabled), which is alright as the `ref` argument is
7319 // not used by the artReadBarrierSlow entry point.
7320 //
7321 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7322 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7323 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7324 AddSlowPath(slow_path);
7325
Roland Levillain0d5a2812015-11-13 10:07:31 +00007326 __ jmp(slow_path->GetEntryLabel());
7327 __ Bind(slow_path->GetExitLabel());
7328}
7329
Roland Levillain7c1559a2015-12-15 10:55:36 +00007330void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7331 Location out,
7332 Location ref,
7333 Location obj,
7334 uint32_t offset,
7335 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007336 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007337 // Baker's read barriers shall be handled by the fast path
7338 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7339 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007340 // If heap poisoning is enabled, unpoisoning will be taken care of
7341 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007342 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007343 } else if (kPoisonHeapReferences) {
7344 __ UnpoisonHeapReference(out.AsRegister<Register>());
7345 }
7346}
7347
Roland Levillain7c1559a2015-12-15 10:55:36 +00007348void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7349 Location out,
7350 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007351 DCHECK(kEmitCompilerReadBarrier);
7352
Roland Levillain7c1559a2015-12-15 10:55:36 +00007353 // Insert a slow path based read barrier *after* the GC root load.
7354 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007355 // Note that GC roots are not affected by heap poisoning, so we do
7356 // not need to do anything special for this here.
7357 SlowPathCode* slow_path =
7358 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7359 AddSlowPath(slow_path);
7360
Roland Levillain0d5a2812015-11-13 10:07:31 +00007361 __ jmp(slow_path->GetEntryLabel());
7362 __ Bind(slow_path->GetExitLabel());
7363}
7364
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007365void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007366 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007367 LOG(FATAL) << "Unreachable";
7368}
7369
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007370void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007371 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007372 LOG(FATAL) << "Unreachable";
7373}
7374
Mark Mendellfe57faa2015-09-18 09:26:15 -04007375// Simple implementation of packed switch - generate cascaded compare/jumps.
7376void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7377 LocationSummary* locations =
7378 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7379 locations->SetInAt(0, Location::RequiresRegister());
7380}
7381
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007382void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7383 int32_t lower_bound,
7384 uint32_t num_entries,
7385 HBasicBlock* switch_block,
7386 HBasicBlock* default_block) {
7387 // Figure out the correct compare values and jump conditions.
7388 // Handle the first compare/branch as a special case because it might
7389 // jump to the default case.
7390 DCHECK_GT(num_entries, 2u);
7391 Condition first_condition;
7392 uint32_t index;
7393 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7394 if (lower_bound != 0) {
7395 first_condition = kLess;
7396 __ cmpl(value_reg, Immediate(lower_bound));
7397 __ j(first_condition, codegen_->GetLabelOf(default_block));
7398 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007399
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007400 index = 1;
7401 } else {
7402 // Handle all the compare/jumps below.
7403 first_condition = kBelow;
7404 index = 0;
7405 }
7406
7407 // Handle the rest of the compare/jumps.
7408 for (; index + 1 < num_entries; index += 2) {
7409 int32_t compare_to_value = lower_bound + index + 1;
7410 __ cmpl(value_reg, Immediate(compare_to_value));
7411 // Jump to successors[index] if value < case_value[index].
7412 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7413 // Jump to successors[index + 1] if value == case_value[index + 1].
7414 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7415 }
7416
7417 if (index != num_entries) {
7418 // There are an odd number of entries. Handle the last one.
7419 DCHECK_EQ(index + 1, num_entries);
7420 __ cmpl(value_reg, Immediate(lower_bound + index));
7421 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007422 }
7423
7424 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007425 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7426 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007427 }
7428}
7429
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007430void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7431 int32_t lower_bound = switch_instr->GetStartValue();
7432 uint32_t num_entries = switch_instr->GetNumEntries();
7433 LocationSummary* locations = switch_instr->GetLocations();
7434 Register value_reg = locations->InAt(0).AsRegister<Register>();
7435
7436 GenPackedSwitchWithCompares(value_reg,
7437 lower_bound,
7438 num_entries,
7439 switch_instr->GetBlock(),
7440 switch_instr->GetDefaultBlock());
7441}
7442
Mark Mendell805b3b52015-09-18 14:10:29 -04007443void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7444 LocationSummary* locations =
7445 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7446 locations->SetInAt(0, Location::RequiresRegister());
7447
7448 // Constant area pointer.
7449 locations->SetInAt(1, Location::RequiresRegister());
7450
7451 // And the temporary we need.
7452 locations->AddTemp(Location::RequiresRegister());
7453}
7454
7455void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7456 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007457 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007458 LocationSummary* locations = switch_instr->GetLocations();
7459 Register value_reg = locations->InAt(0).AsRegister<Register>();
7460 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7461
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007462 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7463 GenPackedSwitchWithCompares(value_reg,
7464 lower_bound,
7465 num_entries,
7466 switch_instr->GetBlock(),
7467 default_block);
7468 return;
7469 }
7470
Mark Mendell805b3b52015-09-18 14:10:29 -04007471 // Optimizing has a jump area.
7472 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7473 Register constant_area = locations->InAt(1).AsRegister<Register>();
7474
7475 // Remove the bias, if needed.
7476 if (lower_bound != 0) {
7477 __ leal(temp_reg, Address(value_reg, -lower_bound));
7478 value_reg = temp_reg;
7479 }
7480
7481 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007482 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007483 __ cmpl(value_reg, Immediate(num_entries - 1));
7484 __ j(kAbove, codegen_->GetLabelOf(default_block));
7485
7486 // We are in the range of the table.
7487 // Load (target-constant_area) from the jump table, indexing by the value.
7488 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7489
7490 // Compute the actual target address by adding in constant_area.
7491 __ addl(temp_reg, constant_area);
7492
7493 // And jump.
7494 __ jmp(temp_reg);
7495}
7496
Mark Mendell0616ae02015-04-17 12:49:27 -04007497void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7498 HX86ComputeBaseMethodAddress* insn) {
7499 LocationSummary* locations =
7500 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7501 locations->SetOut(Location::RequiresRegister());
7502}
7503
7504void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7505 HX86ComputeBaseMethodAddress* insn) {
7506 LocationSummary* locations = insn->GetLocations();
7507 Register reg = locations->Out().AsRegister<Register>();
7508
7509 // Generate call to next instruction.
7510 Label next_instruction;
7511 __ call(&next_instruction);
7512 __ Bind(&next_instruction);
7513
7514 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007515 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007516
7517 // Grab the return address off the stack.
7518 __ popl(reg);
7519}
7520
7521void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7522 HX86LoadFromConstantTable* insn) {
7523 LocationSummary* locations =
7524 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7525
7526 locations->SetInAt(0, Location::RequiresRegister());
7527 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7528
7529 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007530 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007531 return;
7532 }
7533
7534 switch (insn->GetType()) {
7535 case Primitive::kPrimFloat:
7536 case Primitive::kPrimDouble:
7537 locations->SetOut(Location::RequiresFpuRegister());
7538 break;
7539
7540 case Primitive::kPrimInt:
7541 locations->SetOut(Location::RequiresRegister());
7542 break;
7543
7544 default:
7545 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7546 }
7547}
7548
7549void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007550 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007551 return;
7552 }
7553
7554 LocationSummary* locations = insn->GetLocations();
7555 Location out = locations->Out();
7556 Register const_area = locations->InAt(0).AsRegister<Register>();
7557 HConstant *value = insn->GetConstant();
7558
7559 switch (insn->GetType()) {
7560 case Primitive::kPrimFloat:
7561 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007562 codegen_->LiteralFloatAddress(
7563 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007564 break;
7565
7566 case Primitive::kPrimDouble:
7567 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007568 codegen_->LiteralDoubleAddress(
7569 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007570 break;
7571
7572 case Primitive::kPrimInt:
7573 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007574 codegen_->LiteralInt32Address(
7575 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007576 break;
7577
7578 default:
7579 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7580 }
7581}
7582
Mark Mendell0616ae02015-04-17 12:49:27 -04007583/**
7584 * Class to handle late fixup of offsets into constant area.
7585 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007586class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007587 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007588 RIPFixup(CodeGeneratorX86& codegen,
7589 HX86ComputeBaseMethodAddress* base_method_address,
7590 size_t offset)
7591 : codegen_(&codegen),
7592 base_method_address_(base_method_address),
7593 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007594
7595 protected:
7596 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7597
7598 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007599 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007600
7601 private:
7602 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7603 // Patch the correct offset for the instruction. The place to patch is the
7604 // last 4 bytes of the instruction.
7605 // The value to patch is the distance from the offset in the constant area
7606 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007607 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007608 int32_t relative_position =
7609 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007610
7611 // Patch in the right value.
7612 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7613 }
7614
Mark Mendell0616ae02015-04-17 12:49:27 -04007615 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007616 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007617};
7618
Mark Mendell805b3b52015-09-18 14:10:29 -04007619/**
7620 * Class to handle late fixup of offsets to a jump table that will be created in the
7621 * constant area.
7622 */
7623class JumpTableRIPFixup : public RIPFixup {
7624 public:
7625 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007626 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7627 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007628
7629 void CreateJumpTable() {
7630 X86Assembler* assembler = codegen_->GetAssembler();
7631
7632 // Ensure that the reference to the jump table has the correct offset.
7633 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7634 SetOffset(offset_in_constant_table);
7635
7636 // The label values in the jump table are computed relative to the
7637 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007638 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007639
7640 // Populate the jump table with the correct values for the jump table.
7641 int32_t num_entries = switch_instr_->GetNumEntries();
7642 HBasicBlock* block = switch_instr_->GetBlock();
7643 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7644 // The value that we want is the target offset - the position of the table.
7645 for (int32_t i = 0; i < num_entries; i++) {
7646 HBasicBlock* b = successors[i];
7647 Label* l = codegen_->GetLabelOf(b);
7648 DCHECK(l->IsBound());
7649 int32_t offset_to_block = l->Position() - relative_offset;
7650 assembler->AppendInt32(offset_to_block);
7651 }
7652 }
7653
7654 private:
7655 const HX86PackedSwitch* switch_instr_;
7656};
7657
7658void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7659 // Generate the constant area if needed.
7660 X86Assembler* assembler = GetAssembler();
7661 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7662 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7663 // byte values.
7664 assembler->Align(4, 0);
7665 constant_area_start_ = assembler->CodeSize();
7666
7667 // Populate any jump tables.
7668 for (auto jump_table : fixups_to_jump_tables_) {
7669 jump_table->CreateJumpTable();
7670 }
7671
7672 // And now add the constant area to the generated code.
7673 assembler->AddConstantArea();
7674 }
7675
7676 // And finish up.
7677 CodeGenerator::Finalize(allocator);
7678}
7679
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007680Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7681 HX86ComputeBaseMethodAddress* method_base,
7682 Register reg) {
7683 AssemblerFixup* fixup =
7684 new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007685 return Address(reg, kDummy32BitOffset, fixup);
7686}
7687
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007688Address CodeGeneratorX86::LiteralFloatAddress(float v,
7689 HX86ComputeBaseMethodAddress* method_base,
7690 Register reg) {
7691 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007692 return Address(reg, kDummy32BitOffset, fixup);
7693}
7694
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007695Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7696 HX86ComputeBaseMethodAddress* method_base,
7697 Register reg) {
7698 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007699 return Address(reg, kDummy32BitOffset, fixup);
7700}
7701
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007702Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7703 HX86ComputeBaseMethodAddress* method_base,
7704 Register reg) {
7705 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007706 return Address(reg, kDummy32BitOffset, fixup);
7707}
7708
Aart Bika19616e2016-02-01 18:57:58 -08007709void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7710 if (value == 0) {
7711 __ xorl(dest, dest);
7712 } else {
7713 __ movl(dest, Immediate(value));
7714 }
7715}
7716
7717void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7718 if (value == 0) {
7719 __ testl(dest, dest);
7720 } else {
7721 __ cmpl(dest, Immediate(value));
7722 }
7723}
7724
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007725void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7726 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007727 GenerateIntCompare(lhs_reg, rhs);
7728}
7729
7730void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007731 if (rhs.IsConstant()) {
7732 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007733 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007734 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007735 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007736 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007737 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007738 }
7739}
7740
7741Address CodeGeneratorX86::ArrayAddress(Register obj,
7742 Location index,
7743 ScaleFactor scale,
7744 uint32_t data_offset) {
7745 return index.IsConstant() ?
7746 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7747 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7748}
7749
Mark Mendell805b3b52015-09-18 14:10:29 -04007750Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7751 Register reg,
7752 Register value) {
7753 // Create a fixup to be used to create and address the jump table.
7754 JumpTableRIPFixup* table_fixup =
7755 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7756
7757 // We have to populate the jump tables.
7758 fixups_to_jump_tables_.push_back(table_fixup);
7759
7760 // We want a scaled address, as we are extracting the correct offset from the table.
7761 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7762}
7763
Andreas Gampe85b62f22015-09-09 13:15:38 -07007764// TODO: target as memory.
7765void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7766 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007767 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007768 return;
7769 }
7770
7771 DCHECK_NE(type, Primitive::kPrimVoid);
7772
7773 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7774 if (target.Equals(return_loc)) {
7775 return;
7776 }
7777
7778 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7779 // with the else branch.
7780 if (type == Primitive::kPrimLong) {
7781 HParallelMove parallel_move(GetGraph()->GetArena());
7782 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7783 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7784 GetMoveResolver()->EmitNativeCode(&parallel_move);
7785 } else {
7786 // Let the parallel move resolver take care of all of this.
7787 HParallelMove parallel_move(GetGraph()->GetArena());
7788 parallel_move.AddMove(return_loc, target, type, nullptr);
7789 GetMoveResolver()->EmitNativeCode(&parallel_move);
7790 }
7791}
7792
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007793void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7794 const uint8_t* roots_data,
7795 const PatchInfo<Label>& info,
7796 uint64_t index_in_table) const {
7797 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7798 uintptr_t address =
7799 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7800 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7801 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7802 dchecked_integral_cast<uint32_t>(address);
7803}
7804
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007805void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7806 for (const PatchInfo<Label>& info : jit_string_patches_) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007807 const auto& it = jit_string_roots_.find(
7808 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007809 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007810 PatchJitRootUse(code, roots_data, info, it->second);
7811 }
7812
7813 for (const PatchInfo<Label>& info : jit_class_patches_) {
7814 const auto& it = jit_class_roots_.find(
7815 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7816 DCHECK(it != jit_class_roots_.end());
7817 PatchJitRootUse(code, roots_data, info, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007818 }
7819}
7820
Roland Levillain4d027112015-07-01 15:41:14 +01007821#undef __
7822
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007823} // namespace x86
7824} // namespace art