blob: 3e795c7bf829cd657fe447c4c2ac7661ffdfe32a [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 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1019 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001020 boot_image_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1021 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001022 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001023 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001024 constant_area_start_(-1),
1025 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001026 method_address_offset_(std::less<uint32_t>(),
1027 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001028 // Use a fake return address register to mimic Quick.
1029 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001030}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001031
David Brazdil58282f42016-01-14 12:45:10 +00001032void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001033 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001034 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001035}
1036
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001037InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001038 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001039 assembler_(codegen->GetAssembler()),
1040 codegen_(codegen) {}
1041
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001042static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001043 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001044}
1045
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001046void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001047 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001048 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001049 bool skip_overflow_check =
1050 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001051 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001052
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001053 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001054 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001055 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001056 }
1057
Mark Mendell5f874182015-03-04 15:42:45 -05001058 if (HasEmptyFrame()) {
1059 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001060 }
Mark Mendell5f874182015-03-04 15:42:45 -05001061
1062 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1063 Register reg = kCoreCalleeSaves[i];
1064 if (allocated_registers_.ContainsCoreRegister(reg)) {
1065 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001066 __ cfi().AdjustCFAOffset(kX86WordSize);
1067 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001068 }
1069 }
1070
Mingyao Yang063fc772016-08-02 11:02:54 -07001071 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1072 // Initialize should_deoptimize flag to 0.
1073 __ movl(Address(ESP, -kShouldDeoptimizeFlagSize), Immediate(0));
1074 }
1075
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001076 int adjust = GetFrameSize() - FrameEntrySpillSize();
1077 __ subl(ESP, Immediate(adjust));
1078 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001079 // Save the current method if we need it. Note that we do not
1080 // do this in HCurrentMethod, as the instruction might have been removed
1081 // in the SSA graph.
1082 if (RequiresCurrentMethod()) {
1083 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1084 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001085}
1086
1087void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001088 __ cfi().RememberState();
1089 if (!HasEmptyFrame()) {
1090 int adjust = GetFrameSize() - FrameEntrySpillSize();
1091 __ addl(ESP, Immediate(adjust));
1092 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001093
David Srbeckyc34dc932015-04-12 09:27:43 +01001094 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1095 Register reg = kCoreCalleeSaves[i];
1096 if (allocated_registers_.ContainsCoreRegister(reg)) {
1097 __ popl(reg);
1098 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1099 __ cfi().Restore(DWARFReg(reg));
1100 }
Mark Mendell5f874182015-03-04 15:42:45 -05001101 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001102 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001103 __ ret();
1104 __ cfi().RestoreState();
1105 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001106}
1107
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001108void CodeGeneratorX86::Bind(HBasicBlock* block) {
1109 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001110}
1111
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001112Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1113 switch (type) {
1114 case Primitive::kPrimBoolean:
1115 case Primitive::kPrimByte:
1116 case Primitive::kPrimChar:
1117 case Primitive::kPrimShort:
1118 case Primitive::kPrimInt:
1119 case Primitive::kPrimNot:
1120 return Location::RegisterLocation(EAX);
1121
1122 case Primitive::kPrimLong:
1123 return Location::RegisterPairLocation(EAX, EDX);
1124
1125 case Primitive::kPrimVoid:
1126 return Location::NoLocation();
1127
1128 case Primitive::kPrimDouble:
1129 case Primitive::kPrimFloat:
1130 return Location::FpuRegisterLocation(XMM0);
1131 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001132
1133 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001134}
1135
1136Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1137 return Location::RegisterLocation(kMethodRegisterArgument);
1138}
1139
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001140Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001141 switch (type) {
1142 case Primitive::kPrimBoolean:
1143 case Primitive::kPrimByte:
1144 case Primitive::kPrimChar:
1145 case Primitive::kPrimShort:
1146 case Primitive::kPrimInt:
1147 case Primitive::kPrimNot: {
1148 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001149 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001150 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001151 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001152 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001153 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001154 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001155 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001156
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001157 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001158 uint32_t index = gp_index_;
1159 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001160 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001161 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001162 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1163 calling_convention.GetRegisterPairAt(index));
1164 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001165 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001166 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1167 }
1168 }
1169
1170 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001171 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001172 stack_index_++;
1173 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1174 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1175 } else {
1176 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1177 }
1178 }
1179
1180 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001181 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001182 stack_index_ += 2;
1183 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1184 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1185 } else {
1186 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001187 }
1188 }
1189
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001190 case Primitive::kPrimVoid:
1191 LOG(FATAL) << "Unexpected parameter type " << type;
1192 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001193 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001194 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001195}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001196
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001197void CodeGeneratorX86::Move32(Location destination, Location source) {
1198 if (source.Equals(destination)) {
1199 return;
1200 }
1201 if (destination.IsRegister()) {
1202 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001203 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001204 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001205 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001206 } else {
1207 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001208 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001209 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001210 } else if (destination.IsFpuRegister()) {
1211 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001212 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001213 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001214 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001215 } else {
1216 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001217 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001218 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001219 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001220 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001221 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001222 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001223 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001224 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001225 } else if (source.IsConstant()) {
1226 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001227 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001228 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001229 } else {
1230 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001231 __ pushl(Address(ESP, source.GetStackIndex()));
1232 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001233 }
1234 }
1235}
1236
1237void CodeGeneratorX86::Move64(Location destination, Location source) {
1238 if (source.Equals(destination)) {
1239 return;
1240 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001241 if (destination.IsRegisterPair()) {
1242 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001243 EmitParallelMoves(
1244 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1245 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001246 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001247 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001248 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1249 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001250 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001251 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1252 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1253 __ psrlq(src_reg, Immediate(32));
1254 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001255 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001256 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001257 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001258 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1259 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001260 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1261 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001262 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001263 if (source.IsFpuRegister()) {
1264 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1265 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001266 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001267 } else if (source.IsRegisterPair()) {
1268 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1269 // Create stack space for 2 elements.
1270 __ subl(ESP, Immediate(2 * elem_size));
1271 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1272 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1273 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1274 // And remove the temporary stack space we allocated.
1275 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001276 } else {
1277 LOG(FATAL) << "Unimplemented";
1278 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001279 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001280 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001281 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001282 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001283 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001284 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001285 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001286 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001287 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001288 } else if (source.IsConstant()) {
1289 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001290 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1291 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001292 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001293 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1294 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001295 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001296 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001297 EmitParallelMoves(
1298 Location::StackSlot(source.GetStackIndex()),
1299 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001300 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001301 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001302 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1303 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001304 }
1305 }
1306}
1307
Calin Juravle175dc732015-08-25 15:42:32 +01001308void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1309 DCHECK(location.IsRegister());
1310 __ movl(location.AsRegister<Register>(), Immediate(value));
1311}
1312
Calin Juravlee460d1d2015-09-29 04:52:17 +01001313void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001314 HParallelMove move(GetGraph()->GetArena());
1315 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1316 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1317 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001318 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001319 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001320 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001321 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001322}
1323
1324void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1325 if (location.IsRegister()) {
1326 locations->AddTemp(location);
1327 } else if (location.IsRegisterPair()) {
1328 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1329 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1330 } else {
1331 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1332 }
1333}
1334
David Brazdilfc6a86a2015-06-26 10:33:45 +00001335void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001336 DCHECK(!successor->IsExitBlock());
1337
1338 HBasicBlock* block = got->GetBlock();
1339 HInstruction* previous = got->GetPrevious();
1340
1341 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001342 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001343 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1344 return;
1345 }
1346
1347 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1348 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1349 }
1350 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001351 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001352 }
1353}
1354
David Brazdilfc6a86a2015-06-26 10:33:45 +00001355void LocationsBuilderX86::VisitGoto(HGoto* got) {
1356 got->SetLocations(nullptr);
1357}
1358
1359void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1360 HandleGoto(got, got->GetSuccessor());
1361}
1362
1363void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1364 try_boundary->SetLocations(nullptr);
1365}
1366
1367void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1368 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1369 if (!successor->IsExitBlock()) {
1370 HandleGoto(try_boundary, successor);
1371 }
1372}
1373
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001374void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001375 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001376}
1377
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001378void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001379}
1380
Mark Mendell152408f2015-12-31 12:28:50 -05001381template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001382void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001383 LabelType* true_label,
1384 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001385 if (cond->IsFPConditionTrueIfNaN()) {
1386 __ j(kUnordered, true_label);
1387 } else if (cond->IsFPConditionFalseIfNaN()) {
1388 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001389 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001390 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001391}
1392
Mark Mendell152408f2015-12-31 12:28:50 -05001393template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001394void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001395 LabelType* true_label,
1396 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001397 LocationSummary* locations = cond->GetLocations();
1398 Location left = locations->InAt(0);
1399 Location right = locations->InAt(1);
1400 IfCondition if_cond = cond->GetCondition();
1401
Mark Mendellc4701932015-04-10 13:18:51 -04001402 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001403 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001404 IfCondition true_high_cond = if_cond;
1405 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001406 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001407
1408 // Set the conditions for the test, remembering that == needs to be
1409 // decided using the low words.
1410 switch (if_cond) {
1411 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001412 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001413 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001414 break;
1415 case kCondLT:
1416 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001417 break;
1418 case kCondLE:
1419 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001420 break;
1421 case kCondGT:
1422 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001423 break;
1424 case kCondGE:
1425 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001426 break;
Aart Bike9f37602015-10-09 11:15:55 -07001427 case kCondB:
1428 false_high_cond = kCondA;
1429 break;
1430 case kCondBE:
1431 true_high_cond = kCondB;
1432 break;
1433 case kCondA:
1434 false_high_cond = kCondB;
1435 break;
1436 case kCondAE:
1437 true_high_cond = kCondA;
1438 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001439 }
1440
1441 if (right.IsConstant()) {
1442 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001443 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001444 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001445
Aart Bika19616e2016-02-01 18:57:58 -08001446 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001447 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001448 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001449 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001450 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001451 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001452 __ j(X86Condition(true_high_cond), true_label);
1453 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001454 }
1455 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001456 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001457 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001458 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001459 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001460
1461 __ cmpl(left_high, right_high);
1462 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001463 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001464 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001465 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001466 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001467 __ j(X86Condition(true_high_cond), true_label);
1468 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001469 }
1470 // Must be equal high, so compare the lows.
1471 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001472 } else {
1473 DCHECK(right.IsDoubleStackSlot());
1474 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1475 if (if_cond == kCondNE) {
1476 __ j(X86Condition(true_high_cond), true_label);
1477 } else if (if_cond == kCondEQ) {
1478 __ j(X86Condition(false_high_cond), false_label);
1479 } else {
1480 __ j(X86Condition(true_high_cond), true_label);
1481 __ j(X86Condition(false_high_cond), false_label);
1482 }
1483 // Must be equal high, so compare the lows.
1484 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001485 }
1486 // The last comparison might be unsigned.
1487 __ j(final_condition, true_label);
1488}
1489
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001490void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1491 Location rhs,
1492 HInstruction* insn,
1493 bool is_double) {
1494 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1495 if (is_double) {
1496 if (rhs.IsFpuRegister()) {
1497 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1498 } else if (const_area != nullptr) {
1499 DCHECK(const_area->IsEmittedAtUseSite());
1500 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1501 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001502 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1503 const_area->GetBaseMethodAddress(),
1504 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001505 } else {
1506 DCHECK(rhs.IsDoubleStackSlot());
1507 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1508 }
1509 } else {
1510 if (rhs.IsFpuRegister()) {
1511 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1512 } else if (const_area != nullptr) {
1513 DCHECK(const_area->IsEmittedAtUseSite());
1514 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1515 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001516 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1517 const_area->GetBaseMethodAddress(),
1518 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001519 } else {
1520 DCHECK(rhs.IsStackSlot());
1521 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1522 }
1523 }
1524}
1525
Mark Mendell152408f2015-12-31 12:28:50 -05001526template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001527void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001528 LabelType* true_target_in,
1529 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001530 // Generated branching requires both targets to be explicit. If either of the
1531 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001532 LabelType fallthrough_target;
1533 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1534 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001535
Mark Mendellc4701932015-04-10 13:18:51 -04001536 LocationSummary* locations = condition->GetLocations();
1537 Location left = locations->InAt(0);
1538 Location right = locations->InAt(1);
1539
Mark Mendellc4701932015-04-10 13:18:51 -04001540 Primitive::Type type = condition->InputAt(0)->GetType();
1541 switch (type) {
1542 case Primitive::kPrimLong:
1543 GenerateLongComparesAndJumps(condition, true_target, false_target);
1544 break;
1545 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001546 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001547 GenerateFPJumps(condition, true_target, false_target);
1548 break;
1549 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001550 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001551 GenerateFPJumps(condition, true_target, false_target);
1552 break;
1553 default:
1554 LOG(FATAL) << "Unexpected compare type " << type;
1555 }
1556
David Brazdil0debae72015-11-12 18:37:00 +00001557 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001558 __ jmp(false_target);
1559 }
David Brazdil0debae72015-11-12 18:37:00 +00001560
1561 if (fallthrough_target.IsLinked()) {
1562 __ Bind(&fallthrough_target);
1563 }
Mark Mendellc4701932015-04-10 13:18:51 -04001564}
1565
David Brazdil0debae72015-11-12 18:37:00 +00001566static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1567 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1568 // are set only strictly before `branch`. We can't use the eflags on long/FP
1569 // conditions if they are materialized due to the complex branching.
1570 return cond->IsCondition() &&
1571 cond->GetNext() == branch &&
1572 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1573 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1574}
1575
Mark Mendell152408f2015-12-31 12:28:50 -05001576template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001577void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001578 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001579 LabelType* true_target,
1580 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001581 HInstruction* cond = instruction->InputAt(condition_input_index);
1582
1583 if (true_target == nullptr && false_target == nullptr) {
1584 // Nothing to do. The code always falls through.
1585 return;
1586 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001587 // Constant condition, statically compared against "true" (integer value 1).
1588 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001589 if (true_target != nullptr) {
1590 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001591 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001592 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001593 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001594 if (false_target != nullptr) {
1595 __ jmp(false_target);
1596 }
1597 }
1598 return;
1599 }
1600
1601 // The following code generates these patterns:
1602 // (1) true_target == nullptr && false_target != nullptr
1603 // - opposite condition true => branch to false_target
1604 // (2) true_target != nullptr && false_target == nullptr
1605 // - condition true => branch to true_target
1606 // (3) true_target != nullptr && false_target != nullptr
1607 // - condition true => branch to true_target
1608 // - branch to false_target
1609 if (IsBooleanValueOrMaterializedCondition(cond)) {
1610 if (AreEflagsSetFrom(cond, instruction)) {
1611 if (true_target == nullptr) {
1612 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1613 } else {
1614 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1615 }
1616 } else {
1617 // Materialized condition, compare against 0.
1618 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1619 if (lhs.IsRegister()) {
1620 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1621 } else {
1622 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1623 }
1624 if (true_target == nullptr) {
1625 __ j(kEqual, false_target);
1626 } else {
1627 __ j(kNotEqual, true_target);
1628 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001629 }
1630 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001631 // Condition has not been materialized, use its inputs as the comparison and
1632 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001633 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001634
1635 // If this is a long or FP comparison that has been folded into
1636 // the HCondition, generate the comparison directly.
1637 Primitive::Type type = condition->InputAt(0)->GetType();
1638 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1639 GenerateCompareTestAndBranch(condition, true_target, false_target);
1640 return;
1641 }
1642
1643 Location lhs = condition->GetLocations()->InAt(0);
1644 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001645 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001646 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001647 if (true_target == nullptr) {
1648 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1649 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001650 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001651 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001652 }
David Brazdil0debae72015-11-12 18:37:00 +00001653
1654 // If neither branch falls through (case 3), the conditional branch to `true_target`
1655 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1656 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001657 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001658 }
1659}
1660
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001661void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001662 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1663 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001664 locations->SetInAt(0, Location::Any());
1665 }
1666}
1667
1668void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001669 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1670 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1671 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1672 nullptr : codegen_->GetLabelOf(true_successor);
1673 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1674 nullptr : codegen_->GetLabelOf(false_successor);
1675 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001676}
1677
1678void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1679 LocationSummary* locations = new (GetGraph()->GetArena())
1680 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001681 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001682 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001683 locations->SetInAt(0, Location::Any());
1684 }
1685}
1686
1687void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001688 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001689 GenerateTestAndBranch<Label>(deoptimize,
1690 /* condition_input_index */ 0,
1691 slow_path->GetEntryLabel(),
1692 /* false_target */ nullptr);
1693}
1694
Mingyao Yang063fc772016-08-02 11:02:54 -07001695void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1696 LocationSummary* locations = new (GetGraph()->GetArena())
1697 LocationSummary(flag, LocationSummary::kNoCall);
1698 locations->SetOut(Location::RequiresRegister());
1699}
1700
1701void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1702 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1703 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1704}
1705
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001706static bool SelectCanUseCMOV(HSelect* select) {
1707 // There are no conditional move instructions for XMMs.
1708 if (Primitive::IsFloatingPointType(select->GetType())) {
1709 return false;
1710 }
1711
1712 // A FP condition doesn't generate the single CC that we need.
1713 // In 32 bit mode, a long condition doesn't generate a single CC either.
1714 HInstruction* condition = select->GetCondition();
1715 if (condition->IsCondition()) {
1716 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1717 if (compare_type == Primitive::kPrimLong ||
1718 Primitive::IsFloatingPointType(compare_type)) {
1719 return false;
1720 }
1721 }
1722
1723 // We can generate a CMOV for this Select.
1724 return true;
1725}
1726
David Brazdil74eb1b22015-12-14 11:44:01 +00001727void LocationsBuilderX86::VisitSelect(HSelect* select) {
1728 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001729 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001730 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001731 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001732 } else {
1733 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001734 if (SelectCanUseCMOV(select)) {
1735 if (select->InputAt(1)->IsConstant()) {
1736 // Cmov can't handle a constant value.
1737 locations->SetInAt(1, Location::RequiresRegister());
1738 } else {
1739 locations->SetInAt(1, Location::Any());
1740 }
1741 } else {
1742 locations->SetInAt(1, Location::Any());
1743 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001744 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001745 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1746 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001747 }
1748 locations->SetOut(Location::SameAsFirstInput());
1749}
1750
1751void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1752 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001753 DCHECK(locations->InAt(0).Equals(locations->Out()));
1754 if (SelectCanUseCMOV(select)) {
1755 // If both the condition and the source types are integer, we can generate
1756 // a CMOV to implement Select.
1757
1758 HInstruction* select_condition = select->GetCondition();
1759 Condition cond = kNotEqual;
1760
1761 // Figure out how to test the 'condition'.
1762 if (select_condition->IsCondition()) {
1763 HCondition* condition = select_condition->AsCondition();
1764 if (!condition->IsEmittedAtUseSite()) {
1765 // This was a previously materialized condition.
1766 // Can we use the existing condition code?
1767 if (AreEflagsSetFrom(condition, select)) {
1768 // Materialization was the previous instruction. Condition codes are right.
1769 cond = X86Condition(condition->GetCondition());
1770 } else {
1771 // No, we have to recreate the condition code.
1772 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1773 __ testl(cond_reg, cond_reg);
1774 }
1775 } else {
1776 // We can't handle FP or long here.
1777 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1778 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1779 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001780 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001781 cond = X86Condition(condition->GetCondition());
1782 }
1783 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001784 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001785 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1786 __ testl(cond_reg, cond_reg);
1787 }
1788
1789 // If the condition is true, overwrite the output, which already contains false.
1790 Location false_loc = locations->InAt(0);
1791 Location true_loc = locations->InAt(1);
1792 if (select->GetType() == Primitive::kPrimLong) {
1793 // 64 bit conditional move.
1794 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1795 Register false_low = false_loc.AsRegisterPairLow<Register>();
1796 if (true_loc.IsRegisterPair()) {
1797 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1798 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1799 } else {
1800 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1801 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1802 }
1803 } else {
1804 // 32 bit conditional move.
1805 Register false_reg = false_loc.AsRegister<Register>();
1806 if (true_loc.IsRegister()) {
1807 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1808 } else {
1809 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1810 }
1811 }
1812 } else {
1813 NearLabel false_target;
1814 GenerateTestAndBranch<NearLabel>(
1815 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1816 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1817 __ Bind(&false_target);
1818 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001819}
1820
David Srbecky0cf44932015-12-09 14:09:59 +00001821void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1822 new (GetGraph()->GetArena()) LocationSummary(info);
1823}
1824
David Srbeckyd28f4a02016-03-14 17:14:24 +00001825void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1826 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001827}
1828
1829void CodeGeneratorX86::GenerateNop() {
1830 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001831}
1832
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001833void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001834 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001835 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001836 // Handle the long/FP comparisons made in instruction simplification.
1837 switch (cond->InputAt(0)->GetType()) {
1838 case Primitive::kPrimLong: {
1839 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001840 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001841 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001842 locations->SetOut(Location::RequiresRegister());
1843 }
1844 break;
1845 }
1846 case Primitive::kPrimFloat:
1847 case Primitive::kPrimDouble: {
1848 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001849 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1850 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1851 } else if (cond->InputAt(1)->IsConstant()) {
1852 locations->SetInAt(1, Location::RequiresFpuRegister());
1853 } else {
1854 locations->SetInAt(1, Location::Any());
1855 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001856 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001857 locations->SetOut(Location::RequiresRegister());
1858 }
1859 break;
1860 }
1861 default:
1862 locations->SetInAt(0, Location::RequiresRegister());
1863 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001864 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001865 // We need a byte register.
1866 locations->SetOut(Location::RegisterLocation(ECX));
1867 }
1868 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001869 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001870}
1871
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001872void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001873 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001874 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001875 }
Mark Mendellc4701932015-04-10 13:18:51 -04001876
1877 LocationSummary* locations = cond->GetLocations();
1878 Location lhs = locations->InAt(0);
1879 Location rhs = locations->InAt(1);
1880 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001881 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001882
1883 switch (cond->InputAt(0)->GetType()) {
1884 default: {
1885 // Integer case.
1886
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001887 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001888 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001889 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001890 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001891 return;
1892 }
1893 case Primitive::kPrimLong:
1894 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1895 break;
1896 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001897 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001898 GenerateFPJumps(cond, &true_label, &false_label);
1899 break;
1900 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001901 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001902 GenerateFPJumps(cond, &true_label, &false_label);
1903 break;
1904 }
1905
1906 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001907 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001908
Roland Levillain4fa13f62015-07-06 18:11:54 +01001909 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001910 __ Bind(&false_label);
1911 __ xorl(reg, reg);
1912 __ jmp(&done_label);
1913
Roland Levillain4fa13f62015-07-06 18:11:54 +01001914 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001915 __ Bind(&true_label);
1916 __ movl(reg, Immediate(1));
1917 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001918}
1919
1920void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001921 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001922}
1923
1924void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001925 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001926}
1927
1928void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001929 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001930}
1931
1932void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001933 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001934}
1935
1936void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001937 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001938}
1939
1940void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001941 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001942}
1943
1944void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001945 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001946}
1947
1948void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001949 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001950}
1951
1952void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001953 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001954}
1955
1956void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001957 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001958}
1959
1960void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001961 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001962}
1963
1964void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001965 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001966}
1967
Aart Bike9f37602015-10-09 11:15:55 -07001968void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001969 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001970}
1971
1972void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001973 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001974}
1975
1976void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001977 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001978}
1979
1980void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001981 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001982}
1983
1984void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001985 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001986}
1987
1988void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001989 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001990}
1991
1992void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001993 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001994}
1995
1996void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001997 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001998}
1999
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002000void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002001 LocationSummary* locations =
2002 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002003 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002004}
2005
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002006void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002007 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002008}
2009
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002010void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2011 LocationSummary* locations =
2012 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2013 locations->SetOut(Location::ConstantLocation(constant));
2014}
2015
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002016void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002017 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002018}
2019
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002020void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002021 LocationSummary* locations =
2022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002023 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002024}
2025
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002026void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002027 // Will be generated at use site.
2028}
2029
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002030void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2031 LocationSummary* locations =
2032 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2033 locations->SetOut(Location::ConstantLocation(constant));
2034}
2035
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002036void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002037 // Will be generated at use site.
2038}
2039
2040void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2041 LocationSummary* locations =
2042 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2043 locations->SetOut(Location::ConstantLocation(constant));
2044}
2045
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002046void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002047 // Will be generated at use site.
2048}
2049
Calin Juravle27df7582015-04-17 19:12:31 +01002050void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2051 memory_barrier->SetLocations(nullptr);
2052}
2053
2054void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002055 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002056}
2057
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002058void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002059 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002060}
2061
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002062void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002063 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002064}
2065
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002066void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002067 LocationSummary* locations =
2068 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002069 switch (ret->InputAt(0)->GetType()) {
2070 case Primitive::kPrimBoolean:
2071 case Primitive::kPrimByte:
2072 case Primitive::kPrimChar:
2073 case Primitive::kPrimShort:
2074 case Primitive::kPrimInt:
2075 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002076 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002077 break;
2078
2079 case Primitive::kPrimLong:
2080 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002081 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002082 break;
2083
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002084 case Primitive::kPrimFloat:
2085 case Primitive::kPrimDouble:
2086 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002087 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002088 break;
2089
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002090 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002091 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002092 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002093}
2094
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002095void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002096 if (kIsDebugBuild) {
2097 switch (ret->InputAt(0)->GetType()) {
2098 case Primitive::kPrimBoolean:
2099 case Primitive::kPrimByte:
2100 case Primitive::kPrimChar:
2101 case Primitive::kPrimShort:
2102 case Primitive::kPrimInt:
2103 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002104 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002105 break;
2106
2107 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002108 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2109 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002110 break;
2111
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002112 case Primitive::kPrimFloat:
2113 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002114 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002115 break;
2116
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002117 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002118 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002119 }
2120 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002121 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002122}
2123
Calin Juravle175dc732015-08-25 15:42:32 +01002124void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2125 // The trampoline uses the same calling convention as dex calling conventions,
2126 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2127 // the method_idx.
2128 HandleInvoke(invoke);
2129}
2130
2131void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2132 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2133}
2134
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002135void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002136 // Explicit clinit checks triggered by static invokes must have been pruned by
2137 // art::PrepareForRegisterAllocation.
2138 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002139
Mark Mendellfb8d2792015-03-31 22:16:59 -04002140 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002141 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002142 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002143 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002144 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002145 return;
2146 }
2147
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002148 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002149
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002150 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2151 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002152 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002153 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002154}
2155
Mark Mendell09ed1a32015-03-25 08:30:06 -04002156static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2157 if (invoke->GetLocations()->Intrinsified()) {
2158 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2159 intrinsic.Dispatch(invoke);
2160 return true;
2161 }
2162 return false;
2163}
2164
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002165void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002166 // Explicit clinit checks triggered by static invokes must have been pruned by
2167 // art::PrepareForRegisterAllocation.
2168 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002169
Mark Mendell09ed1a32015-03-25 08:30:06 -04002170 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2171 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002172 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002173
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002174 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002175 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002176 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002177 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002178}
2179
2180void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002181 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2182 if (intrinsic.TryDispatch(invoke)) {
2183 return;
2184 }
2185
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002186 HandleInvoke(invoke);
2187}
2188
2189void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002190 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002191 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002192}
2193
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002194void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002195 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2196 return;
2197 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002198
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002199 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002200 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002201 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002202}
2203
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002204void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002205 // This call to HandleInvoke allocates a temporary (core) register
2206 // which is also used to transfer the hidden argument from FP to
2207 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002208 HandleInvoke(invoke);
2209 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002210 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002211}
2212
2213void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2214 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002215 LocationSummary* locations = invoke->GetLocations();
2216 Register temp = locations->GetTemp(0).AsRegister<Register>();
2217 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002218 Location receiver = locations->InAt(0);
2219 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2220
Roland Levillain0d5a2812015-11-13 10:07:31 +00002221 // Set the hidden argument. This is safe to do this here, as XMM7
2222 // won't be modified thereafter, before the `call` instruction.
2223 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002224 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002225 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002226
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002227 if (receiver.IsStackSlot()) {
2228 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002229 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002230 __ movl(temp, Address(temp, class_offset));
2231 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002232 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002233 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002234 }
Roland Levillain4d027112015-07-01 15:41:14 +01002235 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002236 // Instead of simply (possibly) unpoisoning `temp` here, we should
2237 // emit a read barrier for the previous class reference load.
2238 // However this is not required in practice, as this is an
2239 // intermediate/temporary reference and because the current
2240 // concurrent copying collector keeps the from-space memory
2241 // intact/accessible until the end of the marking phase (the
2242 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002243 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002244 // temp = temp->GetAddressOfIMT()
2245 __ movl(temp,
2246 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002247 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002248 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002249 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002250 __ movl(temp, Address(temp, method_offset));
2251 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002252 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002253 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002254
2255 DCHECK(!codegen_->IsLeafMethod());
2256 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2257}
2258
Orion Hodsonac141392017-01-13 11:53:47 +00002259void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2260 HandleInvoke(invoke);
2261}
2262
2263void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2264 codegen_->GenerateInvokePolymorphicCall(invoke);
2265}
2266
Roland Levillain88cb1752014-10-20 16:36:47 +01002267void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2268 LocationSummary* locations =
2269 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2270 switch (neg->GetResultType()) {
2271 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002272 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002273 locations->SetInAt(0, Location::RequiresRegister());
2274 locations->SetOut(Location::SameAsFirstInput());
2275 break;
2276
Roland Levillain88cb1752014-10-20 16:36:47 +01002277 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002278 locations->SetInAt(0, Location::RequiresFpuRegister());
2279 locations->SetOut(Location::SameAsFirstInput());
2280 locations->AddTemp(Location::RequiresRegister());
2281 locations->AddTemp(Location::RequiresFpuRegister());
2282 break;
2283
Roland Levillain88cb1752014-10-20 16:36:47 +01002284 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002285 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002286 locations->SetOut(Location::SameAsFirstInput());
2287 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002288 break;
2289
2290 default:
2291 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2292 }
2293}
2294
2295void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2296 LocationSummary* locations = neg->GetLocations();
2297 Location out = locations->Out();
2298 Location in = locations->InAt(0);
2299 switch (neg->GetResultType()) {
2300 case Primitive::kPrimInt:
2301 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002302 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002303 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002304 break;
2305
2306 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002307 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002308 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002309 __ negl(out.AsRegisterPairLow<Register>());
2310 // Negation is similar to subtraction from zero. The least
2311 // significant byte triggers a borrow when it is different from
2312 // zero; to take it into account, add 1 to the most significant
2313 // byte if the carry flag (CF) is set to 1 after the first NEGL
2314 // operation.
2315 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2316 __ negl(out.AsRegisterPairHigh<Register>());
2317 break;
2318
Roland Levillain5368c212014-11-27 15:03:41 +00002319 case Primitive::kPrimFloat: {
2320 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002321 Register constant = locations->GetTemp(0).AsRegister<Register>();
2322 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002323 // Implement float negation with an exclusive or with value
2324 // 0x80000000 (mask for bit 31, representing the sign of a
2325 // single-precision floating-point number).
2326 __ movl(constant, Immediate(INT32_C(0x80000000)));
2327 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002328 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002329 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002330 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002331
Roland Levillain5368c212014-11-27 15:03:41 +00002332 case Primitive::kPrimDouble: {
2333 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002334 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002335 // Implement double negation with an exclusive or with value
2336 // 0x8000000000000000 (mask for bit 63, representing the sign of
2337 // a double-precision floating-point number).
2338 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002339 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002340 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002341 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002342
2343 default:
2344 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2345 }
2346}
2347
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002348void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2349 LocationSummary* locations =
2350 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2351 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2352 locations->SetInAt(0, Location::RequiresFpuRegister());
2353 locations->SetInAt(1, Location::RequiresRegister());
2354 locations->SetOut(Location::SameAsFirstInput());
2355 locations->AddTemp(Location::RequiresFpuRegister());
2356}
2357
2358void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2359 LocationSummary* locations = neg->GetLocations();
2360 Location out = locations->Out();
2361 DCHECK(locations->InAt(0).Equals(out));
2362
2363 Register constant_area = locations->InAt(1).AsRegister<Register>();
2364 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2365 if (neg->GetType() == Primitive::kPrimFloat) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002366 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2367 neg->GetBaseMethodAddress(),
2368 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002369 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2370 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002371 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2372 neg->GetBaseMethodAddress(),
2373 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002374 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2375 }
2376}
2377
Roland Levillaindff1f282014-11-05 14:15:05 +00002378void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002379 Primitive::Type result_type = conversion->GetResultType();
2380 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002381 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002382
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002383 // The float-to-long and double-to-long type conversions rely on a
2384 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002385 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002386 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2387 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002388 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002389 : LocationSummary::kNoCall;
2390 LocationSummary* locations =
2391 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2392
David Brazdilb2bd1c52015-03-25 11:17:37 +00002393 // The Java language does not allow treating boolean as an integral type but
2394 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002395
Roland Levillaindff1f282014-11-05 14:15:05 +00002396 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002397 case Primitive::kPrimByte:
2398 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002399 case Primitive::kPrimLong: {
2400 // Type conversion from long to byte is a result of code transformations.
2401 HInstruction* input = conversion->InputAt(0);
2402 Location input_location = input->IsConstant()
2403 ? Location::ConstantLocation(input->AsConstant())
2404 : Location::RegisterPairLocation(EAX, EDX);
2405 locations->SetInAt(0, input_location);
2406 // Make the output overlap to please the register allocator. This greatly simplifies
2407 // the validation of the linear scan implementation
2408 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2409 break;
2410 }
David Brazdil46e2a392015-03-16 17:31:52 +00002411 case Primitive::kPrimBoolean:
2412 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002413 case Primitive::kPrimShort:
2414 case Primitive::kPrimInt:
2415 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002416 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002417 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2418 // Make the output overlap to please the register allocator. This greatly simplifies
2419 // the validation of the linear scan implementation
2420 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002421 break;
2422
2423 default:
2424 LOG(FATAL) << "Unexpected type conversion from " << input_type
2425 << " to " << result_type;
2426 }
2427 break;
2428
Roland Levillain01a8d712014-11-14 16:27:39 +00002429 case Primitive::kPrimShort:
2430 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002431 case Primitive::kPrimLong:
2432 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002433 case Primitive::kPrimBoolean:
2434 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002435 case Primitive::kPrimByte:
2436 case Primitive::kPrimInt:
2437 case Primitive::kPrimChar:
2438 // Processing a Dex `int-to-short' instruction.
2439 locations->SetInAt(0, Location::Any());
2440 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2441 break;
2442
2443 default:
2444 LOG(FATAL) << "Unexpected type conversion from " << input_type
2445 << " to " << result_type;
2446 }
2447 break;
2448
Roland Levillain946e1432014-11-11 17:35:19 +00002449 case Primitive::kPrimInt:
2450 switch (input_type) {
2451 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002452 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002453 locations->SetInAt(0, Location::Any());
2454 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2455 break;
2456
2457 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002458 // Processing a Dex `float-to-int' instruction.
2459 locations->SetInAt(0, Location::RequiresFpuRegister());
2460 locations->SetOut(Location::RequiresRegister());
2461 locations->AddTemp(Location::RequiresFpuRegister());
2462 break;
2463
Roland Levillain946e1432014-11-11 17:35:19 +00002464 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002465 // Processing a Dex `double-to-int' instruction.
2466 locations->SetInAt(0, Location::RequiresFpuRegister());
2467 locations->SetOut(Location::RequiresRegister());
2468 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002469 break;
2470
2471 default:
2472 LOG(FATAL) << "Unexpected type conversion from " << input_type
2473 << " to " << result_type;
2474 }
2475 break;
2476
Roland Levillaindff1f282014-11-05 14:15:05 +00002477 case Primitive::kPrimLong:
2478 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002479 case Primitive::kPrimBoolean:
2480 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002481 case Primitive::kPrimByte:
2482 case Primitive::kPrimShort:
2483 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002484 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002485 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002486 locations->SetInAt(0, Location::RegisterLocation(EAX));
2487 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2488 break;
2489
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002490 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002491 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002492 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002493 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002494 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2495 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2496
Vladimir Marko949c91f2015-01-27 10:48:44 +00002497 // The runtime helper puts the result in EAX, EDX.
2498 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002499 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002500 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002501
2502 default:
2503 LOG(FATAL) << "Unexpected type conversion from " << input_type
2504 << " to " << result_type;
2505 }
2506 break;
2507
Roland Levillain981e4542014-11-14 11:47:14 +00002508 case Primitive::kPrimChar:
2509 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002510 case Primitive::kPrimLong:
2511 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002512 case Primitive::kPrimBoolean:
2513 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002514 case Primitive::kPrimByte:
2515 case Primitive::kPrimShort:
2516 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002517 // Processing a Dex `int-to-char' instruction.
2518 locations->SetInAt(0, Location::Any());
2519 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2520 break;
2521
2522 default:
2523 LOG(FATAL) << "Unexpected type conversion from " << input_type
2524 << " to " << result_type;
2525 }
2526 break;
2527
Roland Levillaindff1f282014-11-05 14:15:05 +00002528 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002529 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002530 case Primitive::kPrimBoolean:
2531 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002532 case Primitive::kPrimByte:
2533 case Primitive::kPrimShort:
2534 case Primitive::kPrimInt:
2535 case Primitive::kPrimChar:
2536 // Processing a Dex `int-to-float' instruction.
2537 locations->SetInAt(0, Location::RequiresRegister());
2538 locations->SetOut(Location::RequiresFpuRegister());
2539 break;
2540
2541 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002542 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002543 locations->SetInAt(0, Location::Any());
2544 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002545 break;
2546
Roland Levillaincff13742014-11-17 14:32:17 +00002547 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002548 // Processing a Dex `double-to-float' instruction.
2549 locations->SetInAt(0, Location::RequiresFpuRegister());
2550 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002551 break;
2552
2553 default:
2554 LOG(FATAL) << "Unexpected type conversion from " << input_type
2555 << " to " << result_type;
2556 };
2557 break;
2558
Roland Levillaindff1f282014-11-05 14:15:05 +00002559 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002560 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002561 case Primitive::kPrimBoolean:
2562 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002563 case Primitive::kPrimByte:
2564 case Primitive::kPrimShort:
2565 case Primitive::kPrimInt:
2566 case Primitive::kPrimChar:
2567 // Processing a Dex `int-to-double' instruction.
2568 locations->SetInAt(0, Location::RequiresRegister());
2569 locations->SetOut(Location::RequiresFpuRegister());
2570 break;
2571
2572 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002573 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002574 locations->SetInAt(0, Location::Any());
2575 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002576 break;
2577
Roland Levillaincff13742014-11-17 14:32:17 +00002578 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002579 // Processing a Dex `float-to-double' instruction.
2580 locations->SetInAt(0, Location::RequiresFpuRegister());
2581 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002582 break;
2583
2584 default:
2585 LOG(FATAL) << "Unexpected type conversion from " << input_type
2586 << " to " << result_type;
2587 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002588 break;
2589
2590 default:
2591 LOG(FATAL) << "Unexpected type conversion from " << input_type
2592 << " to " << result_type;
2593 }
2594}
2595
2596void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2597 LocationSummary* locations = conversion->GetLocations();
2598 Location out = locations->Out();
2599 Location in = locations->InAt(0);
2600 Primitive::Type result_type = conversion->GetResultType();
2601 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002602 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002603 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002604 case Primitive::kPrimByte:
2605 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002606 case Primitive::kPrimLong:
2607 // Type conversion from long to byte is a result of code transformations.
2608 if (in.IsRegisterPair()) {
2609 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2610 } else {
2611 DCHECK(in.GetConstant()->IsLongConstant());
2612 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2613 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2614 }
2615 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002616 case Primitive::kPrimBoolean:
2617 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002618 case Primitive::kPrimShort:
2619 case Primitive::kPrimInt:
2620 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002621 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002622 if (in.IsRegister()) {
2623 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002624 } else {
2625 DCHECK(in.GetConstant()->IsIntConstant());
2626 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2627 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2628 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002629 break;
2630
2631 default:
2632 LOG(FATAL) << "Unexpected type conversion from " << input_type
2633 << " to " << result_type;
2634 }
2635 break;
2636
Roland Levillain01a8d712014-11-14 16:27:39 +00002637 case Primitive::kPrimShort:
2638 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002639 case Primitive::kPrimLong:
2640 // Type conversion from long to short is a result of code transformations.
2641 if (in.IsRegisterPair()) {
2642 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2643 } else if (in.IsDoubleStackSlot()) {
2644 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2645 } else {
2646 DCHECK(in.GetConstant()->IsLongConstant());
2647 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2648 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2649 }
2650 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002651 case Primitive::kPrimBoolean:
2652 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002653 case Primitive::kPrimByte:
2654 case Primitive::kPrimInt:
2655 case Primitive::kPrimChar:
2656 // Processing a Dex `int-to-short' instruction.
2657 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002658 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002659 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002660 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002661 } else {
2662 DCHECK(in.GetConstant()->IsIntConstant());
2663 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002664 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002665 }
2666 break;
2667
2668 default:
2669 LOG(FATAL) << "Unexpected type conversion from " << input_type
2670 << " to " << result_type;
2671 }
2672 break;
2673
Roland Levillain946e1432014-11-11 17:35:19 +00002674 case Primitive::kPrimInt:
2675 switch (input_type) {
2676 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002677 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002678 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002679 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002680 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002681 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002682 } else {
2683 DCHECK(in.IsConstant());
2684 DCHECK(in.GetConstant()->IsLongConstant());
2685 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002686 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002687 }
2688 break;
2689
Roland Levillain3f8f9362014-12-02 17:45:01 +00002690 case Primitive::kPrimFloat: {
2691 // Processing a Dex `float-to-int' instruction.
2692 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2693 Register output = out.AsRegister<Register>();
2694 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002695 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002696
2697 __ movl(output, Immediate(kPrimIntMax));
2698 // temp = int-to-float(output)
2699 __ cvtsi2ss(temp, output);
2700 // if input >= temp goto done
2701 __ comiss(input, temp);
2702 __ j(kAboveEqual, &done);
2703 // if input == NaN goto nan
2704 __ j(kUnordered, &nan);
2705 // output = float-to-int-truncate(input)
2706 __ cvttss2si(output, input);
2707 __ jmp(&done);
2708 __ Bind(&nan);
2709 // output = 0
2710 __ xorl(output, output);
2711 __ Bind(&done);
2712 break;
2713 }
2714
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002715 case Primitive::kPrimDouble: {
2716 // Processing a Dex `double-to-int' instruction.
2717 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2718 Register output = out.AsRegister<Register>();
2719 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002720 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002721
2722 __ movl(output, Immediate(kPrimIntMax));
2723 // temp = int-to-double(output)
2724 __ cvtsi2sd(temp, output);
2725 // if input >= temp goto done
2726 __ comisd(input, temp);
2727 __ j(kAboveEqual, &done);
2728 // if input == NaN goto nan
2729 __ j(kUnordered, &nan);
2730 // output = double-to-int-truncate(input)
2731 __ cvttsd2si(output, input);
2732 __ jmp(&done);
2733 __ Bind(&nan);
2734 // output = 0
2735 __ xorl(output, output);
2736 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002737 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002738 }
Roland Levillain946e1432014-11-11 17:35:19 +00002739
2740 default:
2741 LOG(FATAL) << "Unexpected type conversion from " << input_type
2742 << " to " << result_type;
2743 }
2744 break;
2745
Roland Levillaindff1f282014-11-05 14:15:05 +00002746 case Primitive::kPrimLong:
2747 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002748 case Primitive::kPrimBoolean:
2749 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002750 case Primitive::kPrimByte:
2751 case Primitive::kPrimShort:
2752 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002753 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002754 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002755 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2756 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002757 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002758 __ cdq();
2759 break;
2760
2761 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002762 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002763 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002764 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002765 break;
2766
Roland Levillaindff1f282014-11-05 14:15:05 +00002767 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002768 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002769 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002770 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002771 break;
2772
2773 default:
2774 LOG(FATAL) << "Unexpected type conversion from " << input_type
2775 << " to " << result_type;
2776 }
2777 break;
2778
Roland Levillain981e4542014-11-14 11:47:14 +00002779 case Primitive::kPrimChar:
2780 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002781 case Primitive::kPrimLong:
2782 // Type conversion from long to short is a result of code transformations.
2783 if (in.IsRegisterPair()) {
2784 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2785 } else if (in.IsDoubleStackSlot()) {
2786 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2787 } else {
2788 DCHECK(in.GetConstant()->IsLongConstant());
2789 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2790 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2791 }
2792 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002793 case Primitive::kPrimBoolean:
2794 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002795 case Primitive::kPrimByte:
2796 case Primitive::kPrimShort:
2797 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002798 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2799 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002800 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002801 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002802 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002803 } else {
2804 DCHECK(in.GetConstant()->IsIntConstant());
2805 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002806 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002807 }
2808 break;
2809
2810 default:
2811 LOG(FATAL) << "Unexpected type conversion from " << input_type
2812 << " to " << result_type;
2813 }
2814 break;
2815
Roland Levillaindff1f282014-11-05 14:15:05 +00002816 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002817 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002818 case Primitive::kPrimBoolean:
2819 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002820 case Primitive::kPrimByte:
2821 case Primitive::kPrimShort:
2822 case Primitive::kPrimInt:
2823 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002824 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002825 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002826 break;
2827
Roland Levillain6d0e4832014-11-27 18:31:21 +00002828 case Primitive::kPrimLong: {
2829 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002830 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002831
Roland Levillain232ade02015-04-20 15:14:36 +01002832 // Create stack space for the call to
2833 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2834 // TODO: enhance register allocator to ask for stack temporaries.
2835 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2836 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2837 __ subl(ESP, Immediate(adjustment));
2838 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002839
Roland Levillain232ade02015-04-20 15:14:36 +01002840 // Load the value to the FP stack, using temporaries if needed.
2841 PushOntoFPStack(in, 0, adjustment, false, true);
2842
2843 if (out.IsStackSlot()) {
2844 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2845 } else {
2846 __ fstps(Address(ESP, 0));
2847 Location stack_temp = Location::StackSlot(0);
2848 codegen_->Move32(out, stack_temp);
2849 }
2850
2851 // Remove the temporary stack space we allocated.
2852 if (adjustment != 0) {
2853 __ addl(ESP, Immediate(adjustment));
2854 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002855 break;
2856 }
2857
Roland Levillaincff13742014-11-17 14:32:17 +00002858 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002859 // Processing a Dex `double-to-float' instruction.
2860 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002861 break;
2862
2863 default:
2864 LOG(FATAL) << "Unexpected type conversion from " << input_type
2865 << " to " << result_type;
2866 };
2867 break;
2868
Roland Levillaindff1f282014-11-05 14:15:05 +00002869 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002870 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002871 case Primitive::kPrimBoolean:
2872 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002873 case Primitive::kPrimByte:
2874 case Primitive::kPrimShort:
2875 case Primitive::kPrimInt:
2876 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002877 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002878 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002879 break;
2880
Roland Levillain647b9ed2014-11-27 12:06:00 +00002881 case Primitive::kPrimLong: {
2882 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002883 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002884
Roland Levillain232ade02015-04-20 15:14:36 +01002885 // Create stack space for the call to
2886 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2887 // TODO: enhance register allocator to ask for stack temporaries.
2888 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2889 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2890 __ subl(ESP, Immediate(adjustment));
2891 }
2892
2893 // Load the value to the FP stack, using temporaries if needed.
2894 PushOntoFPStack(in, 0, adjustment, false, true);
2895
2896 if (out.IsDoubleStackSlot()) {
2897 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2898 } else {
2899 __ fstpl(Address(ESP, 0));
2900 Location stack_temp = Location::DoubleStackSlot(0);
2901 codegen_->Move64(out, stack_temp);
2902 }
2903
2904 // Remove the temporary stack space we allocated.
2905 if (adjustment != 0) {
2906 __ addl(ESP, Immediate(adjustment));
2907 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002908 break;
2909 }
2910
Roland Levillaincff13742014-11-17 14:32:17 +00002911 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002912 // Processing a Dex `float-to-double' instruction.
2913 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002914 break;
2915
2916 default:
2917 LOG(FATAL) << "Unexpected type conversion from " << input_type
2918 << " to " << result_type;
2919 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002920 break;
2921
2922 default:
2923 LOG(FATAL) << "Unexpected type conversion from " << input_type
2924 << " to " << result_type;
2925 }
2926}
2927
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002928void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002929 LocationSummary* locations =
2930 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002931 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002932 case Primitive::kPrimInt: {
2933 locations->SetInAt(0, Location::RequiresRegister());
2934 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2935 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2936 break;
2937 }
2938
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002939 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002940 locations->SetInAt(0, Location::RequiresRegister());
2941 locations->SetInAt(1, Location::Any());
2942 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002943 break;
2944 }
2945
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002946 case Primitive::kPrimFloat:
2947 case Primitive::kPrimDouble: {
2948 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002949 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2950 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002951 } else if (add->InputAt(1)->IsConstant()) {
2952 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002953 } else {
2954 locations->SetInAt(1, Location::Any());
2955 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002956 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002957 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002958 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002959
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002960 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002961 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2962 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002963 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002964}
2965
2966void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2967 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002968 Location first = locations->InAt(0);
2969 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002970 Location out = locations->Out();
2971
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002972 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002973 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002974 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002975 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2976 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002977 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2978 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002979 } else {
2980 __ leal(out.AsRegister<Register>(), Address(
2981 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2982 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002983 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002984 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2985 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2986 __ addl(out.AsRegister<Register>(), Immediate(value));
2987 } else {
2988 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2989 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002990 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002991 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002992 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002993 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002994 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002995 }
2996
2997 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002998 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002999 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3000 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003001 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003002 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3003 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003004 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003005 } else {
3006 DCHECK(second.IsConstant()) << second;
3007 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3008 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3009 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003010 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003011 break;
3012 }
3013
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003014 case Primitive::kPrimFloat: {
3015 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003016 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003017 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3018 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003019 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003020 __ addss(first.AsFpuRegister<XmmRegister>(),
3021 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003022 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3023 const_area->GetBaseMethodAddress(),
3024 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003025 } else {
3026 DCHECK(second.IsStackSlot());
3027 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003028 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003029 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003030 }
3031
3032 case Primitive::kPrimDouble: {
3033 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003034 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003035 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3036 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003037 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003038 __ addsd(first.AsFpuRegister<XmmRegister>(),
3039 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003040 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3041 const_area->GetBaseMethodAddress(),
3042 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003043 } else {
3044 DCHECK(second.IsDoubleStackSlot());
3045 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003046 }
3047 break;
3048 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003049
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003050 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003051 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003052 }
3053}
3054
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003055void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003056 LocationSummary* locations =
3057 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003058 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003059 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003060 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003061 locations->SetInAt(0, Location::RequiresRegister());
3062 locations->SetInAt(1, Location::Any());
3063 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003064 break;
3065 }
Calin Juravle11351682014-10-23 15:38:15 +01003066 case Primitive::kPrimFloat:
3067 case Primitive::kPrimDouble: {
3068 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003069 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3070 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003071 } else if (sub->InputAt(1)->IsConstant()) {
3072 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003073 } else {
3074 locations->SetInAt(1, Location::Any());
3075 }
Calin Juravle11351682014-10-23 15:38:15 +01003076 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003077 break;
Calin Juravle11351682014-10-23 15:38:15 +01003078 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003079
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003080 default:
Calin Juravle11351682014-10-23 15:38:15 +01003081 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003082 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003083}
3084
3085void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3086 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003087 Location first = locations->InAt(0);
3088 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003089 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003090 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003091 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003092 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003093 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003094 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003095 __ subl(first.AsRegister<Register>(),
3096 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003097 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003098 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003099 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003100 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003101 }
3102
3103 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003104 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003105 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3106 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003107 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003108 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003109 __ sbbl(first.AsRegisterPairHigh<Register>(),
3110 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003111 } else {
3112 DCHECK(second.IsConstant()) << second;
3113 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3114 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3115 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003116 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003117 break;
3118 }
3119
Calin Juravle11351682014-10-23 15:38:15 +01003120 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003121 if (second.IsFpuRegister()) {
3122 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3123 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3124 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003125 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003126 __ subss(first.AsFpuRegister<XmmRegister>(),
3127 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003128 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3129 const_area->GetBaseMethodAddress(),
3130 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003131 } else {
3132 DCHECK(second.IsStackSlot());
3133 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3134 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003135 break;
Calin Juravle11351682014-10-23 15:38:15 +01003136 }
3137
3138 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003139 if (second.IsFpuRegister()) {
3140 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3141 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3142 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003143 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003144 __ subsd(first.AsFpuRegister<XmmRegister>(),
3145 codegen_->LiteralDoubleAddress(
3146 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003147 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003148 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3149 } else {
3150 DCHECK(second.IsDoubleStackSlot());
3151 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3152 }
Calin Juravle11351682014-10-23 15:38:15 +01003153 break;
3154 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003155
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003156 default:
Calin Juravle11351682014-10-23 15:38:15 +01003157 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003158 }
3159}
3160
Calin Juravle34bacdf2014-10-07 20:23:36 +01003161void LocationsBuilderX86::VisitMul(HMul* mul) {
3162 LocationSummary* locations =
3163 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3164 switch (mul->GetResultType()) {
3165 case Primitive::kPrimInt:
3166 locations->SetInAt(0, Location::RequiresRegister());
3167 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003168 if (mul->InputAt(1)->IsIntConstant()) {
3169 // Can use 3 operand multiply.
3170 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3171 } else {
3172 locations->SetOut(Location::SameAsFirstInput());
3173 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003174 break;
3175 case Primitive::kPrimLong: {
3176 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003177 locations->SetInAt(1, Location::Any());
3178 locations->SetOut(Location::SameAsFirstInput());
3179 // Needed for imul on 32bits with 64bits output.
3180 locations->AddTemp(Location::RegisterLocation(EAX));
3181 locations->AddTemp(Location::RegisterLocation(EDX));
3182 break;
3183 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003184 case Primitive::kPrimFloat:
3185 case Primitive::kPrimDouble: {
3186 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003187 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3188 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003189 } else if (mul->InputAt(1)->IsConstant()) {
3190 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003191 } else {
3192 locations->SetInAt(1, Location::Any());
3193 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003194 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003195 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003196 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003197
3198 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003199 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003200 }
3201}
3202
3203void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3204 LocationSummary* locations = mul->GetLocations();
3205 Location first = locations->InAt(0);
3206 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003207 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003208
3209 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003210 case Primitive::kPrimInt:
3211 // The constant may have ended up in a register, so test explicitly to avoid
3212 // problems where the output may not be the same as the first operand.
3213 if (mul->InputAt(1)->IsIntConstant()) {
3214 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3215 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3216 } else if (second.IsRegister()) {
3217 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003218 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003219 } else {
3220 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003221 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003222 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003223 }
3224 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003225
3226 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003227 Register in1_hi = first.AsRegisterPairHigh<Register>();
3228 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003229 Register eax = locations->GetTemp(0).AsRegister<Register>();
3230 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003231
3232 DCHECK_EQ(EAX, eax);
3233 DCHECK_EQ(EDX, edx);
3234
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003235 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003236 // output: in1
3237 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3238 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3239 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003240 if (second.IsConstant()) {
3241 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003242
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003243 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3244 int32_t low_value = Low32Bits(value);
3245 int32_t high_value = High32Bits(value);
3246 Immediate low(low_value);
3247 Immediate high(high_value);
3248
3249 __ movl(eax, high);
3250 // eax <- in1.lo * in2.hi
3251 __ imull(eax, in1_lo);
3252 // in1.hi <- in1.hi * in2.lo
3253 __ imull(in1_hi, low);
3254 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3255 __ addl(in1_hi, eax);
3256 // move in2_lo to eax to prepare for double precision
3257 __ movl(eax, low);
3258 // edx:eax <- in1.lo * in2.lo
3259 __ mull(in1_lo);
3260 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3261 __ addl(in1_hi, edx);
3262 // in1.lo <- (in1.lo * in2.lo)[31:0];
3263 __ movl(in1_lo, eax);
3264 } else if (second.IsRegisterPair()) {
3265 Register in2_hi = second.AsRegisterPairHigh<Register>();
3266 Register in2_lo = second.AsRegisterPairLow<Register>();
3267
3268 __ movl(eax, in2_hi);
3269 // eax <- in1.lo * in2.hi
3270 __ imull(eax, in1_lo);
3271 // in1.hi <- in1.hi * in2.lo
3272 __ imull(in1_hi, in2_lo);
3273 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3274 __ addl(in1_hi, eax);
3275 // move in1_lo to eax to prepare for double precision
3276 __ movl(eax, in1_lo);
3277 // edx:eax <- in1.lo * in2.lo
3278 __ mull(in2_lo);
3279 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3280 __ addl(in1_hi, edx);
3281 // in1.lo <- (in1.lo * in2.lo)[31:0];
3282 __ movl(in1_lo, eax);
3283 } else {
3284 DCHECK(second.IsDoubleStackSlot()) << second;
3285 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3286 Address in2_lo(ESP, second.GetStackIndex());
3287
3288 __ movl(eax, in2_hi);
3289 // eax <- in1.lo * in2.hi
3290 __ imull(eax, in1_lo);
3291 // in1.hi <- in1.hi * in2.lo
3292 __ imull(in1_hi, in2_lo);
3293 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3294 __ addl(in1_hi, eax);
3295 // move in1_lo to eax to prepare for double precision
3296 __ movl(eax, in1_lo);
3297 // edx:eax <- in1.lo * in2.lo
3298 __ mull(in2_lo);
3299 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3300 __ addl(in1_hi, edx);
3301 // in1.lo <- (in1.lo * in2.lo)[31:0];
3302 __ movl(in1_lo, eax);
3303 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003304
3305 break;
3306 }
3307
Calin Juravleb5bfa962014-10-21 18:02:24 +01003308 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003309 DCHECK(first.Equals(locations->Out()));
3310 if (second.IsFpuRegister()) {
3311 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3312 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3313 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003314 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003315 __ mulss(first.AsFpuRegister<XmmRegister>(),
3316 codegen_->LiteralFloatAddress(
3317 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003318 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003319 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3320 } else {
3321 DCHECK(second.IsStackSlot());
3322 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3323 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003324 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003325 }
3326
3327 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003328 DCHECK(first.Equals(locations->Out()));
3329 if (second.IsFpuRegister()) {
3330 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3331 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3332 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003333 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003334 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3335 codegen_->LiteralDoubleAddress(
3336 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003337 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003338 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3339 } else {
3340 DCHECK(second.IsDoubleStackSlot());
3341 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3342 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003343 break;
3344 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003345
3346 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003347 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003348 }
3349}
3350
Roland Levillain232ade02015-04-20 15:14:36 +01003351void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3352 uint32_t temp_offset,
3353 uint32_t stack_adjustment,
3354 bool is_fp,
3355 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003356 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003357 DCHECK(!is_wide);
3358 if (is_fp) {
3359 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3360 } else {
3361 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3362 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003363 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003364 DCHECK(is_wide);
3365 if (is_fp) {
3366 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3367 } else {
3368 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3369 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003370 } else {
3371 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003372 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003373 Location stack_temp = Location::StackSlot(temp_offset);
3374 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003375 if (is_fp) {
3376 __ flds(Address(ESP, temp_offset));
3377 } else {
3378 __ filds(Address(ESP, temp_offset));
3379 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003380 } else {
3381 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3382 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003383 if (is_fp) {
3384 __ fldl(Address(ESP, temp_offset));
3385 } else {
3386 __ fildl(Address(ESP, temp_offset));
3387 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003388 }
3389 }
3390}
3391
3392void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3393 Primitive::Type type = rem->GetResultType();
3394 bool is_float = type == Primitive::kPrimFloat;
3395 size_t elem_size = Primitive::ComponentSize(type);
3396 LocationSummary* locations = rem->GetLocations();
3397 Location first = locations->InAt(0);
3398 Location second = locations->InAt(1);
3399 Location out = locations->Out();
3400
3401 // Create stack space for 2 elements.
3402 // TODO: enhance register allocator to ask for stack temporaries.
3403 __ subl(ESP, Immediate(2 * elem_size));
3404
3405 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003406 const bool is_wide = !is_float;
3407 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3408 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003409
3410 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003411 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003412 __ Bind(&retry);
3413 __ fprem();
3414
3415 // Move FP status to AX.
3416 __ fstsw();
3417
3418 // And see if the argument reduction is complete. This is signaled by the
3419 // C2 FPU flag bit set to 0.
3420 __ andl(EAX, Immediate(kC2ConditionMask));
3421 __ j(kNotEqual, &retry);
3422
3423 // We have settled on the final value. Retrieve it into an XMM register.
3424 // Store FP top of stack to real stack.
3425 if (is_float) {
3426 __ fsts(Address(ESP, 0));
3427 } else {
3428 __ fstl(Address(ESP, 0));
3429 }
3430
3431 // Pop the 2 items from the FP stack.
3432 __ fucompp();
3433
3434 // Load the value from the stack into an XMM register.
3435 DCHECK(out.IsFpuRegister()) << out;
3436 if (is_float) {
3437 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3438 } else {
3439 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3440 }
3441
3442 // And remove the temporary stack space we allocated.
3443 __ addl(ESP, Immediate(2 * elem_size));
3444}
3445
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003446
3447void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3448 DCHECK(instruction->IsDiv() || instruction->IsRem());
3449
3450 LocationSummary* locations = instruction->GetLocations();
3451 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003452 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003453
3454 Register out_register = locations->Out().AsRegister<Register>();
3455 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003456 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003457
3458 DCHECK(imm == 1 || imm == -1);
3459
3460 if (instruction->IsRem()) {
3461 __ xorl(out_register, out_register);
3462 } else {
3463 __ movl(out_register, input_register);
3464 if (imm == -1) {
3465 __ negl(out_register);
3466 }
3467 }
3468}
3469
3470
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003471void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003472 LocationSummary* locations = instruction->GetLocations();
3473
3474 Register out_register = locations->Out().AsRegister<Register>();
3475 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003476 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003477 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3478 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003479
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003480 Register num = locations->GetTemp(0).AsRegister<Register>();
3481
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003482 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003483 __ testl(input_register, input_register);
3484 __ cmovl(kGreaterEqual, num, input_register);
3485 int shift = CTZ(imm);
3486 __ sarl(num, Immediate(shift));
3487
3488 if (imm < 0) {
3489 __ negl(num);
3490 }
3491
3492 __ movl(out_register, num);
3493}
3494
3495void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3496 DCHECK(instruction->IsDiv() || instruction->IsRem());
3497
3498 LocationSummary* locations = instruction->GetLocations();
3499 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3500
3501 Register eax = locations->InAt(0).AsRegister<Register>();
3502 Register out = locations->Out().AsRegister<Register>();
3503 Register num;
3504 Register edx;
3505
3506 if (instruction->IsDiv()) {
3507 edx = locations->GetTemp(0).AsRegister<Register>();
3508 num = locations->GetTemp(1).AsRegister<Register>();
3509 } else {
3510 edx = locations->Out().AsRegister<Register>();
3511 num = locations->GetTemp(0).AsRegister<Register>();
3512 }
3513
3514 DCHECK_EQ(EAX, eax);
3515 DCHECK_EQ(EDX, edx);
3516 if (instruction->IsDiv()) {
3517 DCHECK_EQ(EAX, out);
3518 } else {
3519 DCHECK_EQ(EDX, out);
3520 }
3521
3522 int64_t magic;
3523 int shift;
3524 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3525
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003526 // Save the numerator.
3527 __ movl(num, eax);
3528
3529 // EAX = magic
3530 __ movl(eax, Immediate(magic));
3531
3532 // EDX:EAX = magic * numerator
3533 __ imull(num);
3534
3535 if (imm > 0 && magic < 0) {
3536 // EDX += num
3537 __ addl(edx, num);
3538 } else if (imm < 0 && magic > 0) {
3539 __ subl(edx, num);
3540 }
3541
3542 // Shift if needed.
3543 if (shift != 0) {
3544 __ sarl(edx, Immediate(shift));
3545 }
3546
3547 // EDX += 1 if EDX < 0
3548 __ movl(eax, edx);
3549 __ shrl(edx, Immediate(31));
3550 __ addl(edx, eax);
3551
3552 if (instruction->IsRem()) {
3553 __ movl(eax, num);
3554 __ imull(edx, Immediate(imm));
3555 __ subl(eax, edx);
3556 __ movl(edx, eax);
3557 } else {
3558 __ movl(eax, edx);
3559 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003560}
3561
Calin Juravlebacfec32014-11-14 15:54:36 +00003562void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3563 DCHECK(instruction->IsDiv() || instruction->IsRem());
3564
3565 LocationSummary* locations = instruction->GetLocations();
3566 Location out = locations->Out();
3567 Location first = locations->InAt(0);
3568 Location second = locations->InAt(1);
3569 bool is_div = instruction->IsDiv();
3570
3571 switch (instruction->GetResultType()) {
3572 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003573 DCHECK_EQ(EAX, first.AsRegister<Register>());
3574 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003575
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003576 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003577 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003578
3579 if (imm == 0) {
3580 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3581 } else if (imm == 1 || imm == -1) {
3582 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003583 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003584 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003585 } else {
3586 DCHECK(imm <= -2 || imm >= 2);
3587 GenerateDivRemWithAnyConstant(instruction);
3588 }
3589 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003590 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3591 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003592 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003593
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003594 Register second_reg = second.AsRegister<Register>();
3595 // 0x80000000/-1 triggers an arithmetic exception!
3596 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3597 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003598
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003599 __ cmpl(second_reg, Immediate(-1));
3600 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003601
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003602 // edx:eax <- sign-extended of eax
3603 __ cdq();
3604 // eax = quotient, edx = remainder
3605 __ idivl(second_reg);
3606 __ Bind(slow_path->GetExitLabel());
3607 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003608 break;
3609 }
3610
3611 case Primitive::kPrimLong: {
3612 InvokeRuntimeCallingConvention calling_convention;
3613 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3614 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3615 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3616 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3617 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3618 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3619
3620 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003621 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003622 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003623 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003624 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003625 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003626 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003627 break;
3628 }
3629
3630 default:
3631 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3632 }
3633}
3634
Calin Juravle7c4954d2014-10-28 16:57:40 +00003635void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003636 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003637 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003638 : LocationSummary::kNoCall;
3639 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3640
Calin Juravle7c4954d2014-10-28 16:57:40 +00003641 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003642 case Primitive::kPrimInt: {
3643 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003644 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003645 locations->SetOut(Location::SameAsFirstInput());
3646 // Intel uses edx:eax as the dividend.
3647 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003648 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3649 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3650 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003651 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003652 locations->AddTemp(Location::RequiresRegister());
3653 }
Calin Juravled0d48522014-11-04 16:40:20 +00003654 break;
3655 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003656 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003657 InvokeRuntimeCallingConvention calling_convention;
3658 locations->SetInAt(0, Location::RegisterPairLocation(
3659 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3660 locations->SetInAt(1, Location::RegisterPairLocation(
3661 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3662 // Runtime helper puts the result in EAX, EDX.
3663 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003664 break;
3665 }
3666 case Primitive::kPrimFloat:
3667 case Primitive::kPrimDouble: {
3668 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003669 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3670 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003671 } else if (div->InputAt(1)->IsConstant()) {
3672 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003673 } else {
3674 locations->SetInAt(1, Location::Any());
3675 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003676 locations->SetOut(Location::SameAsFirstInput());
3677 break;
3678 }
3679
3680 default:
3681 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3682 }
3683}
3684
3685void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3686 LocationSummary* locations = div->GetLocations();
3687 Location first = locations->InAt(0);
3688 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003689
3690 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003691 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003692 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003693 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003694 break;
3695 }
3696
3697 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003698 if (second.IsFpuRegister()) {
3699 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3700 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3701 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003702 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003703 __ divss(first.AsFpuRegister<XmmRegister>(),
3704 codegen_->LiteralFloatAddress(
3705 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003706 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003707 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3708 } else {
3709 DCHECK(second.IsStackSlot());
3710 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3711 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003712 break;
3713 }
3714
3715 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003716 if (second.IsFpuRegister()) {
3717 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3718 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3719 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003720 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003721 __ divsd(first.AsFpuRegister<XmmRegister>(),
3722 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003723 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3724 const_area->GetBaseMethodAddress(),
3725 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003726 } else {
3727 DCHECK(second.IsDoubleStackSlot());
3728 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3729 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003730 break;
3731 }
3732
3733 default:
3734 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3735 }
3736}
3737
Calin Juravlebacfec32014-11-14 15:54:36 +00003738void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003739 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003740
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003741 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003742 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003743 : LocationSummary::kNoCall;
3744 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003745
Calin Juravled2ec87d2014-12-08 14:24:46 +00003746 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003747 case Primitive::kPrimInt: {
3748 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003749 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003750 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003751 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3752 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3753 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003754 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003755 locations->AddTemp(Location::RequiresRegister());
3756 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003757 break;
3758 }
3759 case Primitive::kPrimLong: {
3760 InvokeRuntimeCallingConvention calling_convention;
3761 locations->SetInAt(0, Location::RegisterPairLocation(
3762 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3763 locations->SetInAt(1, Location::RegisterPairLocation(
3764 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3765 // Runtime helper puts the result in EAX, EDX.
3766 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3767 break;
3768 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003769 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003770 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003771 locations->SetInAt(0, Location::Any());
3772 locations->SetInAt(1, Location::Any());
3773 locations->SetOut(Location::RequiresFpuRegister());
3774 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003775 break;
3776 }
3777
3778 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003779 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003780 }
3781}
3782
3783void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3784 Primitive::Type type = rem->GetResultType();
3785 switch (type) {
3786 case Primitive::kPrimInt:
3787 case Primitive::kPrimLong: {
3788 GenerateDivRemIntegral(rem);
3789 break;
3790 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003791 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003792 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003793 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003794 break;
3795 }
3796 default:
3797 LOG(FATAL) << "Unexpected rem type " << type;
3798 }
3799}
3800
Calin Juravled0d48522014-11-04 16:40:20 +00003801void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003802 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003803 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003804 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003805 case Primitive::kPrimByte:
3806 case Primitive::kPrimChar:
3807 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003808 case Primitive::kPrimInt: {
3809 locations->SetInAt(0, Location::Any());
3810 break;
3811 }
3812 case Primitive::kPrimLong: {
3813 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3814 if (!instruction->IsConstant()) {
3815 locations->AddTemp(Location::RequiresRegister());
3816 }
3817 break;
3818 }
3819 default:
3820 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3821 }
Calin Juravled0d48522014-11-04 16:40:20 +00003822}
3823
3824void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003825 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003826 codegen_->AddSlowPath(slow_path);
3827
3828 LocationSummary* locations = instruction->GetLocations();
3829 Location value = locations->InAt(0);
3830
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003831 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003832 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003833 case Primitive::kPrimByte:
3834 case Primitive::kPrimChar:
3835 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003836 case Primitive::kPrimInt: {
3837 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003838 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003839 __ j(kEqual, slow_path->GetEntryLabel());
3840 } else if (value.IsStackSlot()) {
3841 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3842 __ j(kEqual, slow_path->GetEntryLabel());
3843 } else {
3844 DCHECK(value.IsConstant()) << value;
3845 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003846 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003847 }
3848 }
3849 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003850 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003851 case Primitive::kPrimLong: {
3852 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003853 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003854 __ movl(temp, value.AsRegisterPairLow<Register>());
3855 __ orl(temp, value.AsRegisterPairHigh<Register>());
3856 __ j(kEqual, slow_path->GetEntryLabel());
3857 } else {
3858 DCHECK(value.IsConstant()) << value;
3859 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3860 __ jmp(slow_path->GetEntryLabel());
3861 }
3862 }
3863 break;
3864 }
3865 default:
3866 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003867 }
Calin Juravled0d48522014-11-04 16:40:20 +00003868}
3869
Calin Juravle9aec02f2014-11-18 23:06:35 +00003870void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3871 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3872
3873 LocationSummary* locations =
3874 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3875
3876 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003877 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003878 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003879 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003880 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003881 // The shift count needs to be in CL or a constant.
3882 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003883 locations->SetOut(Location::SameAsFirstInput());
3884 break;
3885 }
3886 default:
3887 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3888 }
3889}
3890
3891void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3892 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3893
3894 LocationSummary* locations = op->GetLocations();
3895 Location first = locations->InAt(0);
3896 Location second = locations->InAt(1);
3897 DCHECK(first.Equals(locations->Out()));
3898
3899 switch (op->GetResultType()) {
3900 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003901 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003902 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003903 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003904 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003905 DCHECK_EQ(ECX, second_reg);
3906 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003907 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003908 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003909 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003910 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003911 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003912 }
3913 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003914 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003915 if (shift == 0) {
3916 return;
3917 }
3918 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003919 if (op->IsShl()) {
3920 __ shll(first_reg, imm);
3921 } else if (op->IsShr()) {
3922 __ sarl(first_reg, imm);
3923 } else {
3924 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003925 }
3926 }
3927 break;
3928 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003929 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003930 if (second.IsRegister()) {
3931 Register second_reg = second.AsRegister<Register>();
3932 DCHECK_EQ(ECX, second_reg);
3933 if (op->IsShl()) {
3934 GenerateShlLong(first, second_reg);
3935 } else if (op->IsShr()) {
3936 GenerateShrLong(first, second_reg);
3937 } else {
3938 GenerateUShrLong(first, second_reg);
3939 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003940 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003941 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003942 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003943 // Nothing to do if the shift is 0, as the input is already the output.
3944 if (shift != 0) {
3945 if (op->IsShl()) {
3946 GenerateShlLong(first, shift);
3947 } else if (op->IsShr()) {
3948 GenerateShrLong(first, shift);
3949 } else {
3950 GenerateUShrLong(first, shift);
3951 }
3952 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003953 }
3954 break;
3955 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003956 default:
3957 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3958 }
3959}
3960
Mark P Mendell73945692015-04-29 14:56:17 +00003961void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3962 Register low = loc.AsRegisterPairLow<Register>();
3963 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003964 if (shift == 1) {
3965 // This is just an addition.
3966 __ addl(low, low);
3967 __ adcl(high, high);
3968 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003969 // Shift by 32 is easy. High gets low, and low gets 0.
3970 codegen_->EmitParallelMoves(
3971 loc.ToLow(),
3972 loc.ToHigh(),
3973 Primitive::kPrimInt,
3974 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3975 loc.ToLow(),
3976 Primitive::kPrimInt);
3977 } else if (shift > 32) {
3978 // Low part becomes 0. High part is low part << (shift-32).
3979 __ movl(high, low);
3980 __ shll(high, Immediate(shift - 32));
3981 __ xorl(low, low);
3982 } else {
3983 // Between 1 and 31.
3984 __ shld(high, low, Immediate(shift));
3985 __ shll(low, Immediate(shift));
3986 }
3987}
3988
Calin Juravle9aec02f2014-11-18 23:06:35 +00003989void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003990 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003991 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3992 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3993 __ testl(shifter, Immediate(32));
3994 __ j(kEqual, &done);
3995 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3996 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3997 __ Bind(&done);
3998}
3999
Mark P Mendell73945692015-04-29 14:56:17 +00004000void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4001 Register low = loc.AsRegisterPairLow<Register>();
4002 Register high = loc.AsRegisterPairHigh<Register>();
4003 if (shift == 32) {
4004 // Need to copy the sign.
4005 DCHECK_NE(low, high);
4006 __ movl(low, high);
4007 __ sarl(high, Immediate(31));
4008 } else if (shift > 32) {
4009 DCHECK_NE(low, high);
4010 // High part becomes sign. Low part is shifted by shift - 32.
4011 __ movl(low, high);
4012 __ sarl(high, Immediate(31));
4013 __ sarl(low, Immediate(shift - 32));
4014 } else {
4015 // Between 1 and 31.
4016 __ shrd(low, high, Immediate(shift));
4017 __ sarl(high, Immediate(shift));
4018 }
4019}
4020
Calin Juravle9aec02f2014-11-18 23:06:35 +00004021void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004022 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004023 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4024 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4025 __ testl(shifter, Immediate(32));
4026 __ j(kEqual, &done);
4027 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4028 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4029 __ Bind(&done);
4030}
4031
Mark P Mendell73945692015-04-29 14:56:17 +00004032void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4033 Register low = loc.AsRegisterPairLow<Register>();
4034 Register high = loc.AsRegisterPairHigh<Register>();
4035 if (shift == 32) {
4036 // Shift by 32 is easy. Low gets high, and high gets 0.
4037 codegen_->EmitParallelMoves(
4038 loc.ToHigh(),
4039 loc.ToLow(),
4040 Primitive::kPrimInt,
4041 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4042 loc.ToHigh(),
4043 Primitive::kPrimInt);
4044 } else if (shift > 32) {
4045 // Low part is high >> (shift - 32). High part becomes 0.
4046 __ movl(low, high);
4047 __ shrl(low, Immediate(shift - 32));
4048 __ xorl(high, high);
4049 } else {
4050 // Between 1 and 31.
4051 __ shrd(low, high, Immediate(shift));
4052 __ shrl(high, Immediate(shift));
4053 }
4054}
4055
Calin Juravle9aec02f2014-11-18 23:06:35 +00004056void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004057 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004058 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4059 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4060 __ testl(shifter, Immediate(32));
4061 __ j(kEqual, &done);
4062 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4063 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4064 __ Bind(&done);
4065}
4066
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004067void LocationsBuilderX86::VisitRor(HRor* ror) {
4068 LocationSummary* locations =
4069 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4070
4071 switch (ror->GetResultType()) {
4072 case Primitive::kPrimLong:
4073 // Add the temporary needed.
4074 locations->AddTemp(Location::RequiresRegister());
4075 FALLTHROUGH_INTENDED;
4076 case Primitive::kPrimInt:
4077 locations->SetInAt(0, Location::RequiresRegister());
4078 // The shift count needs to be in CL (unless it is a constant).
4079 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4080 locations->SetOut(Location::SameAsFirstInput());
4081 break;
4082 default:
4083 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4084 UNREACHABLE();
4085 }
4086}
4087
4088void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4089 LocationSummary* locations = ror->GetLocations();
4090 Location first = locations->InAt(0);
4091 Location second = locations->InAt(1);
4092
4093 if (ror->GetResultType() == Primitive::kPrimInt) {
4094 Register first_reg = first.AsRegister<Register>();
4095 if (second.IsRegister()) {
4096 Register second_reg = second.AsRegister<Register>();
4097 __ rorl(first_reg, second_reg);
4098 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004099 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004100 __ rorl(first_reg, imm);
4101 }
4102 return;
4103 }
4104
4105 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4106 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4107 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4108 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4109 if (second.IsRegister()) {
4110 Register second_reg = second.AsRegister<Register>();
4111 DCHECK_EQ(second_reg, ECX);
4112 __ movl(temp_reg, first_reg_hi);
4113 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4114 __ shrd(first_reg_lo, temp_reg, second_reg);
4115 __ movl(temp_reg, first_reg_hi);
4116 __ testl(second_reg, Immediate(32));
4117 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4118 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4119 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004120 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004121 if (shift_amt == 0) {
4122 // Already fine.
4123 return;
4124 }
4125 if (shift_amt == 32) {
4126 // Just swap.
4127 __ movl(temp_reg, first_reg_lo);
4128 __ movl(first_reg_lo, first_reg_hi);
4129 __ movl(first_reg_hi, temp_reg);
4130 return;
4131 }
4132
4133 Immediate imm(shift_amt);
4134 // Save the constents of the low value.
4135 __ movl(temp_reg, first_reg_lo);
4136
4137 // Shift right into low, feeding bits from high.
4138 __ shrd(first_reg_lo, first_reg_hi, imm);
4139
4140 // Shift right into high, feeding bits from the original low.
4141 __ shrd(first_reg_hi, temp_reg, imm);
4142
4143 // Swap if needed.
4144 if (shift_amt > 32) {
4145 __ movl(temp_reg, first_reg_lo);
4146 __ movl(first_reg_lo, first_reg_hi);
4147 __ movl(first_reg_hi, temp_reg);
4148 }
4149 }
4150}
4151
Calin Juravle9aec02f2014-11-18 23:06:35 +00004152void LocationsBuilderX86::VisitShl(HShl* shl) {
4153 HandleShift(shl);
4154}
4155
4156void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4157 HandleShift(shl);
4158}
4159
4160void LocationsBuilderX86::VisitShr(HShr* shr) {
4161 HandleShift(shr);
4162}
4163
4164void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4165 HandleShift(shr);
4166}
4167
4168void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4169 HandleShift(ushr);
4170}
4171
4172void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4173 HandleShift(ushr);
4174}
4175
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004176void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004177 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004178 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004179 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004180 if (instruction->IsStringAlloc()) {
4181 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4182 } else {
4183 InvokeRuntimeCallingConvention calling_convention;
4184 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004185 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004186}
4187
4188void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004189 // Note: if heap poisoning is enabled, the entry point takes cares
4190 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004191 if (instruction->IsStringAlloc()) {
4192 // String is allocated through StringFactory. Call NewEmptyString entry point.
4193 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004194 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004195 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4196 __ call(Address(temp, code_offset.Int32Value()));
4197 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4198 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004199 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004200 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004201 DCHECK(!codegen_->IsLeafMethod());
4202 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004203}
4204
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004205void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4206 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004207 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004208 locations->SetOut(Location::RegisterLocation(EAX));
4209 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004210 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4211 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004212}
4213
4214void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004215 // Note: if heap poisoning is enabled, the entry point takes cares
4216 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004217 QuickEntrypointEnum entrypoint =
4218 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4219 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004220 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004221 DCHECK(!codegen_->IsLeafMethod());
4222}
4223
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004224void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004225 LocationSummary* locations =
4226 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004227 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4228 if (location.IsStackSlot()) {
4229 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4230 } else if (location.IsDoubleStackSlot()) {
4231 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004232 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004233 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004234}
4235
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004236void InstructionCodeGeneratorX86::VisitParameterValue(
4237 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4238}
4239
4240void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4241 LocationSummary* locations =
4242 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4243 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4244}
4245
4246void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004247}
4248
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004249void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4250 LocationSummary* locations =
4251 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4252 locations->SetInAt(0, Location::RequiresRegister());
4253 locations->SetOut(Location::RequiresRegister());
4254}
4255
4256void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4257 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004258 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004259 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004260 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004261 __ movl(locations->Out().AsRegister<Register>(),
4262 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004263 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004264 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004265 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004266 __ movl(locations->Out().AsRegister<Register>(),
4267 Address(locations->InAt(0).AsRegister<Register>(),
4268 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4269 // temp = temp->GetImtEntryAt(method_offset);
4270 __ movl(locations->Out().AsRegister<Register>(),
4271 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004272 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004273}
4274
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004275void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004276 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004277 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004278 locations->SetInAt(0, Location::RequiresRegister());
4279 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004280}
4281
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004282void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4283 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004284 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004285 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004286 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004287 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004288 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004289 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004290 break;
4291
4292 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004293 __ notl(out.AsRegisterPairLow<Register>());
4294 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004295 break;
4296
4297 default:
4298 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4299 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004300}
4301
David Brazdil66d126e2015-04-03 16:02:44 +01004302void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4303 LocationSummary* locations =
4304 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4305 locations->SetInAt(0, Location::RequiresRegister());
4306 locations->SetOut(Location::SameAsFirstInput());
4307}
4308
4309void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004310 LocationSummary* locations = bool_not->GetLocations();
4311 Location in = locations->InAt(0);
4312 Location out = locations->Out();
4313 DCHECK(in.Equals(out));
4314 __ xorl(out.AsRegister<Register>(), Immediate(1));
4315}
4316
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004317void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004318 LocationSummary* locations =
4319 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004320 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004321 case Primitive::kPrimBoolean:
4322 case Primitive::kPrimByte:
4323 case Primitive::kPrimShort:
4324 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004325 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004326 case Primitive::kPrimLong: {
4327 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004328 locations->SetInAt(1, Location::Any());
4329 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4330 break;
4331 }
4332 case Primitive::kPrimFloat:
4333 case Primitive::kPrimDouble: {
4334 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004335 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4336 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4337 } else if (compare->InputAt(1)->IsConstant()) {
4338 locations->SetInAt(1, Location::RequiresFpuRegister());
4339 } else {
4340 locations->SetInAt(1, Location::Any());
4341 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004342 locations->SetOut(Location::RequiresRegister());
4343 break;
4344 }
4345 default:
4346 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4347 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004348}
4349
4350void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004351 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004352 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004353 Location left = locations->InAt(0);
4354 Location right = locations->InAt(1);
4355
Mark Mendell0c9497d2015-08-21 09:30:05 -04004356 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004357 Condition less_cond = kLess;
4358
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004359 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004360 case Primitive::kPrimBoolean:
4361 case Primitive::kPrimByte:
4362 case Primitive::kPrimShort:
4363 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004364 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004365 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004366 break;
4367 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004368 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004369 Register left_low = left.AsRegisterPairLow<Register>();
4370 Register left_high = left.AsRegisterPairHigh<Register>();
4371 int32_t val_low = 0;
4372 int32_t val_high = 0;
4373 bool right_is_const = false;
4374
4375 if (right.IsConstant()) {
4376 DCHECK(right.GetConstant()->IsLongConstant());
4377 right_is_const = true;
4378 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4379 val_low = Low32Bits(val);
4380 val_high = High32Bits(val);
4381 }
4382
Calin Juravleddb7df22014-11-25 20:56:51 +00004383 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004384 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004385 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004386 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004387 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004388 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004389 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004390 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004391 __ j(kLess, &less); // Signed compare.
4392 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004393 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004394 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004395 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004396 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004397 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004398 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004399 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004400 }
Aart Bika19616e2016-02-01 18:57:58 -08004401 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004402 break;
4403 }
4404 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004405 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004406 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004407 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004408 break;
4409 }
4410 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004411 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004412 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004413 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004414 break;
4415 }
4416 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004417 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004418 }
Aart Bika19616e2016-02-01 18:57:58 -08004419
Calin Juravleddb7df22014-11-25 20:56:51 +00004420 __ movl(out, Immediate(0));
4421 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004422 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004423
4424 __ Bind(&greater);
4425 __ movl(out, Immediate(1));
4426 __ jmp(&done);
4427
4428 __ Bind(&less);
4429 __ movl(out, Immediate(-1));
4430
4431 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004432}
4433
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004434void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004435 LocationSummary* locations =
4436 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004437 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004438 locations->SetInAt(i, Location::Any());
4439 }
4440 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004441}
4442
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004443void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004444 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004445}
4446
Roland Levillain7c1559a2015-12-15 10:55:36 +00004447void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004448 /*
4449 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4450 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4451 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4452 */
4453 switch (kind) {
4454 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004455 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004456 break;
4457 }
4458 case MemBarrierKind::kAnyStore:
4459 case MemBarrierKind::kLoadAny:
4460 case MemBarrierKind::kStoreStore: {
4461 // nop
4462 break;
4463 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004464 case MemBarrierKind::kNTStoreStore:
4465 // Non-Temporal Store/Store needs an explicit fence.
4466 MemoryFence(/* non-temporal */ true);
4467 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004468 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004469}
4470
Vladimir Markodc151b22015-10-15 18:02:30 +01004471HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4472 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004473 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004474 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004475}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004476
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004477Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4478 Register temp) {
4479 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004480 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004481 if (!invoke->GetLocations()->Intrinsified()) {
4482 return location.AsRegister<Register>();
4483 }
4484 // For intrinsics we allow any location, so it may be on the stack.
4485 if (!location.IsRegister()) {
4486 __ movl(temp, Address(ESP, location.GetStackIndex()));
4487 return temp;
4488 }
4489 // For register locations, check if the register was saved. If so, get it from the stack.
4490 // Note: There is a chance that the register was saved but not overwritten, so we could
4491 // save one load. However, since this is just an intrinsic slow path we prefer this
4492 // simple and more robust approach rather that trying to determine if that's the case.
4493 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004494 if (slow_path != nullptr) {
4495 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4496 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4497 __ movl(temp, Address(ESP, stack_offset));
4498 return temp;
4499 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004500 }
4501 return location.AsRegister<Register>();
4502}
4503
Serguei Katkov288c7a82016-05-16 11:53:15 +06004504Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4505 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004506 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4507 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004508 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004509 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004510 uint32_t offset =
4511 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4512 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004513 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004514 }
Vladimir Marko58155012015-08-19 12:49:41 +00004515 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004516 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004517 break;
4518 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4519 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4520 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004521 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4522 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4523 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004524 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004525 // Bind a new fixup label at the end of the "movl" insn.
4526 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004527 __ Bind(NewPcRelativeDexCacheArrayPatch(
4528 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
4529 invoke->GetDexFileForPcRelativeDexCache(),
4530 offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004531 break;
4532 }
Vladimir Marko58155012015-08-19 12:49:41 +00004533 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004534 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004535 Register method_reg;
4536 Register reg = temp.AsRegister<Register>();
4537 if (current_method.IsRegister()) {
4538 method_reg = current_method.AsRegister<Register>();
4539 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004540 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004541 DCHECK(!current_method.IsValid());
4542 method_reg = reg;
4543 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4544 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004545 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004546 __ movl(reg, Address(method_reg,
4547 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004548 // temp = temp[index_in_cache];
4549 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4550 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004551 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4552 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004553 }
Vladimir Marko58155012015-08-19 12:49:41 +00004554 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004555 return callee_method;
4556}
4557
4558void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4559 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004560
4561 switch (invoke->GetCodePtrLocation()) {
4562 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4563 __ call(GetFrameEntryLabel());
4564 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004565 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4566 // (callee_method + offset_of_quick_compiled_code)()
4567 __ call(Address(callee_method.AsRegister<Register>(),
4568 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004569 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004570 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004571 }
4572
4573 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004574}
4575
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004576void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4577 Register temp = temp_in.AsRegister<Register>();
4578 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4579 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004580
4581 // Use the calling convention instead of the location of the receiver, as
4582 // intrinsics may have put the receiver in a different register. In the intrinsics
4583 // slow path, the arguments have been moved to the right place, so here we are
4584 // guaranteed that the receiver is the first register of the calling convention.
4585 InvokeDexCallingConvention calling_convention;
4586 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004587 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004588 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004589 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004590 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004591 // Instead of simply (possibly) unpoisoning `temp` here, we should
4592 // emit a read barrier for the previous class reference load.
4593 // However this is not required in practice, as this is an
4594 // intermediate/temporary reference and because the current
4595 // concurrent copying collector keeps the from-space memory
4596 // intact/accessible until the end of the marking phase (the
4597 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004598 __ MaybeUnpoisonHeapReference(temp);
4599 // temp = temp->GetMethodAt(method_offset);
4600 __ movl(temp, Address(temp, method_offset));
4601 // call temp->GetEntryPoint();
4602 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004603 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004604}
4605
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004606void CodeGeneratorX86::RecordSimplePatch() {
4607 if (GetCompilerOptions().GetIncludePatchInformation()) {
4608 simple_patches_.emplace_back();
4609 __ Bind(&simple_patches_.back());
4610 }
4611}
4612
Vladimir Markoaad75c62016-10-03 08:46:48 +00004613void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4614 DCHECK(GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004615 HX86ComputeBaseMethodAddress* address = nullptr;
4616 if (GetCompilerOptions().GetCompilePic()) {
4617 address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4618 } else {
4619 DCHECK_EQ(load_string->InputCount(), 0u);
4620 }
4621 string_patches_.emplace_back(address,
4622 load_string->GetDexFile(),
4623 load_string->GetStringIndex().index_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004624 __ Bind(&string_patches_.back().label);
4625}
4626
Vladimir Marko1998cd02017-01-13 13:02:58 +00004627void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004628 HX86ComputeBaseMethodAddress* address = nullptr;
4629 if (GetCompilerOptions().GetCompilePic()) {
4630 address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4631 } else {
4632 DCHECK_EQ(load_class->InputCount(), 0u);
4633 }
4634 boot_image_type_patches_.emplace_back(address,
4635 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004636 load_class->GetTypeIndex().index_);
4637 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004638}
4639
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004640Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004641 HX86ComputeBaseMethodAddress* address =
4642 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4643 type_bss_entry_patches_.emplace_back(
4644 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004645 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004646}
4647
Vladimir Markoaad75c62016-10-03 08:46:48 +00004648Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4649 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004650 HX86ComputeBaseMethodAddress* address =
4651 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4652 string_patches_.emplace_back(
4653 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004654 return &string_patches_.back().label;
4655}
4656
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004657Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(
4658 HX86ComputeBaseMethodAddress* method_address,
4659 const DexFile& dex_file,
4660 uint32_t element_offset) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004661 // Add the patch entry and bind its label at the end of the instruction.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004662 pc_relative_dex_cache_patches_.emplace_back(method_address, dex_file, element_offset);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004663 return &pc_relative_dex_cache_patches_.back().label;
4664}
4665
Vladimir Markoaad75c62016-10-03 08:46:48 +00004666// The label points to the end of the "movl" or another instruction but the literal offset
4667// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4668constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4669
4670template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4671inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004672 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004673 ArenaVector<LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004674 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004675 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004676 linker_patches->push_back(Factory(
4677 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004678 }
4679}
4680
Vladimir Marko58155012015-08-19 12:49:41 +00004681void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4682 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004683 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004684 pc_relative_dex_cache_patches_.size() +
4685 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004686 string_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004687 boot_image_type_patches_.size() +
4688 type_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004689 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004690 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4691 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004692 for (const Label& label : simple_patches_) {
4693 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4694 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4695 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004696 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004697 DCHECK(boot_image_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004698 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4699 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004700 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(boot_image_type_patches_,
4701 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004702 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004703 } else {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004704 for (const PatchInfo<Label>& info : boot_image_type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004705 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004706 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004707 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004708 for (const PatchInfo<Label>& info : string_patches_) {
4709 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4710 linker_patches->push_back(
4711 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
4712 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004713 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004714 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4715 linker_patches);
4716 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004717}
4718
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004719void CodeGeneratorX86::MarkGCCard(Register temp,
4720 Register card,
4721 Register object,
4722 Register value,
4723 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004724 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004725 if (value_can_be_null) {
4726 __ testl(value, value);
4727 __ j(kEqual, &is_null);
4728 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004729 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004730 __ movl(temp, object);
4731 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004732 __ movb(Address(temp, card, TIMES_1, 0),
4733 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004734 if (value_can_be_null) {
4735 __ Bind(&is_null);
4736 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004737}
4738
Calin Juravle52c48962014-12-16 17:02:57 +00004739void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4740 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004741
4742 bool object_field_get_with_read_barrier =
4743 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004744 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004745 new (GetGraph()->GetArena()) LocationSummary(instruction,
4746 kEmitCompilerReadBarrier ?
4747 LocationSummary::kCallOnSlowPath :
4748 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004749 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004750 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004751 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004752 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004753
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004754 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4755 locations->SetOut(Location::RequiresFpuRegister());
4756 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004757 // The output overlaps in case of long: we don't want the low move
4758 // to overwrite the object's location. Likewise, in the case of
4759 // an object field get with read barriers enabled, we do not want
4760 // the move to overwrite the object's location, as we need it to emit
4761 // the read barrier.
4762 locations->SetOut(
4763 Location::RequiresRegister(),
4764 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4765 Location::kOutputOverlap :
4766 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004767 }
Calin Juravle52c48962014-12-16 17:02:57 +00004768
4769 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4770 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004771 // So we use an XMM register as a temp to achieve atomicity (first
4772 // load the temp into the XMM and then copy the XMM into the
4773 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004774 locations->AddTemp(Location::RequiresFpuRegister());
4775 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004776}
4777
Calin Juravle52c48962014-12-16 17:02:57 +00004778void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4779 const FieldInfo& field_info) {
4780 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004781
Calin Juravle52c48962014-12-16 17:02:57 +00004782 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004783 Location base_loc = locations->InAt(0);
4784 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004785 Location out = locations->Out();
4786 bool is_volatile = field_info.IsVolatile();
4787 Primitive::Type field_type = field_info.GetFieldType();
4788 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4789
4790 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004791 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004792 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004793 break;
4794 }
4795
4796 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004797 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004798 break;
4799 }
4800
4801 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004802 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004803 break;
4804 }
4805
4806 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004807 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004808 break;
4809 }
4810
4811 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004812 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004813 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004814
4815 case Primitive::kPrimNot: {
4816 // /* HeapReference<Object> */ out = *(base + offset)
4817 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004818 // Note that a potential implicit null check is handled in this
4819 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4820 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004821 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004822 if (is_volatile) {
4823 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4824 }
4825 } else {
4826 __ movl(out.AsRegister<Register>(), Address(base, offset));
4827 codegen_->MaybeRecordImplicitNullCheck(instruction);
4828 if (is_volatile) {
4829 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4830 }
4831 // If read barriers are enabled, emit read barriers other than
4832 // Baker's using a slow path (and also unpoison the loaded
4833 // reference, if heap poisoning is enabled).
4834 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4835 }
4836 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004837 }
4838
4839 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004840 if (is_volatile) {
4841 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4842 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004843 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004844 __ movd(out.AsRegisterPairLow<Register>(), temp);
4845 __ psrlq(temp, Immediate(32));
4846 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4847 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004848 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004849 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004850 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004851 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4852 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004853 break;
4854 }
4855
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004856 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004857 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004858 break;
4859 }
4860
4861 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004862 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004863 break;
4864 }
4865
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004866 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004867 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004868 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004869 }
Calin Juravle52c48962014-12-16 17:02:57 +00004870
Roland Levillain7c1559a2015-12-15 10:55:36 +00004871 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4872 // Potential implicit null checks, in the case of reference or
4873 // long fields, are handled in the previous switch statement.
4874 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004875 codegen_->MaybeRecordImplicitNullCheck(instruction);
4876 }
4877
Calin Juravle52c48962014-12-16 17:02:57 +00004878 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004879 if (field_type == Primitive::kPrimNot) {
4880 // Memory barriers, in the case of references, are also handled
4881 // in the previous switch statement.
4882 } else {
4883 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4884 }
Roland Levillain4d027112015-07-01 15:41:14 +01004885 }
Calin Juravle52c48962014-12-16 17:02:57 +00004886}
4887
4888void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4889 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4890
4891 LocationSummary* locations =
4892 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4893 locations->SetInAt(0, Location::RequiresRegister());
4894 bool is_volatile = field_info.IsVolatile();
4895 Primitive::Type field_type = field_info.GetFieldType();
4896 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4897 || (field_type == Primitive::kPrimByte);
4898
4899 // The register allocator does not support multiple
4900 // inputs that die at entry with one in a specific register.
4901 if (is_byte_type) {
4902 // Ensure the value is in a byte register.
4903 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004904 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004905 if (is_volatile && field_type == Primitive::kPrimDouble) {
4906 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4907 locations->SetInAt(1, Location::RequiresFpuRegister());
4908 } else {
4909 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4910 }
4911 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4912 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004913 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004914
Calin Juravle52c48962014-12-16 17:02:57 +00004915 // 64bits value can be atomically written to an address with movsd and an XMM register.
4916 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4917 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4918 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4919 // isolated cases when we need this it isn't worth adding the extra complexity.
4920 locations->AddTemp(Location::RequiresFpuRegister());
4921 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004922 } else {
4923 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4924
4925 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4926 // Temporary registers for the write barrier.
4927 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4928 // Ensure the card is in a byte register.
4929 locations->AddTemp(Location::RegisterLocation(ECX));
4930 }
Calin Juravle52c48962014-12-16 17:02:57 +00004931 }
4932}
4933
4934void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004935 const FieldInfo& field_info,
4936 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004937 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4938
4939 LocationSummary* locations = instruction->GetLocations();
4940 Register base = locations->InAt(0).AsRegister<Register>();
4941 Location value = locations->InAt(1);
4942 bool is_volatile = field_info.IsVolatile();
4943 Primitive::Type field_type = field_info.GetFieldType();
4944 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004945 bool needs_write_barrier =
4946 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004947
4948 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004949 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004950 }
4951
Mark Mendell81489372015-11-04 11:30:41 -05004952 bool maybe_record_implicit_null_check_done = false;
4953
Calin Juravle52c48962014-12-16 17:02:57 +00004954 switch (field_type) {
4955 case Primitive::kPrimBoolean:
4956 case Primitive::kPrimByte: {
4957 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4958 break;
4959 }
4960
4961 case Primitive::kPrimShort:
4962 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004963 if (value.IsConstant()) {
4964 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4965 __ movw(Address(base, offset), Immediate(v));
4966 } else {
4967 __ movw(Address(base, offset), value.AsRegister<Register>());
4968 }
Calin Juravle52c48962014-12-16 17:02:57 +00004969 break;
4970 }
4971
4972 case Primitive::kPrimInt:
4973 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004974 if (kPoisonHeapReferences && needs_write_barrier) {
4975 // Note that in the case where `value` is a null reference,
4976 // we do not enter this block, as the reference does not
4977 // need poisoning.
4978 DCHECK_EQ(field_type, Primitive::kPrimNot);
4979 Register temp = locations->GetTemp(0).AsRegister<Register>();
4980 __ movl(temp, value.AsRegister<Register>());
4981 __ PoisonHeapReference(temp);
4982 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004983 } else if (value.IsConstant()) {
4984 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4985 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004986 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004987 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004988 __ movl(Address(base, offset), value.AsRegister<Register>());
4989 }
Calin Juravle52c48962014-12-16 17:02:57 +00004990 break;
4991 }
4992
4993 case Primitive::kPrimLong: {
4994 if (is_volatile) {
4995 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4996 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4997 __ movd(temp1, value.AsRegisterPairLow<Register>());
4998 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4999 __ punpckldq(temp1, temp2);
5000 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005001 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005002 } else if (value.IsConstant()) {
5003 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5004 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5005 codegen_->MaybeRecordImplicitNullCheck(instruction);
5006 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005007 } else {
5008 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005009 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005010 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5011 }
Mark Mendell81489372015-11-04 11:30:41 -05005012 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005013 break;
5014 }
5015
5016 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05005017 if (value.IsConstant()) {
5018 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5019 __ movl(Address(base, offset), Immediate(v));
5020 } else {
5021 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5022 }
Calin Juravle52c48962014-12-16 17:02:57 +00005023 break;
5024 }
5025
5026 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005027 if (value.IsConstant()) {
5028 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5029 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5030 codegen_->MaybeRecordImplicitNullCheck(instruction);
5031 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5032 maybe_record_implicit_null_check_done = true;
5033 } else {
5034 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5035 }
Calin Juravle52c48962014-12-16 17:02:57 +00005036 break;
5037 }
5038
5039 case Primitive::kPrimVoid:
5040 LOG(FATAL) << "Unreachable type " << field_type;
5041 UNREACHABLE();
5042 }
5043
Mark Mendell81489372015-11-04 11:30:41 -05005044 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005045 codegen_->MaybeRecordImplicitNullCheck(instruction);
5046 }
5047
Roland Levillain4d027112015-07-01 15:41:14 +01005048 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005049 Register temp = locations->GetTemp(0).AsRegister<Register>();
5050 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005051 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005052 }
5053
Calin Juravle52c48962014-12-16 17:02:57 +00005054 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005055 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005056 }
5057}
5058
5059void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5060 HandleFieldGet(instruction, instruction->GetFieldInfo());
5061}
5062
5063void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5064 HandleFieldGet(instruction, instruction->GetFieldInfo());
5065}
5066
5067void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5068 HandleFieldSet(instruction, instruction->GetFieldInfo());
5069}
5070
5071void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005072 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005073}
5074
5075void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5076 HandleFieldSet(instruction, instruction->GetFieldInfo());
5077}
5078
5079void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005080 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005081}
5082
5083void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5084 HandleFieldGet(instruction, instruction->GetFieldInfo());
5085}
5086
5087void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5088 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005089}
5090
Calin Juravlee460d1d2015-09-29 04:52:17 +01005091void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5092 HUnresolvedInstanceFieldGet* instruction) {
5093 FieldAccessCallingConventionX86 calling_convention;
5094 codegen_->CreateUnresolvedFieldLocationSummary(
5095 instruction, instruction->GetFieldType(), calling_convention);
5096}
5097
5098void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5099 HUnresolvedInstanceFieldGet* instruction) {
5100 FieldAccessCallingConventionX86 calling_convention;
5101 codegen_->GenerateUnresolvedFieldAccess(instruction,
5102 instruction->GetFieldType(),
5103 instruction->GetFieldIndex(),
5104 instruction->GetDexPc(),
5105 calling_convention);
5106}
5107
5108void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5109 HUnresolvedInstanceFieldSet* instruction) {
5110 FieldAccessCallingConventionX86 calling_convention;
5111 codegen_->CreateUnresolvedFieldLocationSummary(
5112 instruction, instruction->GetFieldType(), calling_convention);
5113}
5114
5115void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5116 HUnresolvedInstanceFieldSet* instruction) {
5117 FieldAccessCallingConventionX86 calling_convention;
5118 codegen_->GenerateUnresolvedFieldAccess(instruction,
5119 instruction->GetFieldType(),
5120 instruction->GetFieldIndex(),
5121 instruction->GetDexPc(),
5122 calling_convention);
5123}
5124
5125void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5126 HUnresolvedStaticFieldGet* instruction) {
5127 FieldAccessCallingConventionX86 calling_convention;
5128 codegen_->CreateUnresolvedFieldLocationSummary(
5129 instruction, instruction->GetFieldType(), calling_convention);
5130}
5131
5132void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5133 HUnresolvedStaticFieldGet* instruction) {
5134 FieldAccessCallingConventionX86 calling_convention;
5135 codegen_->GenerateUnresolvedFieldAccess(instruction,
5136 instruction->GetFieldType(),
5137 instruction->GetFieldIndex(),
5138 instruction->GetDexPc(),
5139 calling_convention);
5140}
5141
5142void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5143 HUnresolvedStaticFieldSet* instruction) {
5144 FieldAccessCallingConventionX86 calling_convention;
5145 codegen_->CreateUnresolvedFieldLocationSummary(
5146 instruction, instruction->GetFieldType(), calling_convention);
5147}
5148
5149void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5150 HUnresolvedStaticFieldSet* instruction) {
5151 FieldAccessCallingConventionX86 calling_convention;
5152 codegen_->GenerateUnresolvedFieldAccess(instruction,
5153 instruction->GetFieldType(),
5154 instruction->GetFieldIndex(),
5155 instruction->GetDexPc(),
5156 calling_convention);
5157}
5158
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005159void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005160 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5161 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5162 ? Location::RequiresRegister()
5163 : Location::Any();
5164 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005165}
5166
Calin Juravle2ae48182016-03-16 14:05:09 +00005167void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5168 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005169 return;
5170 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005171 LocationSummary* locations = instruction->GetLocations();
5172 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005173
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005174 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005175 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005176}
5177
Calin Juravle2ae48182016-03-16 14:05:09 +00005178void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005179 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005180 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005181
5182 LocationSummary* locations = instruction->GetLocations();
5183 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005184
5185 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005186 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005187 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005188 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005189 } else {
5190 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005191 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005192 __ jmp(slow_path->GetEntryLabel());
5193 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005194 }
5195 __ j(kEqual, slow_path->GetEntryLabel());
5196}
5197
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005198void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005199 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005200}
5201
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005202void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005203 bool object_array_get_with_read_barrier =
5204 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005205 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005206 new (GetGraph()->GetArena()) LocationSummary(instruction,
5207 object_array_get_with_read_barrier ?
5208 LocationSummary::kCallOnSlowPath :
5209 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005210 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005211 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005212 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005213 locations->SetInAt(0, Location::RequiresRegister());
5214 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005215 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5216 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5217 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005218 // The output overlaps in case of long: we don't want the low move
5219 // to overwrite the array's location. Likewise, in the case of an
5220 // object array get with read barriers enabled, we do not want the
5221 // move to overwrite the array's location, as we need it to emit
5222 // the read barrier.
5223 locations->SetOut(
5224 Location::RequiresRegister(),
5225 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5226 Location::kOutputOverlap :
5227 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005228 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005229}
5230
5231void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5232 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005233 Location obj_loc = locations->InAt(0);
5234 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005235 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005236 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005237 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005238
Calin Juravle77520bc2015-01-12 18:45:46 +00005239 Primitive::Type type = instruction->GetType();
5240 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005241 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005242 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005243 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005244 break;
5245 }
5246
5247 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005248 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005249 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005250 break;
5251 }
5252
5253 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005254 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005255 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005256 break;
5257 }
5258
5259 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005260 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005261 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5262 // Branch cases into compressed and uncompressed for each index's type.
5263 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5264 NearLabel done, not_compressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005265 __ testl(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005266 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005267 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5268 "Expecting 0=compressed, 1=uncompressed");
5269 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005270 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5271 __ jmp(&done);
5272 __ Bind(&not_compressed);
5273 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5274 __ Bind(&done);
5275 } else {
5276 // Common case for charAt of array of char or when string compression's
5277 // feature is turned off.
5278 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5279 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005280 break;
5281 }
5282
Roland Levillain7c1559a2015-12-15 10:55:36 +00005283 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005284 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005285 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005286 break;
5287 }
5288
Roland Levillain7c1559a2015-12-15 10:55:36 +00005289 case Primitive::kPrimNot: {
5290 static_assert(
5291 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5292 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005293 // /* HeapReference<Object> */ out =
5294 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5295 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005296 // Note that a potential implicit null check is handled in this
5297 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5298 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005299 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005300 } else {
5301 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005302 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5303 codegen_->MaybeRecordImplicitNullCheck(instruction);
5304 // If read barriers are enabled, emit read barriers other than
5305 // Baker's using a slow path (and also unpoison the loaded
5306 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005307 if (index.IsConstant()) {
5308 uint32_t offset =
5309 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005310 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5311 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005312 codegen_->MaybeGenerateReadBarrierSlow(
5313 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5314 }
5315 }
5316 break;
5317 }
5318
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005319 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005320 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005321 __ movl(out_loc.AsRegisterPairLow<Register>(),
5322 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5323 codegen_->MaybeRecordImplicitNullCheck(instruction);
5324 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5325 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326 break;
5327 }
5328
Mark Mendell7c8d0092015-01-26 11:21:33 -05005329 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005330 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005331 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005332 break;
5333 }
5334
5335 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005336 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005337 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005338 break;
5339 }
5340
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005341 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005342 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005343 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005344 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005345
Roland Levillain7c1559a2015-12-15 10:55:36 +00005346 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5347 // Potential implicit null checks, in the case of reference or
5348 // long arrays, are handled in the previous switch statement.
5349 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005350 codegen_->MaybeRecordImplicitNullCheck(instruction);
5351 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005352}
5353
5354void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005355 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005356
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005357 bool needs_write_barrier =
5358 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005359 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005360
Nicolas Geoffray39468442014-09-02 15:17:15 +01005361 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5362 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005363 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005364 LocationSummary::kCallOnSlowPath :
5365 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005366
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005367 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5368 || (value_type == Primitive::kPrimByte);
5369 // We need the inputs to be different than the output in case of long operation.
5370 // In case of a byte operation, the register allocator does not support multiple
5371 // inputs that die at entry with one in a specific register.
5372 locations->SetInAt(0, Location::RequiresRegister());
5373 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5374 if (is_byte_type) {
5375 // Ensure the value is in a byte register.
5376 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5377 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005378 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005379 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005380 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5381 }
5382 if (needs_write_barrier) {
5383 // Temporary registers for the write barrier.
5384 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5385 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005386 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005387 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005388}
5389
5390void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5391 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005392 Location array_loc = locations->InAt(0);
5393 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005394 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005395 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005396 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005397 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5398 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5399 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005400 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005401 bool needs_write_barrier =
5402 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005403
5404 switch (value_type) {
5405 case Primitive::kPrimBoolean:
5406 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005407 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005408 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005409 if (value.IsRegister()) {
5410 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005411 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005412 __ movb(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
5418 case Primitive::kPrimShort:
5419 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005420 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005421 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005422 if (value.IsRegister()) {
5423 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005424 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005425 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005426 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005427 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005428 break;
5429 }
5430
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005431 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005432 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005433 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005434
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005435 if (!value.IsRegister()) {
5436 // Just setting null.
5437 DCHECK(instruction->InputAt(2)->IsNullConstant());
5438 DCHECK(value.IsConstant()) << value;
5439 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005440 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005441 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005442 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005443 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005444 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005445
5446 DCHECK(needs_write_barrier);
5447 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005448 // We cannot use a NearLabel for `done`, as its range may be too
5449 // short when Baker read barriers are enabled.
5450 Label done;
5451 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005452 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005453 Location temp_loc = locations->GetTemp(0);
5454 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005455 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005456 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5457 codegen_->AddSlowPath(slow_path);
5458 if (instruction->GetValueCanBeNull()) {
5459 __ testl(register_value, register_value);
5460 __ j(kNotEqual, &not_null);
5461 __ movl(address, Immediate(0));
5462 codegen_->MaybeRecordImplicitNullCheck(instruction);
5463 __ jmp(&done);
5464 __ Bind(&not_null);
5465 }
5466
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005467 // Note that when Baker read barriers are enabled, the type
5468 // checks are performed without read barriers. This is fine,
5469 // even in the case where a class object is in the from-space
5470 // after the flip, as a comparison involving such a type would
5471 // not produce a false positive; it may of course produce a
5472 // false negative, in which case we would take the ArraySet
5473 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005474
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005475 // /* HeapReference<Class> */ temp = array->klass_
5476 __ movl(temp, Address(array, class_offset));
5477 codegen_->MaybeRecordImplicitNullCheck(instruction);
5478 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005479
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005480 // /* HeapReference<Class> */ temp = temp->component_type_
5481 __ movl(temp, Address(temp, component_offset));
5482 // If heap poisoning is enabled, no need to unpoison `temp`
5483 // nor the object reference in `register_value->klass`, as
5484 // we are comparing two poisoned references.
5485 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005486
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005487 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5488 __ j(kEqual, &do_put);
5489 // If heap poisoning is enabled, the `temp` reference has
5490 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005491 __ MaybeUnpoisonHeapReference(temp);
5492
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005493 // If heap poisoning is enabled, no need to unpoison the
5494 // heap reference loaded below, as it is only used for a
5495 // comparison with null.
5496 __ cmpl(Address(temp, super_offset), Immediate(0));
5497 __ j(kNotEqual, slow_path->GetEntryLabel());
5498 __ Bind(&do_put);
5499 } else {
5500 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005501 }
5502 }
5503
5504 if (kPoisonHeapReferences) {
5505 __ movl(temp, register_value);
5506 __ PoisonHeapReference(temp);
5507 __ movl(address, temp);
5508 } else {
5509 __ movl(address, register_value);
5510 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005511 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005512 codegen_->MaybeRecordImplicitNullCheck(instruction);
5513 }
5514
5515 Register card = locations->GetTemp(1).AsRegister<Register>();
5516 codegen_->MarkGCCard(
5517 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5518 __ Bind(&done);
5519
5520 if (slow_path != nullptr) {
5521 __ Bind(slow_path->GetExitLabel());
5522 }
5523
5524 break;
5525 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005526
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005527 case Primitive::kPrimInt: {
5528 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005529 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005530 if (value.IsRegister()) {
5531 __ movl(address, value.AsRegister<Register>());
5532 } else {
5533 DCHECK(value.IsConstant()) << value;
5534 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5535 __ movl(address, Immediate(v));
5536 }
5537 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005538 break;
5539 }
5540
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005541 case Primitive::kPrimLong: {
5542 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005543 if (value.IsRegisterPair()) {
5544 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5545 value.AsRegisterPairLow<Register>());
5546 codegen_->MaybeRecordImplicitNullCheck(instruction);
5547 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5548 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005549 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005550 DCHECK(value.IsConstant());
5551 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5552 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5553 Immediate(Low32Bits(val)));
5554 codegen_->MaybeRecordImplicitNullCheck(instruction);
5555 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5556 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005557 }
5558 break;
5559 }
5560
Mark Mendell7c8d0092015-01-26 11:21:33 -05005561 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005562 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005563 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005564 if (value.IsFpuRegister()) {
5565 __ movss(address, value.AsFpuRegister<XmmRegister>());
5566 } else {
5567 DCHECK(value.IsConstant());
5568 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5569 __ movl(address, Immediate(v));
5570 }
5571 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005572 break;
5573 }
5574
5575 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005576 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005577 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005578 if (value.IsFpuRegister()) {
5579 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5580 } else {
5581 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005582 Address address_hi =
5583 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005584 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5585 __ movl(address, Immediate(Low32Bits(v)));
5586 codegen_->MaybeRecordImplicitNullCheck(instruction);
5587 __ movl(address_hi, Immediate(High32Bits(v)));
5588 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005589 break;
5590 }
5591
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005592 case Primitive::kPrimVoid:
5593 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005594 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005595 }
5596}
5597
5598void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5599 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005600 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005601 if (!instruction->IsEmittedAtUseSite()) {
5602 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5603 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005604}
5605
5606void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005607 if (instruction->IsEmittedAtUseSite()) {
5608 return;
5609 }
5610
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005611 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005612 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005613 Register obj = locations->InAt(0).AsRegister<Register>();
5614 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005615 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005616 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005617 // Mask out most significant bit in case the array is String's array of char.
5618 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005619 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005620 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005621}
5622
5623void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005624 RegisterSet caller_saves = RegisterSet::Empty();
5625 InvokeRuntimeCallingConvention calling_convention;
5626 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5627 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5628 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005629 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005630 HInstruction* length = instruction->InputAt(1);
5631 if (!length->IsEmittedAtUseSite()) {
5632 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5633 }
jessicahandojo4877b792016-09-08 19:49:13 -07005634 // Need register to see array's length.
5635 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5636 locations->AddTemp(Location::RequiresRegister());
5637 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005638}
5639
5640void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005641 const bool is_string_compressed_char_at =
5642 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005643 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005644 Location index_loc = locations->InAt(0);
5645 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005646 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005647 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005648
Mark Mendell99dbd682015-04-22 16:18:52 -04005649 if (length_loc.IsConstant()) {
5650 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5651 if (index_loc.IsConstant()) {
5652 // BCE will remove the bounds check if we are guarenteed to pass.
5653 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5654 if (index < 0 || index >= length) {
5655 codegen_->AddSlowPath(slow_path);
5656 __ jmp(slow_path->GetEntryLabel());
5657 } else {
5658 // Some optimization after BCE may have generated this, and we should not
5659 // generate a bounds check if it is a valid range.
5660 }
5661 return;
5662 }
5663
5664 // We have to reverse the jump condition because the length is the constant.
5665 Register index_reg = index_loc.AsRegister<Register>();
5666 __ cmpl(index_reg, Immediate(length));
5667 codegen_->AddSlowPath(slow_path);
5668 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005669 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005670 HInstruction* array_length = instruction->InputAt(1);
5671 if (array_length->IsEmittedAtUseSite()) {
5672 // Address the length field in the array.
5673 DCHECK(array_length->IsArrayLength());
5674 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5675 Location array_loc = array_length->GetLocations()->InAt(0);
5676 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005677 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005678 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5679 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005680 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5681 __ movl(length_reg, array_len);
5682 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005683 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005684 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005685 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005686 // Checking bounds for general case:
5687 // Array of char or string's array with feature compression off.
5688 if (index_loc.IsConstant()) {
5689 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5690 __ cmpl(array_len, Immediate(value));
5691 } else {
5692 __ cmpl(array_len, index_loc.AsRegister<Register>());
5693 }
5694 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005695 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005696 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005697 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005698 }
5699 codegen_->AddSlowPath(slow_path);
5700 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005701 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005702}
5703
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005704void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005705 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005706}
5707
5708void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005709 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5710}
5711
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005712void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005713 LocationSummary* locations =
5714 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005715 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005716}
5717
5718void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005719 HBasicBlock* block = instruction->GetBlock();
5720 if (block->GetLoopInformation() != nullptr) {
5721 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5722 // The back edge will generate the suspend check.
5723 return;
5724 }
5725 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5726 // The goto will generate the suspend check.
5727 return;
5728 }
5729 GenerateSuspendCheck(instruction, nullptr);
5730}
5731
5732void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5733 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005734 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005735 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5736 if (slow_path == nullptr) {
5737 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5738 instruction->SetSlowPath(slow_path);
5739 codegen_->AddSlowPath(slow_path);
5740 if (successor != nullptr) {
5741 DCHECK(successor->IsLoopHeader());
5742 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5743 }
5744 } else {
5745 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5746 }
5747
Andreas Gampe542451c2016-07-26 09:02:02 -07005748 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005749 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005750 if (successor == nullptr) {
5751 __ j(kNotEqual, slow_path->GetEntryLabel());
5752 __ Bind(slow_path->GetReturnLabel());
5753 } else {
5754 __ j(kEqual, codegen_->GetLabelOf(successor));
5755 __ jmp(slow_path->GetEntryLabel());
5756 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005757}
5758
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005759X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5760 return codegen_->GetAssembler();
5761}
5762
Mark Mendell7c8d0092015-01-26 11:21:33 -05005763void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005764 ScratchRegisterScope ensure_scratch(
5765 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5766 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5767 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5768 __ movl(temp_reg, Address(ESP, src + stack_offset));
5769 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005770}
5771
5772void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005773 ScratchRegisterScope ensure_scratch(
5774 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5775 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5776 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5777 __ movl(temp_reg, Address(ESP, src + stack_offset));
5778 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5779 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5780 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005781}
5782
5783void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005784 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005785 Location source = move->GetSource();
5786 Location destination = move->GetDestination();
5787
5788 if (source.IsRegister()) {
5789 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005790 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005791 } else if (destination.IsFpuRegister()) {
5792 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005793 } else {
5794 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005795 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005796 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005797 } else if (source.IsRegisterPair()) {
5798 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5799 // Create stack space for 2 elements.
5800 __ subl(ESP, Immediate(2 * elem_size));
5801 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5802 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5803 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5804 // And remove the temporary stack space we allocated.
5805 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005806 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005807 if (destination.IsRegister()) {
5808 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5809 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005810 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005811 } else if (destination.IsRegisterPair()) {
5812 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5813 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5814 __ psrlq(src_reg, Immediate(32));
5815 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005816 } else if (destination.IsStackSlot()) {
5817 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5818 } else {
5819 DCHECK(destination.IsDoubleStackSlot());
5820 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5821 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005822 } else if (source.IsStackSlot()) {
5823 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005824 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005825 } else if (destination.IsFpuRegister()) {
5826 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005827 } else {
5828 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005829 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5830 }
5831 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005832 if (destination.IsRegisterPair()) {
5833 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5834 __ movl(destination.AsRegisterPairHigh<Register>(),
5835 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5836 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005837 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5838 } else {
5839 DCHECK(destination.IsDoubleStackSlot()) << destination;
5840 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005841 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005842 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005843 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005844 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005845 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005846 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005847 if (value == 0) {
5848 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5849 } else {
5850 __ movl(destination.AsRegister<Register>(), Immediate(value));
5851 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005852 } else {
5853 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005854 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005855 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005856 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005857 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005858 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005859 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005860 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005861 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5862 if (value == 0) {
5863 // Easy handling of 0.0.
5864 __ xorps(dest, dest);
5865 } else {
5866 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005867 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5868 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5869 __ movl(temp, Immediate(value));
5870 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005871 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005872 } else {
5873 DCHECK(destination.IsStackSlot()) << destination;
5874 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5875 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005876 } else if (constant->IsLongConstant()) {
5877 int64_t value = constant->AsLongConstant()->GetValue();
5878 int32_t low_value = Low32Bits(value);
5879 int32_t high_value = High32Bits(value);
5880 Immediate low(low_value);
5881 Immediate high(high_value);
5882 if (destination.IsDoubleStackSlot()) {
5883 __ movl(Address(ESP, destination.GetStackIndex()), low);
5884 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5885 } else {
5886 __ movl(destination.AsRegisterPairLow<Register>(), low);
5887 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5888 }
5889 } else {
5890 DCHECK(constant->IsDoubleConstant());
5891 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005892 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005893 int32_t low_value = Low32Bits(value);
5894 int32_t high_value = High32Bits(value);
5895 Immediate low(low_value);
5896 Immediate high(high_value);
5897 if (destination.IsFpuRegister()) {
5898 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5899 if (value == 0) {
5900 // Easy handling of 0.0.
5901 __ xorpd(dest, dest);
5902 } else {
5903 __ pushl(high);
5904 __ pushl(low);
5905 __ movsd(dest, Address(ESP, 0));
5906 __ addl(ESP, Immediate(8));
5907 }
5908 } else {
5909 DCHECK(destination.IsDoubleStackSlot()) << destination;
5910 __ movl(Address(ESP, destination.GetStackIndex()), low);
5911 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5912 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005913 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005914 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005915 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005916 }
5917}
5918
Mark Mendella5c19ce2015-04-01 12:51:05 -04005919void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005920 Register suggested_scratch = reg == EAX ? EBX : EAX;
5921 ScratchRegisterScope ensure_scratch(
5922 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5923
5924 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5925 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5926 __ movl(Address(ESP, mem + stack_offset), reg);
5927 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005928}
5929
Mark Mendell7c8d0092015-01-26 11:21:33 -05005930void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005931 ScratchRegisterScope ensure_scratch(
5932 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5933
5934 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5935 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5936 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5937 __ movss(Address(ESP, mem + stack_offset), reg);
5938 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005939}
5940
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005941void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005942 ScratchRegisterScope ensure_scratch1(
5943 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005944
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005945 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5946 ScratchRegisterScope ensure_scratch2(
5947 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005948
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005949 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5950 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5951 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5952 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5953 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5954 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005955}
5956
5957void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005958 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005959 Location source = move->GetSource();
5960 Location destination = move->GetDestination();
5961
5962 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005963 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5964 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5965 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5966 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5967 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005968 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005969 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005970 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005971 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005972 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5973 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005974 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5975 // Use XOR Swap algorithm to avoid a temporary.
5976 DCHECK_NE(source.reg(), destination.reg());
5977 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5978 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5979 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5980 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5981 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5982 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5983 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005984 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5985 // Take advantage of the 16 bytes in the XMM register.
5986 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5987 Address stack(ESP, destination.GetStackIndex());
5988 // Load the double into the high doubleword.
5989 __ movhpd(reg, stack);
5990
5991 // Store the low double into the destination.
5992 __ movsd(stack, reg);
5993
5994 // Move the high double to the low double.
5995 __ psrldq(reg, Immediate(8));
5996 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5997 // Take advantage of the 16 bytes in the XMM register.
5998 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5999 Address stack(ESP, source.GetStackIndex());
6000 // Load the double into the high doubleword.
6001 __ movhpd(reg, stack);
6002
6003 // Store the low double into the destination.
6004 __ movsd(stack, reg);
6005
6006 // Move the high double to the low double.
6007 __ psrldq(reg, Immediate(8));
6008 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
6009 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6010 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006011 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006012 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006013 }
6014}
6015
6016void ParallelMoveResolverX86::SpillScratch(int reg) {
6017 __ pushl(static_cast<Register>(reg));
6018}
6019
6020void ParallelMoveResolverX86::RestoreScratch(int reg) {
6021 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006022}
6023
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006024HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6025 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006026 switch (desired_class_load_kind) {
6027 case HLoadClass::LoadKind::kReferrersClass:
6028 break;
6029 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6030 DCHECK(!GetCompilerOptions().GetCompilePic());
6031 break;
6032 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6033 DCHECK(GetCompilerOptions().GetCompilePic());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006034 FALLTHROUGH_INTENDED;
6035 case HLoadClass::LoadKind::kBssEntry:
6036 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006037 break;
6038 case HLoadClass::LoadKind::kBootImageAddress:
6039 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006040 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006041 DCHECK(Runtime::Current()->UseJitCompilation());
6042 break;
6043 case HLoadClass::LoadKind::kDexCacheViaMethod:
6044 break;
6045 }
6046 return desired_class_load_kind;
6047}
6048
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006049void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006050 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6051 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006052 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006053 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006054 cls,
6055 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006056 Location::RegisterLocation(EAX));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006057 return;
6058 }
Vladimir Marko41559982017-01-06 14:04:23 +00006059 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006060
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006061 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6062 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006063 ? LocationSummary::kCallOnSlowPath
6064 : LocationSummary::kNoCall;
6065 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006066 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006067 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006068 }
6069
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006070 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006071 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6072 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006073 locations->SetInAt(0, Location::RequiresRegister());
6074 }
6075 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006076}
6077
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006078Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6079 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006080 Handle<mirror::Class> handle) {
6081 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
6082 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006083 // Add a patch entry and return the label.
6084 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6085 PatchInfo<Label>* info = &jit_class_patches_.back();
6086 return &info->label;
6087}
6088
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006089// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6090// move.
6091void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006092 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6093 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6094 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006095 return;
6096 }
Vladimir Marko41559982017-01-06 14:04:23 +00006097 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006098
Vladimir Marko41559982017-01-06 14:04:23 +00006099 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006100 Location out_loc = locations->Out();
6101 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006102
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006103 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006104 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6105 ? kWithoutReadBarrier
6106 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006107 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006108 case HLoadClass::LoadKind::kReferrersClass: {
6109 DCHECK(!cls->CanCallRuntime());
6110 DCHECK(!cls->MustGenerateClinitCheck());
6111 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6112 Register current_method = locations->InAt(0).AsRegister<Register>();
6113 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006114 cls,
6115 out_loc,
6116 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006117 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006118 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006119 break;
6120 }
6121 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006122 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006123 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006124 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006125 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006126 break;
6127 }
6128 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006129 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006130 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006131 Register method_address = locations->InAt(0).AsRegister<Register>();
6132 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006133 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006134 break;
6135 }
6136 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006137 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006138 uint32_t address = dchecked_integral_cast<uint32_t>(
6139 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6140 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006141 __ movl(out, Immediate(address));
6142 codegen_->RecordSimplePatch();
6143 break;
6144 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006145 case HLoadClass::LoadKind::kBssEntry: {
6146 Register method_address = locations->InAt(0).AsRegister<Register>();
6147 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6148 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6149 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6150 generate_null_check = true;
6151 break;
6152 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006153 case HLoadClass::LoadKind::kJitTableAddress: {
6154 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6155 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006156 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006157 // /* GcRoot<mirror::Class> */ out = *address
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006158 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006159 break;
6160 }
Vladimir Marko41559982017-01-06 14:04:23 +00006161 case HLoadClass::LoadKind::kDexCacheViaMethod:
6162 LOG(FATAL) << "UNREACHABLE";
6163 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006164 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006165
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006166 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6167 DCHECK(cls->CanCallRuntime());
6168 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6169 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6170 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006171
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006172 if (generate_null_check) {
6173 __ testl(out, out);
6174 __ j(kEqual, slow_path->GetEntryLabel());
6175 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006176
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006177 if (cls->MustGenerateClinitCheck()) {
6178 GenerateClassInitializationCheck(slow_path, out);
6179 } else {
6180 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006181 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006182 }
6183}
6184
6185void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6186 LocationSummary* locations =
6187 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6188 locations->SetInAt(0, Location::RequiresRegister());
6189 if (check->HasUses()) {
6190 locations->SetOut(Location::SameAsFirstInput());
6191 }
6192}
6193
6194void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006195 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006196 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006197 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006198 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006199 GenerateClassInitializationCheck(slow_path,
6200 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006201}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006202
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006203void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006204 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006205 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6206 Immediate(mirror::Class::kStatusInitialized));
6207 __ j(kLess, slow_path->GetEntryLabel());
6208 __ Bind(slow_path->GetExitLabel());
6209 // No need for memory fence, thanks to the X86 memory model.
6210}
6211
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006212HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6213 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006214 switch (desired_string_load_kind) {
6215 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6216 DCHECK(!GetCompilerOptions().GetCompilePic());
6217 break;
6218 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6219 DCHECK(GetCompilerOptions().GetCompilePic());
6220 FALLTHROUGH_INTENDED;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006221 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006222 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006223 break;
6224 case HLoadString::LoadKind::kBootImageAddress:
6225 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006226 case HLoadString::LoadKind::kJitTableAddress:
6227 DCHECK(Runtime::Current()->UseJitCompilation());
6228 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006229 case HLoadString::LoadKind::kDexCacheViaMethod:
6230 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006231 }
6232 return desired_string_load_kind;
6233}
6234
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006235void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006236 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006237 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006238 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006239 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006240 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006241 locations->SetInAt(0, Location::RequiresRegister());
6242 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006243 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6244 locations->SetOut(Location::RegisterLocation(EAX));
6245 } else {
6246 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006247 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6248 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6249 // Rely on the pResolveString and/or marking to save everything.
6250 RegisterSet caller_saves = RegisterSet::Empty();
6251 InvokeRuntimeCallingConvention calling_convention;
6252 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6253 locations->SetCustomSlowPathCallerSaves(caller_saves);
6254 } else {
6255 // For non-Baker read barrier we have a temp-clobbering call.
6256 }
6257 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006258 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006259}
6260
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006261Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006262 dex::StringIndex dex_index,
6263 Handle<mirror::String> handle) {
6264 jit_string_roots_.Overwrite(
6265 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006266 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006267 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006268 PatchInfo<Label>* info = &jit_string_patches_.back();
6269 return &info->label;
6270}
6271
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006272// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6273// move.
6274void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006275 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006276 Location out_loc = locations->Out();
6277 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006278
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006279 switch (load->GetLoadKind()) {
6280 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006281 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006282 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006283 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006284 return; // No dex cache slow path.
6285 }
6286 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006287 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006288 Register method_address = locations->InAt(0).AsRegister<Register>();
6289 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006290 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006291 return; // No dex cache slow path.
6292 }
6293 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006294 uint32_t address = dchecked_integral_cast<uint32_t>(
6295 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6296 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006297 __ movl(out, Immediate(address));
6298 codegen_->RecordSimplePatch();
6299 return; // No dex cache slow path.
6300 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006301 case HLoadString::LoadKind::kBssEntry: {
6302 Register method_address = locations->InAt(0).AsRegister<Register>();
6303 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6304 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006305 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006306 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006307 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6308 codegen_->AddSlowPath(slow_path);
6309 __ testl(out, out);
6310 __ j(kEqual, slow_path->GetEntryLabel());
6311 __ Bind(slow_path->GetExitLabel());
6312 return;
6313 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006314 case HLoadString::LoadKind::kJitTableAddress: {
6315 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6316 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006317 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006318 // /* GcRoot<mirror::String> */ out = *address
6319 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6320 return;
6321 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006322 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006323 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006324 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006325
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006326 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006327 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006328 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006329 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006330 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6331 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006332}
6333
David Brazdilcb1c0552015-08-04 16:22:25 +01006334static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006335 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006336}
6337
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006338void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6339 LocationSummary* locations =
6340 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6341 locations->SetOut(Location::RequiresRegister());
6342}
6343
6344void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006345 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6346}
6347
6348void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6349 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6350}
6351
6352void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6353 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006354}
6355
6356void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6357 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006358 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006359 InvokeRuntimeCallingConvention calling_convention;
6360 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6361}
6362
6363void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006364 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006365 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006366}
6367
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006368// Temp is used for read barrier.
6369static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6370 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006371 !kUseBakerReadBarrier &&
6372 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006373 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006374 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6375 return 1;
6376 }
6377 return 0;
6378}
6379
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006380// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006381// interface pointer, one for loading the current interface.
6382// The other checks have one temp for loading the object's class.
6383static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6384 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6385 return 2;
6386 }
6387 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006388}
6389
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006390void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006391 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006392 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006393 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006394 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006395 case TypeCheckKind::kExactCheck:
6396 case TypeCheckKind::kAbstractClassCheck:
6397 case TypeCheckKind::kClassHierarchyCheck:
6398 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006399 call_kind =
6400 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006401 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006402 break;
6403 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006404 case TypeCheckKind::kUnresolvedCheck:
6405 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406 call_kind = LocationSummary::kCallOnSlowPath;
6407 break;
6408 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006409
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006410 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006411 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006412 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006413 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006414 locations->SetInAt(0, Location::RequiresRegister());
6415 locations->SetInAt(1, Location::Any());
6416 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6417 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006418 // When read barriers are enabled, we need a temporary register for some cases.
6419 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006420}
6421
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006422void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006423 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006424 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006425 Location obj_loc = locations->InAt(0);
6426 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006427 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006428 Location out_loc = locations->Out();
6429 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006430 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6431 DCHECK_LE(num_temps, 1u);
6432 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006433 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006434 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6435 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6436 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006437 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006438 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006439
6440 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006441 // Avoid null check if we know obj is not null.
6442 if (instruction->MustDoNullCheck()) {
6443 __ testl(obj, obj);
6444 __ j(kEqual, &zero);
6445 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006446
Roland Levillain7c1559a2015-12-15 10:55:36 +00006447 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006448 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006449 // /* HeapReference<Class> */ out = obj->klass_
6450 GenerateReferenceLoadTwoRegisters(instruction,
6451 out_loc,
6452 obj_loc,
6453 class_offset,
6454 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006455 if (cls.IsRegister()) {
6456 __ cmpl(out, cls.AsRegister<Register>());
6457 } else {
6458 DCHECK(cls.IsStackSlot()) << cls;
6459 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6460 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006461
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006462 // Classes must be equal for the instanceof to succeed.
6463 __ j(kNotEqual, &zero);
6464 __ movl(out, Immediate(1));
6465 __ jmp(&done);
6466 break;
6467 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006468
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006469 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006470 // /* HeapReference<Class> */ out = obj->klass_
6471 GenerateReferenceLoadTwoRegisters(instruction,
6472 out_loc,
6473 obj_loc,
6474 class_offset,
6475 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006476 // If the class is abstract, we eagerly fetch the super class of the
6477 // object to avoid doing a comparison we know will fail.
6478 NearLabel loop;
6479 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006480 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006481 GenerateReferenceLoadOneRegister(instruction,
6482 out_loc,
6483 super_offset,
6484 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006485 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006486 __ testl(out, out);
6487 // If `out` is null, we use it for the result, and jump to `done`.
6488 __ j(kEqual, &done);
6489 if (cls.IsRegister()) {
6490 __ cmpl(out, cls.AsRegister<Register>());
6491 } else {
6492 DCHECK(cls.IsStackSlot()) << cls;
6493 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6494 }
6495 __ j(kNotEqual, &loop);
6496 __ movl(out, Immediate(1));
6497 if (zero.IsLinked()) {
6498 __ jmp(&done);
6499 }
6500 break;
6501 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006502
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006503 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006504 // /* HeapReference<Class> */ out = obj->klass_
6505 GenerateReferenceLoadTwoRegisters(instruction,
6506 out_loc,
6507 obj_loc,
6508 class_offset,
6509 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006510 // Walk over the class hierarchy to find a match.
6511 NearLabel loop, success;
6512 __ Bind(&loop);
6513 if (cls.IsRegister()) {
6514 __ cmpl(out, cls.AsRegister<Register>());
6515 } else {
6516 DCHECK(cls.IsStackSlot()) << cls;
6517 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6518 }
6519 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006520 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006521 GenerateReferenceLoadOneRegister(instruction,
6522 out_loc,
6523 super_offset,
6524 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006525 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006526 __ testl(out, out);
6527 __ j(kNotEqual, &loop);
6528 // If `out` is null, we use it for the result, and jump to `done`.
6529 __ jmp(&done);
6530 __ Bind(&success);
6531 __ movl(out, Immediate(1));
6532 if (zero.IsLinked()) {
6533 __ jmp(&done);
6534 }
6535 break;
6536 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006537
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006538 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006539 // /* HeapReference<Class> */ out = obj->klass_
6540 GenerateReferenceLoadTwoRegisters(instruction,
6541 out_loc,
6542 obj_loc,
6543 class_offset,
6544 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006545 // Do an exact check.
6546 NearLabel exact_check;
6547 if (cls.IsRegister()) {
6548 __ cmpl(out, cls.AsRegister<Register>());
6549 } else {
6550 DCHECK(cls.IsStackSlot()) << cls;
6551 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6552 }
6553 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006554 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006555 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006556 GenerateReferenceLoadOneRegister(instruction,
6557 out_loc,
6558 component_offset,
6559 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006560 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006561 __ testl(out, out);
6562 // If `out` is null, we use it for the result, and jump to `done`.
6563 __ j(kEqual, &done);
6564 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6565 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006566 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006567 __ movl(out, Immediate(1));
6568 __ jmp(&done);
6569 break;
6570 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006571
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006572 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006573 // No read barrier since the slow path will retry upon failure.
6574 // /* HeapReference<Class> */ out = obj->klass_
6575 GenerateReferenceLoadTwoRegisters(instruction,
6576 out_loc,
6577 obj_loc,
6578 class_offset,
6579 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006580 if (cls.IsRegister()) {
6581 __ cmpl(out, cls.AsRegister<Register>());
6582 } else {
6583 DCHECK(cls.IsStackSlot()) << cls;
6584 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6585 }
6586 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006587 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6588 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006589 codegen_->AddSlowPath(slow_path);
6590 __ j(kNotEqual, slow_path->GetEntryLabel());
6591 __ movl(out, Immediate(1));
6592 if (zero.IsLinked()) {
6593 __ jmp(&done);
6594 }
6595 break;
6596 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006597
Calin Juravle98893e12015-10-02 21:05:03 +01006598 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006599 case TypeCheckKind::kInterfaceCheck: {
6600 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006601 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006602 // cases.
6603 //
6604 // We cannot directly call the InstanceofNonTrivial runtime
6605 // entry point without resorting to a type checking slow path
6606 // here (i.e. by calling InvokeRuntime directly), as it would
6607 // require to assign fixed registers for the inputs of this
6608 // HInstanceOf instruction (following the runtime calling
6609 // convention), which might be cluttered by the potential first
6610 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006611 //
6612 // TODO: Introduce a new runtime entry point taking the object
6613 // to test (instead of its class) as argument, and let it deal
6614 // with the read barrier issues. This will let us refactor this
6615 // case of the `switch` code as it was previously (with a direct
6616 // call to the runtime not using a type checking slow path).
6617 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006618 DCHECK(locations->OnlyCallsOnSlowPath());
6619 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6620 /* is_fatal */ false);
6621 codegen_->AddSlowPath(slow_path);
6622 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006623 if (zero.IsLinked()) {
6624 __ jmp(&done);
6625 }
6626 break;
6627 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006628 }
6629
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006630 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006631 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006632 __ xorl(out, out);
6633 }
6634
6635 if (done.IsLinked()) {
6636 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006637 }
6638
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006639 if (slow_path != nullptr) {
6640 __ Bind(slow_path->GetExitLabel());
6641 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006642}
6643
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006644static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6645 switch (type_check_kind) {
6646 case TypeCheckKind::kExactCheck:
6647 case TypeCheckKind::kAbstractClassCheck:
6648 case TypeCheckKind::kClassHierarchyCheck:
6649 case TypeCheckKind::kArrayObjectCheck:
6650 return !throws_into_catch && !kEmitCompilerReadBarrier;
6651 case TypeCheckKind::kInterfaceCheck:
6652 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6653 case TypeCheckKind::kArrayCheck:
6654 case TypeCheckKind::kUnresolvedCheck:
6655 return false;
6656 }
6657 LOG(FATAL) << "Unreachable";
6658 UNREACHABLE();
6659}
6660
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006661void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006662 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006663 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006664 LocationSummary::CallKind call_kind =
6665 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6666 ? LocationSummary::kNoCall
6667 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006668 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6669 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006670 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6671 // Require a register for the interface check since there is a loop that compares the class to
6672 // a memory address.
6673 locations->SetInAt(1, Location::RequiresRegister());
6674 } else {
6675 locations->SetInAt(1, Location::Any());
6676 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006677 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6678 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006679 // When read barriers are enabled, we need an additional temporary register for some cases.
6680 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6681}
6682
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006683void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006684 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006685 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006686 Location obj_loc = locations->InAt(0);
6687 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006688 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006689 Location temp_loc = locations->GetTemp(0);
6690 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006691 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6692 DCHECK_GE(num_temps, 1u);
6693 DCHECK_LE(num_temps, 2u);
6694 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6695 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6696 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6697 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6698 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6699 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6700 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6701 const uint32_t object_array_data_offset =
6702 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006703
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006704 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6705 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6706 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006707 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006708 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6709
Roland Levillain0d5a2812015-11-13 10:07:31 +00006710 SlowPathCode* type_check_slow_path =
6711 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6712 is_type_check_slow_path_fatal);
6713 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006714
Roland Levillain0d5a2812015-11-13 10:07:31 +00006715 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006716 // Avoid null check if we know obj is not null.
6717 if (instruction->MustDoNullCheck()) {
6718 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006719 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006720 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006721
Roland Levillain0d5a2812015-11-13 10:07:31 +00006722 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006723 case TypeCheckKind::kExactCheck:
6724 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006725 // /* HeapReference<Class> */ temp = obj->klass_
6726 GenerateReferenceLoadTwoRegisters(instruction,
6727 temp_loc,
6728 obj_loc,
6729 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006730 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006731
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006732 if (cls.IsRegister()) {
6733 __ cmpl(temp, cls.AsRegister<Register>());
6734 } else {
6735 DCHECK(cls.IsStackSlot()) << cls;
6736 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6737 }
6738 // Jump to slow path for throwing the exception or doing a
6739 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006740 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006741 break;
6742 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006743
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006744 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006745 // /* HeapReference<Class> */ temp = obj->klass_
6746 GenerateReferenceLoadTwoRegisters(instruction,
6747 temp_loc,
6748 obj_loc,
6749 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006750 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006751
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006752 // If the class is abstract, we eagerly fetch the super class of the
6753 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006754 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006755 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006756 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006757 GenerateReferenceLoadOneRegister(instruction,
6758 temp_loc,
6759 super_offset,
6760 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006761 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006762
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006763 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6764 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006765 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006766 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006767
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006768 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006769 if (cls.IsRegister()) {
6770 __ cmpl(temp, cls.AsRegister<Register>());
6771 } else {
6772 DCHECK(cls.IsStackSlot()) << cls;
6773 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6774 }
6775 __ j(kNotEqual, &loop);
6776 break;
6777 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006778
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006779 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006780 // /* HeapReference<Class> */ temp = obj->klass_
6781 GenerateReferenceLoadTwoRegisters(instruction,
6782 temp_loc,
6783 obj_loc,
6784 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006785 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006786
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006787 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006788 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006789 __ Bind(&loop);
6790 if (cls.IsRegister()) {
6791 __ cmpl(temp, cls.AsRegister<Register>());
6792 } else {
6793 DCHECK(cls.IsStackSlot()) << cls;
6794 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6795 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006796 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006797
Roland Levillain0d5a2812015-11-13 10:07:31 +00006798 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006799 GenerateReferenceLoadOneRegister(instruction,
6800 temp_loc,
6801 super_offset,
6802 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006803 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006804
6805 // If the class reference currently in `temp` is not null, jump
6806 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006807 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006808 __ j(kNotZero, &loop);
6809 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006810 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006811 break;
6812 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006813
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006814 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006815 // /* HeapReference<Class> */ temp = obj->klass_
6816 GenerateReferenceLoadTwoRegisters(instruction,
6817 temp_loc,
6818 obj_loc,
6819 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006820 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006821
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006822 // Do an exact check.
6823 if (cls.IsRegister()) {
6824 __ cmpl(temp, cls.AsRegister<Register>());
6825 } else {
6826 DCHECK(cls.IsStackSlot()) << cls;
6827 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6828 }
6829 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006830
6831 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006832 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006833 GenerateReferenceLoadOneRegister(instruction,
6834 temp_loc,
6835 component_offset,
6836 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006837 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006838
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006839 // If the component type is null (i.e. the object not an array), jump to the slow path to
6840 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006841 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006842 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006843
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006844 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006845 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006846 break;
6847 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006848
Calin Juravle98893e12015-10-02 21:05:03 +01006849 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006850 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006851 // We cannot directly call the CheckCast runtime entry point
6852 // without resorting to a type checking slow path here (i.e. by
6853 // calling InvokeRuntime directly), as it would require to
6854 // assign fixed registers for the inputs of this HInstanceOf
6855 // instruction (following the runtime calling convention), which
6856 // might be cluttered by the potential first read barrier
6857 // emission at the beginning of this method.
6858 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006859 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006860
6861 case TypeCheckKind::kInterfaceCheck: {
6862 // Fast path for the interface check. Since we compare with a memory location in the inner
6863 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6864 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006865 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006866 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006867 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6868 // doing this.
6869 // /* HeapReference<Class> */ temp = obj->klass_
6870 GenerateReferenceLoadTwoRegisters(instruction,
6871 temp_loc,
6872 obj_loc,
6873 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006874 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006875
6876 // /* HeapReference<Class> */ temp = temp->iftable_
6877 GenerateReferenceLoadTwoRegisters(instruction,
6878 temp_loc,
6879 temp_loc,
6880 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006881 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006882 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006883 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006884 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006885 NearLabel start_loop;
6886 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006887 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006888 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006889 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6890 // Go to next interface if the classes do not match.
6891 __ cmpl(cls.AsRegister<Register>(),
6892 CodeGeneratorX86::ArrayAddress(temp,
6893 maybe_temp2_loc,
6894 TIMES_4,
6895 object_array_data_offset));
6896 __ j(kNotEqual, &start_loop);
6897 } else {
6898 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006899 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006900 break;
6901 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006902 }
6903 __ Bind(&done);
6904
Roland Levillain0d5a2812015-11-13 10:07:31 +00006905 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006906}
6907
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006908void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6909 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006910 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006911 InvokeRuntimeCallingConvention calling_convention;
6912 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6913}
6914
6915void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006916 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6917 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006918 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006919 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006920 if (instruction->IsEnter()) {
6921 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6922 } else {
6923 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6924 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006925}
6926
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006927void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6928void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6929void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6930
6931void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6932 LocationSummary* locations =
6933 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6934 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6935 || instruction->GetResultType() == Primitive::kPrimLong);
6936 locations->SetInAt(0, Location::RequiresRegister());
6937 locations->SetInAt(1, Location::Any());
6938 locations->SetOut(Location::SameAsFirstInput());
6939}
6940
6941void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6942 HandleBitwiseOperation(instruction);
6943}
6944
6945void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6946 HandleBitwiseOperation(instruction);
6947}
6948
6949void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6950 HandleBitwiseOperation(instruction);
6951}
6952
6953void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6954 LocationSummary* locations = instruction->GetLocations();
6955 Location first = locations->InAt(0);
6956 Location second = locations->InAt(1);
6957 DCHECK(first.Equals(locations->Out()));
6958
6959 if (instruction->GetResultType() == Primitive::kPrimInt) {
6960 if (second.IsRegister()) {
6961 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006962 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006963 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006964 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006965 } else {
6966 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006967 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006968 }
6969 } else if (second.IsConstant()) {
6970 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006971 __ andl(first.AsRegister<Register>(),
6972 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006973 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006974 __ orl(first.AsRegister<Register>(),
6975 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006976 } else {
6977 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006978 __ xorl(first.AsRegister<Register>(),
6979 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006980 }
6981 } else {
6982 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006983 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006984 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006985 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006986 } else {
6987 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006988 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006989 }
6990 }
6991 } else {
6992 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6993 if (second.IsRegisterPair()) {
6994 if (instruction->IsAnd()) {
6995 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6996 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6997 } else if (instruction->IsOr()) {
6998 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6999 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7000 } else {
7001 DCHECK(instruction->IsXor());
7002 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7003 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7004 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007005 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007006 if (instruction->IsAnd()) {
7007 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7008 __ andl(first.AsRegisterPairHigh<Register>(),
7009 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7010 } else if (instruction->IsOr()) {
7011 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7012 __ orl(first.AsRegisterPairHigh<Register>(),
7013 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7014 } else {
7015 DCHECK(instruction->IsXor());
7016 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7017 __ xorl(first.AsRegisterPairHigh<Register>(),
7018 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7019 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007020 } else {
7021 DCHECK(second.IsConstant()) << second;
7022 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007023 int32_t low_value = Low32Bits(value);
7024 int32_t high_value = High32Bits(value);
7025 Immediate low(low_value);
7026 Immediate high(high_value);
7027 Register first_low = first.AsRegisterPairLow<Register>();
7028 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007029 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007030 if (low_value == 0) {
7031 __ xorl(first_low, first_low);
7032 } else if (low_value != -1) {
7033 __ andl(first_low, low);
7034 }
7035 if (high_value == 0) {
7036 __ xorl(first_high, first_high);
7037 } else if (high_value != -1) {
7038 __ andl(first_high, high);
7039 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007040 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007041 if (low_value != 0) {
7042 __ orl(first_low, low);
7043 }
7044 if (high_value != 0) {
7045 __ orl(first_high, high);
7046 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007047 } else {
7048 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007049 if (low_value != 0) {
7050 __ xorl(first_low, low);
7051 }
7052 if (high_value != 0) {
7053 __ xorl(first_high, high);
7054 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007055 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007056 }
7057 }
7058}
7059
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007060void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7061 HInstruction* instruction,
7062 Location out,
7063 uint32_t offset,
7064 Location maybe_temp,
7065 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007066 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007067 if (read_barrier_option == kWithReadBarrier) {
7068 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007069 if (kUseBakerReadBarrier) {
7070 // Load with fast path based Baker's read barrier.
7071 // /* HeapReference<Object> */ out = *(out + offset)
7072 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007073 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007074 } else {
7075 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007076 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007077 // in the following move operation, as we will need it for the
7078 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007079 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007080 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007081 // /* HeapReference<Object> */ out = *(out + offset)
7082 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007083 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007084 }
7085 } else {
7086 // Plain load with no read barrier.
7087 // /* HeapReference<Object> */ out = *(out + offset)
7088 __ movl(out_reg, Address(out_reg, offset));
7089 __ MaybeUnpoisonHeapReference(out_reg);
7090 }
7091}
7092
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007093void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7094 HInstruction* instruction,
7095 Location out,
7096 Location obj,
7097 uint32_t offset,
7098 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007099 Register out_reg = out.AsRegister<Register>();
7100 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007101 if (read_barrier_option == kWithReadBarrier) {
7102 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007103 if (kUseBakerReadBarrier) {
7104 // Load with fast path based Baker's read barrier.
7105 // /* HeapReference<Object> */ out = *(obj + offset)
7106 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007107 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007108 } else {
7109 // Load with slow path based read barrier.
7110 // /* HeapReference<Object> */ out = *(obj + offset)
7111 __ movl(out_reg, Address(obj_reg, offset));
7112 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7113 }
7114 } else {
7115 // Plain load with no read barrier.
7116 // /* HeapReference<Object> */ out = *(obj + offset)
7117 __ movl(out_reg, Address(obj_reg, offset));
7118 __ MaybeUnpoisonHeapReference(out_reg);
7119 }
7120}
7121
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007122void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7123 HInstruction* instruction,
7124 Location root,
7125 const Address& address,
7126 Label* fixup_label,
7127 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007128 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007129 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007130 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007131 if (kUseBakerReadBarrier) {
7132 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7133 // Baker's read barrier are used:
7134 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007135 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00007136 // if (Thread::Current()->GetIsGcMarking()) {
7137 // root = ReadBarrier::Mark(root)
7138 // }
7139
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007140 // /* GcRoot<mirror::Object> */ root = *address
7141 __ movl(root_reg, address);
7142 if (fixup_label != nullptr) {
7143 __ Bind(fixup_label);
7144 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007145 static_assert(
7146 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7147 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7148 "have different sizes.");
7149 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7150 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7151 "have different sizes.");
7152
Vladimir Marko953437b2016-08-24 08:30:46 +00007153 // Slow path marking the GC root `root`.
7154 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007155 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007156 codegen_->AddSlowPath(slow_path);
7157
Andreas Gampe542451c2016-07-26 09:02:02 -07007158 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00007159 Immediate(0));
7160 __ j(kNotEqual, slow_path->GetEntryLabel());
7161 __ Bind(slow_path->GetExitLabel());
7162 } else {
7163 // GC root loaded through a slow path for read barriers other
7164 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007165 // /* GcRoot<mirror::Object>* */ root = address
7166 __ leal(root_reg, address);
7167 if (fixup_label != nullptr) {
7168 __ Bind(fixup_label);
7169 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007170 // /* mirror::Object* */ root = root->Read()
7171 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7172 }
7173 } else {
7174 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007175 // /* GcRoot<mirror::Object> */ root = *address
7176 __ movl(root_reg, address);
7177 if (fixup_label != nullptr) {
7178 __ Bind(fixup_label);
7179 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007180 // Note that GC roots are not affected by heap poisoning, thus we
7181 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007182 }
7183}
7184
7185void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7186 Location ref,
7187 Register obj,
7188 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007189 bool needs_null_check) {
7190 DCHECK(kEmitCompilerReadBarrier);
7191 DCHECK(kUseBakerReadBarrier);
7192
7193 // /* HeapReference<Object> */ ref = *(obj + offset)
7194 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007195 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007196}
7197
7198void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7199 Location ref,
7200 Register obj,
7201 uint32_t data_offset,
7202 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007203 bool needs_null_check) {
7204 DCHECK(kEmitCompilerReadBarrier);
7205 DCHECK(kUseBakerReadBarrier);
7206
Roland Levillain3d312422016-06-23 13:53:42 +01007207 static_assert(
7208 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7209 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007210 // /* HeapReference<Object> */ ref =
7211 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007212 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007213 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007214}
7215
7216void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7217 Location ref,
7218 Register obj,
7219 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007220 bool needs_null_check,
7221 bool always_update_field,
7222 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007223 DCHECK(kEmitCompilerReadBarrier);
7224 DCHECK(kUseBakerReadBarrier);
7225
7226 // In slow path based read barriers, the read barrier call is
7227 // inserted after the original load. However, in fast path based
7228 // Baker's read barriers, we need to perform the load of
7229 // mirror::Object::monitor_ *before* the original reference load.
7230 // This load-load ordering is required by the read barrier.
7231 // The fast path/slow path (for Baker's algorithm) should look like:
7232 //
7233 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7234 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7235 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007236 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007237 // if (is_gray) {
7238 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7239 // }
7240 //
7241 // Note: the original implementation in ReadBarrier::Barrier is
7242 // slightly more complex as:
7243 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007244 // the high-bits of rb_state, which are expected to be all zeroes
7245 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7246 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007247 // - it performs additional checks that we do not do here for
7248 // performance reasons.
7249
7250 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007251 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7252
Vladimir Marko953437b2016-08-24 08:30:46 +00007253 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007254 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7255 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007256 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7257 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7258 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7259
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007260 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007261 // ref = ReadBarrier::Mark(ref);
7262 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7263 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007264 if (needs_null_check) {
7265 MaybeRecordImplicitNullCheck(instruction);
7266 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007267
7268 // Load fence to prevent load-load reordering.
7269 // Note that this is a no-op, thanks to the x86 memory model.
7270 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7271
7272 // The actual reference load.
7273 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007274 __ movl(ref_reg, src); // Flags are unaffected.
7275
7276 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7277 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007278 SlowPathCode* slow_path;
7279 if (always_update_field) {
7280 DCHECK(temp != nullptr);
7281 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7282 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7283 } else {
7284 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7285 instruction, ref, /* unpoison_ref_before_marking */ true);
7286 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007287 AddSlowPath(slow_path);
7288
7289 // We have done the "if" of the gray bit check above, now branch based on the flags.
7290 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007291
7292 // Object* ref = ref_addr->AsMirrorPtr()
7293 __ MaybeUnpoisonHeapReference(ref_reg);
7294
Roland Levillain7c1559a2015-12-15 10:55:36 +00007295 __ Bind(slow_path->GetExitLabel());
7296}
7297
7298void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7299 Location out,
7300 Location ref,
7301 Location obj,
7302 uint32_t offset,
7303 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007304 DCHECK(kEmitCompilerReadBarrier);
7305
Roland Levillain7c1559a2015-12-15 10:55:36 +00007306 // Insert a slow path based read barrier *after* the reference load.
7307 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007308 // If heap poisoning is enabled, the unpoisoning of the loaded
7309 // reference will be carried out by the runtime within the slow
7310 // path.
7311 //
7312 // Note that `ref` currently does not get unpoisoned (when heap
7313 // poisoning is enabled), which is alright as the `ref` argument is
7314 // not used by the artReadBarrierSlow entry point.
7315 //
7316 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7317 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7318 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7319 AddSlowPath(slow_path);
7320
Roland Levillain0d5a2812015-11-13 10:07:31 +00007321 __ jmp(slow_path->GetEntryLabel());
7322 __ Bind(slow_path->GetExitLabel());
7323}
7324
Roland Levillain7c1559a2015-12-15 10:55:36 +00007325void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7326 Location out,
7327 Location ref,
7328 Location obj,
7329 uint32_t offset,
7330 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007331 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007332 // Baker's read barriers shall be handled by the fast path
7333 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7334 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007335 // If heap poisoning is enabled, unpoisoning will be taken care of
7336 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007337 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007338 } else if (kPoisonHeapReferences) {
7339 __ UnpoisonHeapReference(out.AsRegister<Register>());
7340 }
7341}
7342
Roland Levillain7c1559a2015-12-15 10:55:36 +00007343void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7344 Location out,
7345 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007346 DCHECK(kEmitCompilerReadBarrier);
7347
Roland Levillain7c1559a2015-12-15 10:55:36 +00007348 // Insert a slow path based read barrier *after* the GC root load.
7349 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007350 // Note that GC roots are not affected by heap poisoning, so we do
7351 // not need to do anything special for this here.
7352 SlowPathCode* slow_path =
7353 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7354 AddSlowPath(slow_path);
7355
Roland Levillain0d5a2812015-11-13 10:07:31 +00007356 __ jmp(slow_path->GetEntryLabel());
7357 __ Bind(slow_path->GetExitLabel());
7358}
7359
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007360void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007361 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007362 LOG(FATAL) << "Unreachable";
7363}
7364
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007365void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007366 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007367 LOG(FATAL) << "Unreachable";
7368}
7369
Mark Mendellfe57faa2015-09-18 09:26:15 -04007370// Simple implementation of packed switch - generate cascaded compare/jumps.
7371void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7372 LocationSummary* locations =
7373 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7374 locations->SetInAt(0, Location::RequiresRegister());
7375}
7376
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007377void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7378 int32_t lower_bound,
7379 uint32_t num_entries,
7380 HBasicBlock* switch_block,
7381 HBasicBlock* default_block) {
7382 // Figure out the correct compare values and jump conditions.
7383 // Handle the first compare/branch as a special case because it might
7384 // jump to the default case.
7385 DCHECK_GT(num_entries, 2u);
7386 Condition first_condition;
7387 uint32_t index;
7388 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7389 if (lower_bound != 0) {
7390 first_condition = kLess;
7391 __ cmpl(value_reg, Immediate(lower_bound));
7392 __ j(first_condition, codegen_->GetLabelOf(default_block));
7393 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007394
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007395 index = 1;
7396 } else {
7397 // Handle all the compare/jumps below.
7398 first_condition = kBelow;
7399 index = 0;
7400 }
7401
7402 // Handle the rest of the compare/jumps.
7403 for (; index + 1 < num_entries; index += 2) {
7404 int32_t compare_to_value = lower_bound + index + 1;
7405 __ cmpl(value_reg, Immediate(compare_to_value));
7406 // Jump to successors[index] if value < case_value[index].
7407 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7408 // Jump to successors[index + 1] if value == case_value[index + 1].
7409 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7410 }
7411
7412 if (index != num_entries) {
7413 // There are an odd number of entries. Handle the last one.
7414 DCHECK_EQ(index + 1, num_entries);
7415 __ cmpl(value_reg, Immediate(lower_bound + index));
7416 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007417 }
7418
7419 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007420 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7421 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007422 }
7423}
7424
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007425void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7426 int32_t lower_bound = switch_instr->GetStartValue();
7427 uint32_t num_entries = switch_instr->GetNumEntries();
7428 LocationSummary* locations = switch_instr->GetLocations();
7429 Register value_reg = locations->InAt(0).AsRegister<Register>();
7430
7431 GenPackedSwitchWithCompares(value_reg,
7432 lower_bound,
7433 num_entries,
7434 switch_instr->GetBlock(),
7435 switch_instr->GetDefaultBlock());
7436}
7437
Mark Mendell805b3b52015-09-18 14:10:29 -04007438void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7439 LocationSummary* locations =
7440 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7441 locations->SetInAt(0, Location::RequiresRegister());
7442
7443 // Constant area pointer.
7444 locations->SetInAt(1, Location::RequiresRegister());
7445
7446 // And the temporary we need.
7447 locations->AddTemp(Location::RequiresRegister());
7448}
7449
7450void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7451 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007452 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007453 LocationSummary* locations = switch_instr->GetLocations();
7454 Register value_reg = locations->InAt(0).AsRegister<Register>();
7455 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7456
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007457 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7458 GenPackedSwitchWithCompares(value_reg,
7459 lower_bound,
7460 num_entries,
7461 switch_instr->GetBlock(),
7462 default_block);
7463 return;
7464 }
7465
Mark Mendell805b3b52015-09-18 14:10:29 -04007466 // Optimizing has a jump area.
7467 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7468 Register constant_area = locations->InAt(1).AsRegister<Register>();
7469
7470 // Remove the bias, if needed.
7471 if (lower_bound != 0) {
7472 __ leal(temp_reg, Address(value_reg, -lower_bound));
7473 value_reg = temp_reg;
7474 }
7475
7476 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007477 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007478 __ cmpl(value_reg, Immediate(num_entries - 1));
7479 __ j(kAbove, codegen_->GetLabelOf(default_block));
7480
7481 // We are in the range of the table.
7482 // Load (target-constant_area) from the jump table, indexing by the value.
7483 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7484
7485 // Compute the actual target address by adding in constant_area.
7486 __ addl(temp_reg, constant_area);
7487
7488 // And jump.
7489 __ jmp(temp_reg);
7490}
7491
Mark Mendell0616ae02015-04-17 12:49:27 -04007492void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7493 HX86ComputeBaseMethodAddress* insn) {
7494 LocationSummary* locations =
7495 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7496 locations->SetOut(Location::RequiresRegister());
7497}
7498
7499void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7500 HX86ComputeBaseMethodAddress* insn) {
7501 LocationSummary* locations = insn->GetLocations();
7502 Register reg = locations->Out().AsRegister<Register>();
7503
7504 // Generate call to next instruction.
7505 Label next_instruction;
7506 __ call(&next_instruction);
7507 __ Bind(&next_instruction);
7508
7509 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007510 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007511
7512 // Grab the return address off the stack.
7513 __ popl(reg);
7514}
7515
7516void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7517 HX86LoadFromConstantTable* insn) {
7518 LocationSummary* locations =
7519 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7520
7521 locations->SetInAt(0, Location::RequiresRegister());
7522 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7523
7524 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007525 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007526 return;
7527 }
7528
7529 switch (insn->GetType()) {
7530 case Primitive::kPrimFloat:
7531 case Primitive::kPrimDouble:
7532 locations->SetOut(Location::RequiresFpuRegister());
7533 break;
7534
7535 case Primitive::kPrimInt:
7536 locations->SetOut(Location::RequiresRegister());
7537 break;
7538
7539 default:
7540 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7541 }
7542}
7543
7544void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007545 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007546 return;
7547 }
7548
7549 LocationSummary* locations = insn->GetLocations();
7550 Location out = locations->Out();
7551 Register const_area = locations->InAt(0).AsRegister<Register>();
7552 HConstant *value = insn->GetConstant();
7553
7554 switch (insn->GetType()) {
7555 case Primitive::kPrimFloat:
7556 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007557 codegen_->LiteralFloatAddress(
7558 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007559 break;
7560
7561 case Primitive::kPrimDouble:
7562 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007563 codegen_->LiteralDoubleAddress(
7564 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007565 break;
7566
7567 case Primitive::kPrimInt:
7568 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007569 codegen_->LiteralInt32Address(
7570 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007571 break;
7572
7573 default:
7574 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7575 }
7576}
7577
Mark Mendell0616ae02015-04-17 12:49:27 -04007578/**
7579 * Class to handle late fixup of offsets into constant area.
7580 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007581class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007582 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007583 RIPFixup(CodeGeneratorX86& codegen,
7584 HX86ComputeBaseMethodAddress* base_method_address,
7585 size_t offset)
7586 : codegen_(&codegen),
7587 base_method_address_(base_method_address),
7588 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007589
7590 protected:
7591 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7592
7593 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007594 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007595
7596 private:
7597 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7598 // Patch the correct offset for the instruction. The place to patch is the
7599 // last 4 bytes of the instruction.
7600 // The value to patch is the distance from the offset in the constant area
7601 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007602 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007603 int32_t relative_position =
7604 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007605
7606 // Patch in the right value.
7607 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7608 }
7609
Mark Mendell0616ae02015-04-17 12:49:27 -04007610 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007611 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007612};
7613
Mark Mendell805b3b52015-09-18 14:10:29 -04007614/**
7615 * Class to handle late fixup of offsets to a jump table that will be created in the
7616 * constant area.
7617 */
7618class JumpTableRIPFixup : public RIPFixup {
7619 public:
7620 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007621 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7622 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007623
7624 void CreateJumpTable() {
7625 X86Assembler* assembler = codegen_->GetAssembler();
7626
7627 // Ensure that the reference to the jump table has the correct offset.
7628 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7629 SetOffset(offset_in_constant_table);
7630
7631 // The label values in the jump table are computed relative to the
7632 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007633 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007634
7635 // Populate the jump table with the correct values for the jump table.
7636 int32_t num_entries = switch_instr_->GetNumEntries();
7637 HBasicBlock* block = switch_instr_->GetBlock();
7638 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7639 // The value that we want is the target offset - the position of the table.
7640 for (int32_t i = 0; i < num_entries; i++) {
7641 HBasicBlock* b = successors[i];
7642 Label* l = codegen_->GetLabelOf(b);
7643 DCHECK(l->IsBound());
7644 int32_t offset_to_block = l->Position() - relative_offset;
7645 assembler->AppendInt32(offset_to_block);
7646 }
7647 }
7648
7649 private:
7650 const HX86PackedSwitch* switch_instr_;
7651};
7652
7653void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7654 // Generate the constant area if needed.
7655 X86Assembler* assembler = GetAssembler();
7656 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7657 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7658 // byte values.
7659 assembler->Align(4, 0);
7660 constant_area_start_ = assembler->CodeSize();
7661
7662 // Populate any jump tables.
7663 for (auto jump_table : fixups_to_jump_tables_) {
7664 jump_table->CreateJumpTable();
7665 }
7666
7667 // And now add the constant area to the generated code.
7668 assembler->AddConstantArea();
7669 }
7670
7671 // And finish up.
7672 CodeGenerator::Finalize(allocator);
7673}
7674
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007675Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7676 HX86ComputeBaseMethodAddress* method_base,
7677 Register reg) {
7678 AssemblerFixup* fixup =
7679 new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007680 return Address(reg, kDummy32BitOffset, fixup);
7681}
7682
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007683Address CodeGeneratorX86::LiteralFloatAddress(float v,
7684 HX86ComputeBaseMethodAddress* method_base,
7685 Register reg) {
7686 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007687 return Address(reg, kDummy32BitOffset, fixup);
7688}
7689
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007690Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7691 HX86ComputeBaseMethodAddress* method_base,
7692 Register reg) {
7693 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007694 return Address(reg, kDummy32BitOffset, fixup);
7695}
7696
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007697Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7698 HX86ComputeBaseMethodAddress* method_base,
7699 Register reg) {
7700 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007701 return Address(reg, kDummy32BitOffset, fixup);
7702}
7703
Aart Bika19616e2016-02-01 18:57:58 -08007704void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7705 if (value == 0) {
7706 __ xorl(dest, dest);
7707 } else {
7708 __ movl(dest, Immediate(value));
7709 }
7710}
7711
7712void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7713 if (value == 0) {
7714 __ testl(dest, dest);
7715 } else {
7716 __ cmpl(dest, Immediate(value));
7717 }
7718}
7719
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007720void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7721 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007722 GenerateIntCompare(lhs_reg, rhs);
7723}
7724
7725void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007726 if (rhs.IsConstant()) {
7727 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007728 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007729 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007730 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007731 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007732 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007733 }
7734}
7735
7736Address CodeGeneratorX86::ArrayAddress(Register obj,
7737 Location index,
7738 ScaleFactor scale,
7739 uint32_t data_offset) {
7740 return index.IsConstant() ?
7741 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7742 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7743}
7744
Mark Mendell805b3b52015-09-18 14:10:29 -04007745Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7746 Register reg,
7747 Register value) {
7748 // Create a fixup to be used to create and address the jump table.
7749 JumpTableRIPFixup* table_fixup =
7750 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7751
7752 // We have to populate the jump tables.
7753 fixups_to_jump_tables_.push_back(table_fixup);
7754
7755 // We want a scaled address, as we are extracting the correct offset from the table.
7756 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7757}
7758
Andreas Gampe85b62f22015-09-09 13:15:38 -07007759// TODO: target as memory.
7760void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7761 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007762 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007763 return;
7764 }
7765
7766 DCHECK_NE(type, Primitive::kPrimVoid);
7767
7768 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7769 if (target.Equals(return_loc)) {
7770 return;
7771 }
7772
7773 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7774 // with the else branch.
7775 if (type == Primitive::kPrimLong) {
7776 HParallelMove parallel_move(GetGraph()->GetArena());
7777 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7778 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7779 GetMoveResolver()->EmitNativeCode(&parallel_move);
7780 } else {
7781 // Let the parallel move resolver take care of all of this.
7782 HParallelMove parallel_move(GetGraph()->GetArena());
7783 parallel_move.AddMove(return_loc, target, type, nullptr);
7784 GetMoveResolver()->EmitNativeCode(&parallel_move);
7785 }
7786}
7787
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007788void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7789 const uint8_t* roots_data,
7790 const PatchInfo<Label>& info,
7791 uint64_t index_in_table) const {
7792 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7793 uintptr_t address =
7794 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7795 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7796 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7797 dchecked_integral_cast<uint32_t>(address);
7798}
7799
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007800void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7801 for (const PatchInfo<Label>& info : jit_string_patches_) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007802 const auto& it = jit_string_roots_.find(
7803 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007804 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007805 PatchJitRootUse(code, roots_data, info, it->second);
7806 }
7807
7808 for (const PatchInfo<Label>& info : jit_class_patches_) {
7809 const auto& it = jit_class_roots_.find(
7810 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7811 DCHECK(it != jit_class_roots_.end());
7812 PatchJitRootUse(code, roots_data, info, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007813 }
7814}
7815
Roland Levillain4d027112015-07-01 15:41:14 +01007816#undef __
7817
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007818} // namespace x86
7819} // namespace art