blob: f13b60aebf501145580d06117f25b410a4d0981c [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;
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800228 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex().index_;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000229 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
230 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)
David Srbecky9cd6d372016-02-09 15:24:47 +0000257 : SlowPathCode(at), cls_(cls), at_(at), 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 {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000262 LocationSummary* locations = at_->GetLocations();
263 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;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800268 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex().index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100269 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
270 : kQuickInitializeType,
Alexandre Rames8158f282015-08-07 10:26:17 +0100271 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000272 if (do_clinit_) {
273 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
274 } else {
275 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
276 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277
278 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000279 Location out = locations->Out();
280 if (out.IsValid()) {
281 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
282 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000285 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000286 __ jmp(GetExitLabel());
287 }
288
Alexandre Rames9931f312015-06-19 14:47:01 +0100289 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
290
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000291 private:
292 // The class this slow path will load.
293 HLoadClass* const cls_;
294
295 // The instruction where this slow path is happening.
296 // (Might be the load class or an initialization check).
297 HInstruction* const at_;
298
299 // The dex PC of `at_`.
300 const uint32_t dex_pc_;
301
302 // Whether to initialize the class.
303 const bool do_clinit_;
304
305 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
306};
307
Andreas Gampe85b62f22015-09-09 13:15:38 -0700308class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000309 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000310 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000311 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000313 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000315 DCHECK(instruction_->IsCheckCast()
316 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317
318 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
319 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000320
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000321 if (!is_fatal_) {
322 SaveLiveRegisters(codegen, locations);
323 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
325 // We're moving two locations to locations that could overlap, so we need a parallel
326 // move resolver.
327 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800328 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800329 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
330 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800331 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800332 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
333 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000334 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100335 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100336 instruction_,
337 instruction_->GetDexPc(),
338 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800339 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000340 } else {
341 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800342 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
343 instruction_,
344 instruction_->GetDexPc(),
345 this);
346 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000347 }
348
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000349 if (!is_fatal_) {
350 if (instruction_->IsInstanceOf()) {
351 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
352 }
353 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000354
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000355 __ jmp(GetExitLabel());
356 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000357 }
358
Alexandre Rames9931f312015-06-19 14:47:01 +0100359 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000360 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100361
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000362 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000363 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000364
365 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
366};
367
Andreas Gampe85b62f22015-09-09 13:15:38 -0700368class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700369 public:
Aart Bik42249c32016-01-07 15:33:50 -0800370 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000371 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700372
373 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100374 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700375 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100376 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000377 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378 }
379
Alexandre Rames9931f312015-06-19 14:47:01 +0100380 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
381
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700382 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700383 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
384};
385
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100386class ArraySetSlowPathX86 : public SlowPathCode {
387 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000388 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
391 LocationSummary* locations = instruction_->GetLocations();
392 __ Bind(GetEntryLabel());
393 SaveLiveRegisters(codegen, locations);
394
395 InvokeRuntimeCallingConvention calling_convention;
396 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
397 parallel_move.AddMove(
398 locations->InAt(0),
399 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
400 Primitive::kPrimNot,
401 nullptr);
402 parallel_move.AddMove(
403 locations->InAt(1),
404 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
405 Primitive::kPrimInt,
406 nullptr);
407 parallel_move.AddMove(
408 locations->InAt(2),
409 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
410 Primitive::kPrimNot,
411 nullptr);
412 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
413
414 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100415 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000416 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100417 RestoreLiveRegisters(codegen, locations);
418 __ jmp(GetExitLabel());
419 }
420
421 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
422
423 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100424 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
425};
426
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100427// Slow path marking an object reference `ref` during a read
428// barrier. The field `obj.field` in the object `obj` holding this
429// reference does not get updated by this slow path after marking (see
430// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
431//
432// This means that after the execution of this slow path, `ref` will
433// always be up-to-date, but `obj.field` may not; i.e., after the
434// flip, `ref` will be a to-space reference, but `obj.field` will
435// probably still be a from-space reference (unless it gets updated by
436// another thread, or if another thread installed another object
437// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000438class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
439 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100440 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
441 Location ref,
442 bool unpoison_ref_before_marking)
443 : SlowPathCode(instruction),
444 ref_(ref),
445 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000446 DCHECK(kEmitCompilerReadBarrier);
447 }
448
449 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
450
451 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
452 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100453 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000454 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100455 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000456 DCHECK(instruction_->IsInstanceFieldGet() ||
457 instruction_->IsStaticFieldGet() ||
458 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100459 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000460 instruction_->IsLoadClass() ||
461 instruction_->IsLoadString() ||
462 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100463 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100464 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
465 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000466 << "Unexpected instruction in read barrier marking slow path: "
467 << instruction_->DebugName();
468
469 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100470 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000471 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100472 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000473 }
Roland Levillain4359e612016-07-20 11:32:19 +0100474 // No need to save live registers; it's taken care of by the
475 // entrypoint. Also, there is no need to update the stack mask,
476 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000477 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100478 DCHECK_NE(ref_reg, ESP);
479 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100480 // "Compact" slow path, saving two moves.
481 //
482 // Instead of using the standard runtime calling convention (input
483 // and output in EAX):
484 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100485 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100486 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100487 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100488 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100489 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100490 // of a dedicated entrypoint:
491 //
492 // rX <- ReadBarrierMarkRegX(rX)
493 //
494 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100495 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100496 // This runtime call does not require a stack map.
497 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000498 __ jmp(GetExitLabel());
499 }
500
501 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100502 // The location (register) of the marked object reference.
503 const Location ref_;
504 // Should the reference in `ref_` be unpoisoned prior to marking it?
505 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000506
507 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
508};
509
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100510// Slow path marking an object reference `ref` during a read barrier,
511// and if needed, atomically updating the field `obj.field` in the
512// object `obj` holding this reference after marking (contrary to
513// ReadBarrierMarkSlowPathX86 above, which never tries to update
514// `obj.field`).
515//
516// This means that after the execution of this slow path, both `ref`
517// and `obj.field` will be up-to-date; i.e., after the flip, both will
518// hold the same to-space reference (unless another thread installed
519// another object reference (different from `ref`) in `obj.field`).
520class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
521 public:
522 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
523 Location ref,
524 Register obj,
525 const Address& field_addr,
526 bool unpoison_ref_before_marking,
527 Register temp)
528 : SlowPathCode(instruction),
529 ref_(ref),
530 obj_(obj),
531 field_addr_(field_addr),
532 unpoison_ref_before_marking_(unpoison_ref_before_marking),
533 temp_(temp) {
534 DCHECK(kEmitCompilerReadBarrier);
535 }
536
537 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
538
539 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
540 LocationSummary* locations = instruction_->GetLocations();
541 Register ref_reg = ref_.AsRegister<Register>();
542 DCHECK(locations->CanCall());
543 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
544 // This slow path is only used by the UnsafeCASObject intrinsic.
545 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
546 << "Unexpected instruction in read barrier marking and field updating slow path: "
547 << instruction_->DebugName();
548 DCHECK(instruction_->GetLocations()->Intrinsified());
549 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
550
551 __ Bind(GetEntryLabel());
552 if (unpoison_ref_before_marking_) {
553 // Object* ref = ref_addr->AsMirrorPtr()
554 __ MaybeUnpoisonHeapReference(ref_reg);
555 }
556
557 // Save the old (unpoisoned) reference.
558 __ movl(temp_, ref_reg);
559
560 // No need to save live registers; it's taken care of by the
561 // entrypoint. Also, there is no need to update the stack mask,
562 // as this runtime call will not trigger a garbage collection.
563 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
564 DCHECK_NE(ref_reg, ESP);
565 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
566 // "Compact" slow path, saving two moves.
567 //
568 // Instead of using the standard runtime calling convention (input
569 // and output in EAX):
570 //
571 // EAX <- ref
572 // EAX <- ReadBarrierMark(EAX)
573 // ref <- EAX
574 //
575 // we just use rX (the register containing `ref`) as input and output
576 // of a dedicated entrypoint:
577 //
578 // rX <- ReadBarrierMarkRegX(rX)
579 //
580 int32_t entry_point_offset =
581 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
582 // This runtime call does not require a stack map.
583 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
584
585 // If the new reference is different from the old reference,
586 // update the field in the holder (`*field_addr`).
587 //
588 // Note that this field could also hold a different object, if
589 // another thread had concurrently changed it. In that case, the
590 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
591 // operation below would abort the CAS, leaving the field as-is.
592 NearLabel done;
593 __ cmpl(temp_, ref_reg);
594 __ j(kEqual, &done);
595
596 // Update the the holder's field atomically. This may fail if
597 // mutator updates before us, but it's OK. This is achieved
598 // using a strong compare-and-set (CAS) operation with relaxed
599 // memory synchronization ordering, where the expected value is
600 // the old reference and the desired value is the new reference.
601 // This operation is implemented with a 32-bit LOCK CMPXLCHG
602 // instruction, which requires the expected value (the old
603 // reference) to be in EAX. Save EAX beforehand, and move the
604 // expected value (stored in `temp_`) into EAX.
605 __ pushl(EAX);
606 __ movl(EAX, temp_);
607
608 // Convenience aliases.
609 Register base = obj_;
610 Register expected = EAX;
611 Register value = ref_reg;
612
613 bool base_equals_value = (base == value);
614 if (kPoisonHeapReferences) {
615 if (base_equals_value) {
616 // If `base` and `value` are the same register location, move
617 // `value` to a temporary register. This way, poisoning
618 // `value` won't invalidate `base`.
619 value = temp_;
620 __ movl(value, base);
621 }
622
623 // Check that the register allocator did not assign the location
624 // of `expected` (EAX) to `value` nor to `base`, so that heap
625 // poisoning (when enabled) works as intended below.
626 // - If `value` were equal to `expected`, both references would
627 // be poisoned twice, meaning they would not be poisoned at
628 // all, as heap poisoning uses address negation.
629 // - If `base` were equal to `expected`, poisoning `expected`
630 // would invalidate `base`.
631 DCHECK_NE(value, expected);
632 DCHECK_NE(base, expected);
633
634 __ PoisonHeapReference(expected);
635 __ PoisonHeapReference(value);
636 }
637
638 __ LockCmpxchgl(field_addr_, value);
639
640 // If heap poisoning is enabled, we need to unpoison the values
641 // that were poisoned earlier.
642 if (kPoisonHeapReferences) {
643 if (base_equals_value) {
644 // `value` has been moved to a temporary register, no need
645 // to unpoison it.
646 } else {
647 __ UnpoisonHeapReference(value);
648 }
649 // No need to unpoison `expected` (EAX), as it is be overwritten below.
650 }
651
652 // Restore EAX.
653 __ popl(EAX);
654
655 __ Bind(&done);
656 __ jmp(GetExitLabel());
657 }
658
659 private:
660 // The location (register) of the marked object reference.
661 const Location ref_;
662 // The register containing the object holding the marked object reference field.
663 const Register obj_;
664 // The address of the marked reference field. The base of this address must be `obj_`.
665 const Address field_addr_;
666
667 // Should the reference in `ref_` be unpoisoned prior to marking it?
668 const bool unpoison_ref_before_marking_;
669
670 const Register temp_;
671
672 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
673};
674
Roland Levillain0d5a2812015-11-13 10:07:31 +0000675// Slow path generating a read barrier for a heap reference.
676class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
677 public:
678 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
679 Location out,
680 Location ref,
681 Location obj,
682 uint32_t offset,
683 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000684 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000685 out_(out),
686 ref_(ref),
687 obj_(obj),
688 offset_(offset),
689 index_(index) {
690 DCHECK(kEmitCompilerReadBarrier);
691 // If `obj` is equal to `out` or `ref`, it means the initial object
692 // has been overwritten by (or after) the heap object reference load
693 // to be instrumented, e.g.:
694 //
695 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000696 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000697 //
698 // In that case, we have lost the information about the original
699 // object, and the emitted read barrier cannot work properly.
700 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
701 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
702 }
703
704 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
705 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
706 LocationSummary* locations = instruction_->GetLocations();
707 Register reg_out = out_.AsRegister<Register>();
708 DCHECK(locations->CanCall());
709 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100710 DCHECK(instruction_->IsInstanceFieldGet() ||
711 instruction_->IsStaticFieldGet() ||
712 instruction_->IsArrayGet() ||
713 instruction_->IsInstanceOf() ||
714 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100715 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000716 << "Unexpected instruction in read barrier for heap reference slow path: "
717 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000718
719 __ Bind(GetEntryLabel());
720 SaveLiveRegisters(codegen, locations);
721
722 // We may have to change the index's value, but as `index_` is a
723 // constant member (like other "inputs" of this slow path),
724 // introduce a copy of it, `index`.
725 Location index = index_;
726 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100727 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000728 if (instruction_->IsArrayGet()) {
729 // Compute the actual memory offset and store it in `index`.
730 Register index_reg = index_.AsRegister<Register>();
731 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
732 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
733 // We are about to change the value of `index_reg` (see the
734 // calls to art::x86::X86Assembler::shll and
735 // art::x86::X86Assembler::AddImmediate below), but it has
736 // not been saved by the previous call to
737 // art::SlowPathCode::SaveLiveRegisters, as it is a
738 // callee-save register --
739 // art::SlowPathCode::SaveLiveRegisters does not consider
740 // callee-save registers, as it has been designed with the
741 // assumption that callee-save registers are supposed to be
742 // handled by the called function. So, as a callee-save
743 // register, `index_reg` _would_ eventually be saved onto
744 // the stack, but it would be too late: we would have
745 // changed its value earlier. Therefore, we manually save
746 // it here into another freely available register,
747 // `free_reg`, chosen of course among the caller-save
748 // registers (as a callee-save `free_reg` register would
749 // exhibit the same problem).
750 //
751 // Note we could have requested a temporary register from
752 // the register allocator instead; but we prefer not to, as
753 // this is a slow path, and we know we can find a
754 // caller-save register that is available.
755 Register free_reg = FindAvailableCallerSaveRegister(codegen);
756 __ movl(free_reg, index_reg);
757 index_reg = free_reg;
758 index = Location::RegisterLocation(index_reg);
759 } else {
760 // The initial register stored in `index_` has already been
761 // saved in the call to art::SlowPathCode::SaveLiveRegisters
762 // (as it is not a callee-save register), so we can freely
763 // use it.
764 }
765 // Shifting the index value contained in `index_reg` by the scale
766 // factor (2) cannot overflow in practice, as the runtime is
767 // unable to allocate object arrays with a size larger than
768 // 2^26 - 1 (that is, 2^28 - 4 bytes).
769 __ shll(index_reg, Immediate(TIMES_4));
770 static_assert(
771 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
772 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
773 __ AddImmediate(index_reg, Immediate(offset_));
774 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100775 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
776 // intrinsics, `index_` is not shifted by a scale factor of 2
777 // (as in the case of ArrayGet), as it is actually an offset
778 // to an object field within an object.
779 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000780 DCHECK(instruction_->GetLocations()->Intrinsified());
781 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
782 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
783 << instruction_->AsInvoke()->GetIntrinsic();
784 DCHECK_EQ(offset_, 0U);
785 DCHECK(index_.IsRegisterPair());
786 // UnsafeGet's offset location is a register pair, the low
787 // part contains the correct offset.
788 index = index_.ToLow();
789 }
790 }
791
792 // We're moving two or three locations to locations that could
793 // overlap, so we need a parallel move resolver.
794 InvokeRuntimeCallingConvention calling_convention;
795 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
796 parallel_move.AddMove(ref_,
797 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
798 Primitive::kPrimNot,
799 nullptr);
800 parallel_move.AddMove(obj_,
801 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
802 Primitive::kPrimNot,
803 nullptr);
804 if (index.IsValid()) {
805 parallel_move.AddMove(index,
806 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
807 Primitive::kPrimInt,
808 nullptr);
809 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
810 } else {
811 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
812 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
813 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100814 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000815 CheckEntrypointTypes<
816 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
817 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
818
819 RestoreLiveRegisters(codegen, locations);
820 __ jmp(GetExitLabel());
821 }
822
823 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
824
825 private:
826 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
827 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
828 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
829 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
830 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
831 return static_cast<Register>(i);
832 }
833 }
834 // We shall never fail to find a free caller-save register, as
835 // there are more than two core caller-save registers on x86
836 // (meaning it is possible to find one which is different from
837 // `ref` and `obj`).
838 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
839 LOG(FATAL) << "Could not find a free caller-save register";
840 UNREACHABLE();
841 }
842
Roland Levillain0d5a2812015-11-13 10:07:31 +0000843 const Location out_;
844 const Location ref_;
845 const Location obj_;
846 const uint32_t offset_;
847 // An additional location containing an index to an array.
848 // Only used for HArrayGet and the UnsafeGetObject &
849 // UnsafeGetObjectVolatile intrinsics.
850 const Location index_;
851
852 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
853};
854
855// Slow path generating a read barrier for a GC root.
856class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
857 public:
858 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000859 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000860 DCHECK(kEmitCompilerReadBarrier);
861 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000862
863 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
864 LocationSummary* locations = instruction_->GetLocations();
865 Register reg_out = out_.AsRegister<Register>();
866 DCHECK(locations->CanCall());
867 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000868 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
869 << "Unexpected instruction in read barrier for GC root slow path: "
870 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000871
872 __ Bind(GetEntryLabel());
873 SaveLiveRegisters(codegen, locations);
874
875 InvokeRuntimeCallingConvention calling_convention;
876 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
877 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100878 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000879 instruction_,
880 instruction_->GetDexPc(),
881 this);
882 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
883 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
884
885 RestoreLiveRegisters(codegen, locations);
886 __ jmp(GetExitLabel());
887 }
888
889 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
890
891 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000892 const Location out_;
893 const Location root_;
894
895 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
896};
897
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100898#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100899// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
900#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100901
Aart Bike9f37602015-10-09 11:15:55 -0700902inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700903 switch (cond) {
904 case kCondEQ: return kEqual;
905 case kCondNE: return kNotEqual;
906 case kCondLT: return kLess;
907 case kCondLE: return kLessEqual;
908 case kCondGT: return kGreater;
909 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700910 case kCondB: return kBelow;
911 case kCondBE: return kBelowEqual;
912 case kCondA: return kAbove;
913 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700914 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100915 LOG(FATAL) << "Unreachable";
916 UNREACHABLE();
917}
918
Aart Bike9f37602015-10-09 11:15:55 -0700919// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100920inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
921 switch (cond) {
922 case kCondEQ: return kEqual;
923 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700924 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100925 case kCondLT: return kBelow;
926 case kCondLE: return kBelowEqual;
927 case kCondGT: return kAbove;
928 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700929 // Unsigned remain unchanged.
930 case kCondB: return kBelow;
931 case kCondBE: return kBelowEqual;
932 case kCondA: return kAbove;
933 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100934 }
935 LOG(FATAL) << "Unreachable";
936 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700937}
938
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100939void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100940 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100941}
942
943void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100944 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100945}
946
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100947size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
948 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
949 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100950}
951
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100952size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
953 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
954 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100955}
956
Mark Mendell7c8d0092015-01-26 11:21:33 -0500957size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
958 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
959 return GetFloatingPointSpillSlotSize();
960}
961
962size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
963 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
964 return GetFloatingPointSpillSlotSize();
965}
966
Calin Juravle175dc732015-08-25 15:42:32 +0100967void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
968 HInstruction* instruction,
969 uint32_t dex_pc,
970 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100971 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100972 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
973 if (EntrypointRequiresStackMap(entrypoint)) {
974 RecordPcInfo(instruction, dex_pc, slow_path);
975 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100976}
977
Roland Levillaindec8f632016-07-22 17:10:06 +0100978void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
979 HInstruction* instruction,
980 SlowPathCode* slow_path) {
981 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100982 GenerateInvokeRuntime(entry_point_offset);
983}
984
985void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +0100986 __ fs()->call(Address::Absolute(entry_point_offset));
987}
988
Mark Mendellfb8d2792015-03-31 22:16:59 -0400989CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000990 const X86InstructionSetFeatures& isa_features,
991 const CompilerOptions& compiler_options,
992 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500993 : CodeGenerator(graph,
994 kNumberOfCpuRegisters,
995 kNumberOfXmmRegisters,
996 kNumberOfRegisterPairs,
997 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
998 arraysize(kCoreCalleeSaves))
999 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001000 0,
1001 compiler_options,
1002 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001003 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001004 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001005 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001006 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001007 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001008 isa_features_(isa_features),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001009 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001010 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1011 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001012 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001013 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001014 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001015 constant_area_start_(-1),
1016 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1017 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001018 // Use a fake return address register to mimic Quick.
1019 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001020}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001021
David Brazdil58282f42016-01-14 12:45:10 +00001022void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001023 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001024 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001025}
1026
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001027InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001028 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001029 assembler_(codegen->GetAssembler()),
1030 codegen_(codegen) {}
1031
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001032static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001033 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001034}
1035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001036void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001037 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001038 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001039 bool skip_overflow_check =
1040 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001041 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001042
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001043 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001044 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001045 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001046 }
1047
Mark Mendell5f874182015-03-04 15:42:45 -05001048 if (HasEmptyFrame()) {
1049 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001050 }
Mark Mendell5f874182015-03-04 15:42:45 -05001051
1052 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1053 Register reg = kCoreCalleeSaves[i];
1054 if (allocated_registers_.ContainsCoreRegister(reg)) {
1055 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001056 __ cfi().AdjustCFAOffset(kX86WordSize);
1057 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001058 }
1059 }
1060
Mingyao Yang063fc772016-08-02 11:02:54 -07001061 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1062 // Initialize should_deoptimize flag to 0.
1063 __ movl(Address(ESP, -kShouldDeoptimizeFlagSize), Immediate(0));
1064 }
1065
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001066 int adjust = GetFrameSize() - FrameEntrySpillSize();
1067 __ subl(ESP, Immediate(adjust));
1068 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001069 // Save the current method if we need it. Note that we do not
1070 // do this in HCurrentMethod, as the instruction might have been removed
1071 // in the SSA graph.
1072 if (RequiresCurrentMethod()) {
1073 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1074 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001075}
1076
1077void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001078 __ cfi().RememberState();
1079 if (!HasEmptyFrame()) {
1080 int adjust = GetFrameSize() - FrameEntrySpillSize();
1081 __ addl(ESP, Immediate(adjust));
1082 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001083
David Srbeckyc34dc932015-04-12 09:27:43 +01001084 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1085 Register reg = kCoreCalleeSaves[i];
1086 if (allocated_registers_.ContainsCoreRegister(reg)) {
1087 __ popl(reg);
1088 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1089 __ cfi().Restore(DWARFReg(reg));
1090 }
Mark Mendell5f874182015-03-04 15:42:45 -05001091 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001092 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001093 __ ret();
1094 __ cfi().RestoreState();
1095 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001096}
1097
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001098void CodeGeneratorX86::Bind(HBasicBlock* block) {
1099 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001100}
1101
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001102Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1103 switch (type) {
1104 case Primitive::kPrimBoolean:
1105 case Primitive::kPrimByte:
1106 case Primitive::kPrimChar:
1107 case Primitive::kPrimShort:
1108 case Primitive::kPrimInt:
1109 case Primitive::kPrimNot:
1110 return Location::RegisterLocation(EAX);
1111
1112 case Primitive::kPrimLong:
1113 return Location::RegisterPairLocation(EAX, EDX);
1114
1115 case Primitive::kPrimVoid:
1116 return Location::NoLocation();
1117
1118 case Primitive::kPrimDouble:
1119 case Primitive::kPrimFloat:
1120 return Location::FpuRegisterLocation(XMM0);
1121 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001122
1123 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001124}
1125
1126Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1127 return Location::RegisterLocation(kMethodRegisterArgument);
1128}
1129
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001130Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001131 switch (type) {
1132 case Primitive::kPrimBoolean:
1133 case Primitive::kPrimByte:
1134 case Primitive::kPrimChar:
1135 case Primitive::kPrimShort:
1136 case Primitive::kPrimInt:
1137 case Primitive::kPrimNot: {
1138 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001139 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001140 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001141 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001142 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001143 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001144 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001145 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001146
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001147 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001148 uint32_t index = gp_index_;
1149 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001150 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001151 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001152 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1153 calling_convention.GetRegisterPairAt(index));
1154 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001155 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001156 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1157 }
1158 }
1159
1160 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001161 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001162 stack_index_++;
1163 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1164 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1165 } else {
1166 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1167 }
1168 }
1169
1170 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001171 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001172 stack_index_ += 2;
1173 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1174 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1175 } else {
1176 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001177 }
1178 }
1179
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001180 case Primitive::kPrimVoid:
1181 LOG(FATAL) << "Unexpected parameter type " << type;
1182 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001183 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001184 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001185}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001186
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001187void CodeGeneratorX86::Move32(Location destination, Location source) {
1188 if (source.Equals(destination)) {
1189 return;
1190 }
1191 if (destination.IsRegister()) {
1192 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001193 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001194 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001195 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001196 } else {
1197 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001198 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001199 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001200 } else if (destination.IsFpuRegister()) {
1201 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001202 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001203 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001204 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001205 } else {
1206 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001207 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001208 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001209 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001210 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001211 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001212 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001213 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001214 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001215 } else if (source.IsConstant()) {
1216 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001217 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001218 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001219 } else {
1220 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001221 __ pushl(Address(ESP, source.GetStackIndex()));
1222 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223 }
1224 }
1225}
1226
1227void CodeGeneratorX86::Move64(Location destination, Location source) {
1228 if (source.Equals(destination)) {
1229 return;
1230 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001231 if (destination.IsRegisterPair()) {
1232 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001233 EmitParallelMoves(
1234 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1235 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001236 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001237 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001238 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1239 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001240 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001241 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1242 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1243 __ psrlq(src_reg, Immediate(32));
1244 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001245 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001246 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001247 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001248 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1249 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001250 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1251 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001252 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001253 if (source.IsFpuRegister()) {
1254 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1255 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001256 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001257 } else if (source.IsRegisterPair()) {
1258 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1259 // Create stack space for 2 elements.
1260 __ subl(ESP, Immediate(2 * elem_size));
1261 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1262 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1263 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1264 // And remove the temporary stack space we allocated.
1265 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001266 } else {
1267 LOG(FATAL) << "Unimplemented";
1268 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001269 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001270 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001271 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001272 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001273 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001274 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001275 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001276 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001277 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001278 } else if (source.IsConstant()) {
1279 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001280 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1281 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001282 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001283 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1284 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001285 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001286 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001287 EmitParallelMoves(
1288 Location::StackSlot(source.GetStackIndex()),
1289 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001290 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001291 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001292 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1293 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001294 }
1295 }
1296}
1297
Calin Juravle175dc732015-08-25 15:42:32 +01001298void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1299 DCHECK(location.IsRegister());
1300 __ movl(location.AsRegister<Register>(), Immediate(value));
1301}
1302
Calin Juravlee460d1d2015-09-29 04:52:17 +01001303void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001304 HParallelMove move(GetGraph()->GetArena());
1305 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1306 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1307 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001308 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001309 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001310 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001311 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001312}
1313
1314void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1315 if (location.IsRegister()) {
1316 locations->AddTemp(location);
1317 } else if (location.IsRegisterPair()) {
1318 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1319 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1320 } else {
1321 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1322 }
1323}
1324
David Brazdilfc6a86a2015-06-26 10:33:45 +00001325void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001326 DCHECK(!successor->IsExitBlock());
1327
1328 HBasicBlock* block = got->GetBlock();
1329 HInstruction* previous = got->GetPrevious();
1330
1331 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001332 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001333 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1334 return;
1335 }
1336
1337 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1338 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1339 }
1340 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001341 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001342 }
1343}
1344
David Brazdilfc6a86a2015-06-26 10:33:45 +00001345void LocationsBuilderX86::VisitGoto(HGoto* got) {
1346 got->SetLocations(nullptr);
1347}
1348
1349void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1350 HandleGoto(got, got->GetSuccessor());
1351}
1352
1353void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1354 try_boundary->SetLocations(nullptr);
1355}
1356
1357void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1358 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1359 if (!successor->IsExitBlock()) {
1360 HandleGoto(try_boundary, successor);
1361 }
1362}
1363
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001364void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001365 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001366}
1367
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001368void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001369}
1370
Mark Mendell152408f2015-12-31 12:28:50 -05001371template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001372void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001373 LabelType* true_label,
1374 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001375 if (cond->IsFPConditionTrueIfNaN()) {
1376 __ j(kUnordered, true_label);
1377 } else if (cond->IsFPConditionFalseIfNaN()) {
1378 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001379 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001380 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001381}
1382
Mark Mendell152408f2015-12-31 12:28:50 -05001383template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001384void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001385 LabelType* true_label,
1386 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001387 LocationSummary* locations = cond->GetLocations();
1388 Location left = locations->InAt(0);
1389 Location right = locations->InAt(1);
1390 IfCondition if_cond = cond->GetCondition();
1391
Mark Mendellc4701932015-04-10 13:18:51 -04001392 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001393 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001394 IfCondition true_high_cond = if_cond;
1395 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001396 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001397
1398 // Set the conditions for the test, remembering that == needs to be
1399 // decided using the low words.
1400 switch (if_cond) {
1401 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001402 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001403 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001404 break;
1405 case kCondLT:
1406 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001407 break;
1408 case kCondLE:
1409 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001410 break;
1411 case kCondGT:
1412 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001413 break;
1414 case kCondGE:
1415 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001416 break;
Aart Bike9f37602015-10-09 11:15:55 -07001417 case kCondB:
1418 false_high_cond = kCondA;
1419 break;
1420 case kCondBE:
1421 true_high_cond = kCondB;
1422 break;
1423 case kCondA:
1424 false_high_cond = kCondB;
1425 break;
1426 case kCondAE:
1427 true_high_cond = kCondA;
1428 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001429 }
1430
1431 if (right.IsConstant()) {
1432 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001433 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001434 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001435
Aart Bika19616e2016-02-01 18:57:58 -08001436 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001437 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001438 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001439 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001440 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001441 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001442 __ j(X86Condition(true_high_cond), true_label);
1443 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001444 }
1445 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001446 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001447 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001448 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001449 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001450
1451 __ cmpl(left_high, right_high);
1452 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001453 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001454 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001455 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001456 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001457 __ j(X86Condition(true_high_cond), true_label);
1458 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001459 }
1460 // Must be equal high, so compare the lows.
1461 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001462 } else {
1463 DCHECK(right.IsDoubleStackSlot());
1464 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1465 if (if_cond == kCondNE) {
1466 __ j(X86Condition(true_high_cond), true_label);
1467 } else if (if_cond == kCondEQ) {
1468 __ j(X86Condition(false_high_cond), false_label);
1469 } else {
1470 __ j(X86Condition(true_high_cond), true_label);
1471 __ j(X86Condition(false_high_cond), false_label);
1472 }
1473 // Must be equal high, so compare the lows.
1474 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001475 }
1476 // The last comparison might be unsigned.
1477 __ j(final_condition, true_label);
1478}
1479
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001480void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1481 Location rhs,
1482 HInstruction* insn,
1483 bool is_double) {
1484 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1485 if (is_double) {
1486 if (rhs.IsFpuRegister()) {
1487 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1488 } else if (const_area != nullptr) {
1489 DCHECK(const_area->IsEmittedAtUseSite());
1490 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1491 codegen_->LiteralDoubleAddress(
1492 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1493 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1494 } else {
1495 DCHECK(rhs.IsDoubleStackSlot());
1496 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1497 }
1498 } else {
1499 if (rhs.IsFpuRegister()) {
1500 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1501 } else if (const_area != nullptr) {
1502 DCHECK(const_area->IsEmittedAtUseSite());
1503 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1504 codegen_->LiteralFloatAddress(
1505 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1506 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1507 } else {
1508 DCHECK(rhs.IsStackSlot());
1509 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1510 }
1511 }
1512}
1513
Mark Mendell152408f2015-12-31 12:28:50 -05001514template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001515void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001516 LabelType* true_target_in,
1517 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001518 // Generated branching requires both targets to be explicit. If either of the
1519 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001520 LabelType fallthrough_target;
1521 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1522 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001523
Mark Mendellc4701932015-04-10 13:18:51 -04001524 LocationSummary* locations = condition->GetLocations();
1525 Location left = locations->InAt(0);
1526 Location right = locations->InAt(1);
1527
Mark Mendellc4701932015-04-10 13:18:51 -04001528 Primitive::Type type = condition->InputAt(0)->GetType();
1529 switch (type) {
1530 case Primitive::kPrimLong:
1531 GenerateLongComparesAndJumps(condition, true_target, false_target);
1532 break;
1533 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001534 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001535 GenerateFPJumps(condition, true_target, false_target);
1536 break;
1537 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001538 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001539 GenerateFPJumps(condition, true_target, false_target);
1540 break;
1541 default:
1542 LOG(FATAL) << "Unexpected compare type " << type;
1543 }
1544
David Brazdil0debae72015-11-12 18:37:00 +00001545 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001546 __ jmp(false_target);
1547 }
David Brazdil0debae72015-11-12 18:37:00 +00001548
1549 if (fallthrough_target.IsLinked()) {
1550 __ Bind(&fallthrough_target);
1551 }
Mark Mendellc4701932015-04-10 13:18:51 -04001552}
1553
David Brazdil0debae72015-11-12 18:37:00 +00001554static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1555 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1556 // are set only strictly before `branch`. We can't use the eflags on long/FP
1557 // conditions if they are materialized due to the complex branching.
1558 return cond->IsCondition() &&
1559 cond->GetNext() == branch &&
1560 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1561 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1562}
1563
Mark Mendell152408f2015-12-31 12:28:50 -05001564template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001565void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001566 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001567 LabelType* true_target,
1568 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001569 HInstruction* cond = instruction->InputAt(condition_input_index);
1570
1571 if (true_target == nullptr && false_target == nullptr) {
1572 // Nothing to do. The code always falls through.
1573 return;
1574 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001575 // Constant condition, statically compared against "true" (integer value 1).
1576 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001577 if (true_target != nullptr) {
1578 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001579 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001580 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001581 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001582 if (false_target != nullptr) {
1583 __ jmp(false_target);
1584 }
1585 }
1586 return;
1587 }
1588
1589 // The following code generates these patterns:
1590 // (1) true_target == nullptr && false_target != nullptr
1591 // - opposite condition true => branch to false_target
1592 // (2) true_target != nullptr && false_target == nullptr
1593 // - condition true => branch to true_target
1594 // (3) true_target != nullptr && false_target != nullptr
1595 // - condition true => branch to true_target
1596 // - branch to false_target
1597 if (IsBooleanValueOrMaterializedCondition(cond)) {
1598 if (AreEflagsSetFrom(cond, instruction)) {
1599 if (true_target == nullptr) {
1600 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1601 } else {
1602 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1603 }
1604 } else {
1605 // Materialized condition, compare against 0.
1606 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1607 if (lhs.IsRegister()) {
1608 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1609 } else {
1610 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1611 }
1612 if (true_target == nullptr) {
1613 __ j(kEqual, false_target);
1614 } else {
1615 __ j(kNotEqual, true_target);
1616 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001617 }
1618 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001619 // Condition has not been materialized, use its inputs as the comparison and
1620 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001621 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001622
1623 // If this is a long or FP comparison that has been folded into
1624 // the HCondition, generate the comparison directly.
1625 Primitive::Type type = condition->InputAt(0)->GetType();
1626 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1627 GenerateCompareTestAndBranch(condition, true_target, false_target);
1628 return;
1629 }
1630
1631 Location lhs = condition->GetLocations()->InAt(0);
1632 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001633 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001634 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001635 if (true_target == nullptr) {
1636 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1637 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001638 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001639 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001640 }
David Brazdil0debae72015-11-12 18:37:00 +00001641
1642 // If neither branch falls through (case 3), the conditional branch to `true_target`
1643 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1644 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001645 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001646 }
1647}
1648
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001649void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001650 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1651 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001652 locations->SetInAt(0, Location::Any());
1653 }
1654}
1655
1656void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001657 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1658 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1659 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1660 nullptr : codegen_->GetLabelOf(true_successor);
1661 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1662 nullptr : codegen_->GetLabelOf(false_successor);
1663 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001664}
1665
1666void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1667 LocationSummary* locations = new (GetGraph()->GetArena())
1668 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001669 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001670 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001671 locations->SetInAt(0, Location::Any());
1672 }
1673}
1674
1675void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001676 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001677 GenerateTestAndBranch<Label>(deoptimize,
1678 /* condition_input_index */ 0,
1679 slow_path->GetEntryLabel(),
1680 /* false_target */ nullptr);
1681}
1682
Mingyao Yang063fc772016-08-02 11:02:54 -07001683void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1684 LocationSummary* locations = new (GetGraph()->GetArena())
1685 LocationSummary(flag, LocationSummary::kNoCall);
1686 locations->SetOut(Location::RequiresRegister());
1687}
1688
1689void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1690 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1691 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1692}
1693
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001694static bool SelectCanUseCMOV(HSelect* select) {
1695 // There are no conditional move instructions for XMMs.
1696 if (Primitive::IsFloatingPointType(select->GetType())) {
1697 return false;
1698 }
1699
1700 // A FP condition doesn't generate the single CC that we need.
1701 // In 32 bit mode, a long condition doesn't generate a single CC either.
1702 HInstruction* condition = select->GetCondition();
1703 if (condition->IsCondition()) {
1704 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1705 if (compare_type == Primitive::kPrimLong ||
1706 Primitive::IsFloatingPointType(compare_type)) {
1707 return false;
1708 }
1709 }
1710
1711 // We can generate a CMOV for this Select.
1712 return true;
1713}
1714
David Brazdil74eb1b22015-12-14 11:44:01 +00001715void LocationsBuilderX86::VisitSelect(HSelect* select) {
1716 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001717 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001718 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001719 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001720 } else {
1721 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001722 if (SelectCanUseCMOV(select)) {
1723 if (select->InputAt(1)->IsConstant()) {
1724 // Cmov can't handle a constant value.
1725 locations->SetInAt(1, Location::RequiresRegister());
1726 } else {
1727 locations->SetInAt(1, Location::Any());
1728 }
1729 } else {
1730 locations->SetInAt(1, Location::Any());
1731 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001732 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001733 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1734 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001735 }
1736 locations->SetOut(Location::SameAsFirstInput());
1737}
1738
1739void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1740 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001741 DCHECK(locations->InAt(0).Equals(locations->Out()));
1742 if (SelectCanUseCMOV(select)) {
1743 // If both the condition and the source types are integer, we can generate
1744 // a CMOV to implement Select.
1745
1746 HInstruction* select_condition = select->GetCondition();
1747 Condition cond = kNotEqual;
1748
1749 // Figure out how to test the 'condition'.
1750 if (select_condition->IsCondition()) {
1751 HCondition* condition = select_condition->AsCondition();
1752 if (!condition->IsEmittedAtUseSite()) {
1753 // This was a previously materialized condition.
1754 // Can we use the existing condition code?
1755 if (AreEflagsSetFrom(condition, select)) {
1756 // Materialization was the previous instruction. Condition codes are right.
1757 cond = X86Condition(condition->GetCondition());
1758 } else {
1759 // No, we have to recreate the condition code.
1760 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1761 __ testl(cond_reg, cond_reg);
1762 }
1763 } else {
1764 // We can't handle FP or long here.
1765 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1766 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1767 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001768 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001769 cond = X86Condition(condition->GetCondition());
1770 }
1771 } else {
1772 // Must be a boolean condition, which needs to be compared to 0.
1773 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1774 __ testl(cond_reg, cond_reg);
1775 }
1776
1777 // If the condition is true, overwrite the output, which already contains false.
1778 Location false_loc = locations->InAt(0);
1779 Location true_loc = locations->InAt(1);
1780 if (select->GetType() == Primitive::kPrimLong) {
1781 // 64 bit conditional move.
1782 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1783 Register false_low = false_loc.AsRegisterPairLow<Register>();
1784 if (true_loc.IsRegisterPair()) {
1785 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1786 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1787 } else {
1788 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1789 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1790 }
1791 } else {
1792 // 32 bit conditional move.
1793 Register false_reg = false_loc.AsRegister<Register>();
1794 if (true_loc.IsRegister()) {
1795 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1796 } else {
1797 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1798 }
1799 }
1800 } else {
1801 NearLabel false_target;
1802 GenerateTestAndBranch<NearLabel>(
1803 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1804 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1805 __ Bind(&false_target);
1806 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001807}
1808
David Srbecky0cf44932015-12-09 14:09:59 +00001809void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1810 new (GetGraph()->GetArena()) LocationSummary(info);
1811}
1812
David Srbeckyd28f4a02016-03-14 17:14:24 +00001813void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1814 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001815}
1816
1817void CodeGeneratorX86::GenerateNop() {
1818 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001819}
1820
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001821void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001822 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001823 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001824 // Handle the long/FP comparisons made in instruction simplification.
1825 switch (cond->InputAt(0)->GetType()) {
1826 case Primitive::kPrimLong: {
1827 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001828 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001829 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001830 locations->SetOut(Location::RequiresRegister());
1831 }
1832 break;
1833 }
1834 case Primitive::kPrimFloat:
1835 case Primitive::kPrimDouble: {
1836 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001837 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1838 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1839 } else if (cond->InputAt(1)->IsConstant()) {
1840 locations->SetInAt(1, Location::RequiresFpuRegister());
1841 } else {
1842 locations->SetInAt(1, Location::Any());
1843 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001844 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001845 locations->SetOut(Location::RequiresRegister());
1846 }
1847 break;
1848 }
1849 default:
1850 locations->SetInAt(0, Location::RequiresRegister());
1851 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001852 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001853 // We need a byte register.
1854 locations->SetOut(Location::RegisterLocation(ECX));
1855 }
1856 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001857 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001858}
1859
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001860void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001861 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001862 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001863 }
Mark Mendellc4701932015-04-10 13:18:51 -04001864
1865 LocationSummary* locations = cond->GetLocations();
1866 Location lhs = locations->InAt(0);
1867 Location rhs = locations->InAt(1);
1868 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001869 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001870
1871 switch (cond->InputAt(0)->GetType()) {
1872 default: {
1873 // Integer case.
1874
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001875 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001876 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001877 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001878 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001879 return;
1880 }
1881 case Primitive::kPrimLong:
1882 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1883 break;
1884 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001885 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001886 GenerateFPJumps(cond, &true_label, &false_label);
1887 break;
1888 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001889 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001890 GenerateFPJumps(cond, &true_label, &false_label);
1891 break;
1892 }
1893
1894 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001895 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001896
Roland Levillain4fa13f62015-07-06 18:11:54 +01001897 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001898 __ Bind(&false_label);
1899 __ xorl(reg, reg);
1900 __ jmp(&done_label);
1901
Roland Levillain4fa13f62015-07-06 18:11:54 +01001902 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001903 __ Bind(&true_label);
1904 __ movl(reg, Immediate(1));
1905 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001906}
1907
1908void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001909 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001910}
1911
1912void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001913 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001914}
1915
1916void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001917 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001918}
1919
1920void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001921 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001922}
1923
1924void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001925 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001926}
1927
1928void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001929 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001930}
1931
1932void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001933 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001934}
1935
1936void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001937 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001938}
1939
1940void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001941 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001942}
1943
1944void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001945 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001946}
1947
1948void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001949 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001950}
1951
1952void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001953 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001954}
1955
Aart Bike9f37602015-10-09 11:15:55 -07001956void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001957 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001958}
1959
1960void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001961 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001962}
1963
1964void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001965 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001966}
1967
1968void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001969 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001970}
1971
1972void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001973 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001974}
1975
1976void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001977 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001978}
1979
1980void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001981 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001982}
1983
1984void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001985 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001986}
1987
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001988void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001989 LocationSummary* locations =
1990 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001991 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001992}
1993
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001994void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001995 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001996}
1997
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001998void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1999 LocationSummary* locations =
2000 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2001 locations->SetOut(Location::ConstantLocation(constant));
2002}
2003
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002004void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002005 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002006}
2007
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002008void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002009 LocationSummary* locations =
2010 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002011 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002012}
2013
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002014void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002015 // Will be generated at use site.
2016}
2017
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002018void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2019 LocationSummary* locations =
2020 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2021 locations->SetOut(Location::ConstantLocation(constant));
2022}
2023
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002024void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002025 // Will be generated at use site.
2026}
2027
2028void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2029 LocationSummary* locations =
2030 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2031 locations->SetOut(Location::ConstantLocation(constant));
2032}
2033
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002034void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002035 // Will be generated at use site.
2036}
2037
Calin Juravle27df7582015-04-17 19:12:31 +01002038void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2039 memory_barrier->SetLocations(nullptr);
2040}
2041
2042void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002043 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002044}
2045
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002046void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002047 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002048}
2049
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002050void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002051 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002052}
2053
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002054void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002055 LocationSummary* locations =
2056 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002057 switch (ret->InputAt(0)->GetType()) {
2058 case Primitive::kPrimBoolean:
2059 case Primitive::kPrimByte:
2060 case Primitive::kPrimChar:
2061 case Primitive::kPrimShort:
2062 case Primitive::kPrimInt:
2063 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002064 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002065 break;
2066
2067 case Primitive::kPrimLong:
2068 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002069 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002070 break;
2071
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002072 case Primitive::kPrimFloat:
2073 case Primitive::kPrimDouble:
2074 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002075 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002076 break;
2077
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002078 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002079 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002080 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002081}
2082
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002083void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002084 if (kIsDebugBuild) {
2085 switch (ret->InputAt(0)->GetType()) {
2086 case Primitive::kPrimBoolean:
2087 case Primitive::kPrimByte:
2088 case Primitive::kPrimChar:
2089 case Primitive::kPrimShort:
2090 case Primitive::kPrimInt:
2091 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002092 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002093 break;
2094
2095 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002096 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2097 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002098 break;
2099
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002100 case Primitive::kPrimFloat:
2101 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002102 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002103 break;
2104
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002105 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002106 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002107 }
2108 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002109 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002110}
2111
Calin Juravle175dc732015-08-25 15:42:32 +01002112void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2113 // The trampoline uses the same calling convention as dex calling conventions,
2114 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2115 // the method_idx.
2116 HandleInvoke(invoke);
2117}
2118
2119void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2120 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2121}
2122
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002123void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002124 // Explicit clinit checks triggered by static invokes must have been pruned by
2125 // art::PrepareForRegisterAllocation.
2126 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002127
Mark Mendellfb8d2792015-03-31 22:16:59 -04002128 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002129 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002130 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002131 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002132 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002133 return;
2134 }
2135
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002136 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002137
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002138 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2139 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002140 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002141 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002142}
2143
Mark Mendell09ed1a32015-03-25 08:30:06 -04002144static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2145 if (invoke->GetLocations()->Intrinsified()) {
2146 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2147 intrinsic.Dispatch(invoke);
2148 return true;
2149 }
2150 return false;
2151}
2152
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002153void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002154 // Explicit clinit checks triggered by static invokes must have been pruned by
2155 // art::PrepareForRegisterAllocation.
2156 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002157
Mark Mendell09ed1a32015-03-25 08:30:06 -04002158 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2159 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002160 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002161
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002162 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002163 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002164 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002165 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002166}
2167
2168void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002169 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2170 if (intrinsic.TryDispatch(invoke)) {
2171 return;
2172 }
2173
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002174 HandleInvoke(invoke);
2175}
2176
2177void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002178 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002179 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002180}
2181
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002182void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002183 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2184 return;
2185 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002186
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002187 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002188 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002189 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002190}
2191
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002192void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002193 // This call to HandleInvoke allocates a temporary (core) register
2194 // which is also used to transfer the hidden argument from FP to
2195 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002196 HandleInvoke(invoke);
2197 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002198 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002199}
2200
2201void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2202 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002203 LocationSummary* locations = invoke->GetLocations();
2204 Register temp = locations->GetTemp(0).AsRegister<Register>();
2205 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002206 Location receiver = locations->InAt(0);
2207 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2208
Roland Levillain0d5a2812015-11-13 10:07:31 +00002209 // Set the hidden argument. This is safe to do this here, as XMM7
2210 // won't be modified thereafter, before the `call` instruction.
2211 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002212 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002213 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002214
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002215 if (receiver.IsStackSlot()) {
2216 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002217 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002218 __ movl(temp, Address(temp, class_offset));
2219 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002220 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002221 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002222 }
Roland Levillain4d027112015-07-01 15:41:14 +01002223 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002224 // Instead of simply (possibly) unpoisoning `temp` here, we should
2225 // emit a read barrier for the previous class reference load.
2226 // However this is not required in practice, as this is an
2227 // intermediate/temporary reference and because the current
2228 // concurrent copying collector keeps the from-space memory
2229 // intact/accessible until the end of the marking phase (the
2230 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002231 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002232 // temp = temp->GetAddressOfIMT()
2233 __ movl(temp,
2234 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002235 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002236 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002237 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002238 __ movl(temp, Address(temp, method_offset));
2239 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002240 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002241 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002242
2243 DCHECK(!codegen_->IsLeafMethod());
2244 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2245}
2246
Roland Levillain88cb1752014-10-20 16:36:47 +01002247void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2248 LocationSummary* locations =
2249 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2250 switch (neg->GetResultType()) {
2251 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002252 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002253 locations->SetInAt(0, Location::RequiresRegister());
2254 locations->SetOut(Location::SameAsFirstInput());
2255 break;
2256
Roland Levillain88cb1752014-10-20 16:36:47 +01002257 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002258 locations->SetInAt(0, Location::RequiresFpuRegister());
2259 locations->SetOut(Location::SameAsFirstInput());
2260 locations->AddTemp(Location::RequiresRegister());
2261 locations->AddTemp(Location::RequiresFpuRegister());
2262 break;
2263
Roland Levillain88cb1752014-10-20 16:36:47 +01002264 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002265 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002266 locations->SetOut(Location::SameAsFirstInput());
2267 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002268 break;
2269
2270 default:
2271 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2272 }
2273}
2274
2275void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2276 LocationSummary* locations = neg->GetLocations();
2277 Location out = locations->Out();
2278 Location in = locations->InAt(0);
2279 switch (neg->GetResultType()) {
2280 case Primitive::kPrimInt:
2281 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002282 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002283 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002284 break;
2285
2286 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002287 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002288 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002289 __ negl(out.AsRegisterPairLow<Register>());
2290 // Negation is similar to subtraction from zero. The least
2291 // significant byte triggers a borrow when it is different from
2292 // zero; to take it into account, add 1 to the most significant
2293 // byte if the carry flag (CF) is set to 1 after the first NEGL
2294 // operation.
2295 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2296 __ negl(out.AsRegisterPairHigh<Register>());
2297 break;
2298
Roland Levillain5368c212014-11-27 15:03:41 +00002299 case Primitive::kPrimFloat: {
2300 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002301 Register constant = locations->GetTemp(0).AsRegister<Register>();
2302 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002303 // Implement float negation with an exclusive or with value
2304 // 0x80000000 (mask for bit 31, representing the sign of a
2305 // single-precision floating-point number).
2306 __ movl(constant, Immediate(INT32_C(0x80000000)));
2307 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002308 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002309 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002310 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002311
Roland Levillain5368c212014-11-27 15:03:41 +00002312 case Primitive::kPrimDouble: {
2313 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002314 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002315 // Implement double negation with an exclusive or with value
2316 // 0x8000000000000000 (mask for bit 63, representing the sign of
2317 // a double-precision floating-point number).
2318 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002319 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002320 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002321 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002322
2323 default:
2324 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2325 }
2326}
2327
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002328void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2329 LocationSummary* locations =
2330 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2331 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2332 locations->SetInAt(0, Location::RequiresFpuRegister());
2333 locations->SetInAt(1, Location::RequiresRegister());
2334 locations->SetOut(Location::SameAsFirstInput());
2335 locations->AddTemp(Location::RequiresFpuRegister());
2336}
2337
2338void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2339 LocationSummary* locations = neg->GetLocations();
2340 Location out = locations->Out();
2341 DCHECK(locations->InAt(0).Equals(out));
2342
2343 Register constant_area = locations->InAt(1).AsRegister<Register>();
2344 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2345 if (neg->GetType() == Primitive::kPrimFloat) {
2346 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2347 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2348 } else {
2349 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2350 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2351 }
2352}
2353
Roland Levillaindff1f282014-11-05 14:15:05 +00002354void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002355 Primitive::Type result_type = conversion->GetResultType();
2356 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002357 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002358
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002359 // The float-to-long and double-to-long type conversions rely on a
2360 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002361 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002362 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2363 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002364 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002365 : LocationSummary::kNoCall;
2366 LocationSummary* locations =
2367 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2368
David Brazdilb2bd1c52015-03-25 11:17:37 +00002369 // The Java language does not allow treating boolean as an integral type but
2370 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002371
Roland Levillaindff1f282014-11-05 14:15:05 +00002372 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002373 case Primitive::kPrimByte:
2374 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002375 case Primitive::kPrimLong: {
2376 // Type conversion from long to byte is a result of code transformations.
2377 HInstruction* input = conversion->InputAt(0);
2378 Location input_location = input->IsConstant()
2379 ? Location::ConstantLocation(input->AsConstant())
2380 : Location::RegisterPairLocation(EAX, EDX);
2381 locations->SetInAt(0, input_location);
2382 // Make the output overlap to please the register allocator. This greatly simplifies
2383 // the validation of the linear scan implementation
2384 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2385 break;
2386 }
David Brazdil46e2a392015-03-16 17:31:52 +00002387 case Primitive::kPrimBoolean:
2388 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002389 case Primitive::kPrimShort:
2390 case Primitive::kPrimInt:
2391 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002392 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002393 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2394 // Make the output overlap to please the register allocator. This greatly simplifies
2395 // the validation of the linear scan implementation
2396 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002397 break;
2398
2399 default:
2400 LOG(FATAL) << "Unexpected type conversion from " << input_type
2401 << " to " << result_type;
2402 }
2403 break;
2404
Roland Levillain01a8d712014-11-14 16:27:39 +00002405 case Primitive::kPrimShort:
2406 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002407 case Primitive::kPrimLong:
2408 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002409 case Primitive::kPrimBoolean:
2410 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002411 case Primitive::kPrimByte:
2412 case Primitive::kPrimInt:
2413 case Primitive::kPrimChar:
2414 // Processing a Dex `int-to-short' instruction.
2415 locations->SetInAt(0, Location::Any());
2416 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2417 break;
2418
2419 default:
2420 LOG(FATAL) << "Unexpected type conversion from " << input_type
2421 << " to " << result_type;
2422 }
2423 break;
2424
Roland Levillain946e1432014-11-11 17:35:19 +00002425 case Primitive::kPrimInt:
2426 switch (input_type) {
2427 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002428 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002429 locations->SetInAt(0, Location::Any());
2430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2431 break;
2432
2433 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002434 // Processing a Dex `float-to-int' instruction.
2435 locations->SetInAt(0, Location::RequiresFpuRegister());
2436 locations->SetOut(Location::RequiresRegister());
2437 locations->AddTemp(Location::RequiresFpuRegister());
2438 break;
2439
Roland Levillain946e1432014-11-11 17:35:19 +00002440 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002441 // Processing a Dex `double-to-int' instruction.
2442 locations->SetInAt(0, Location::RequiresFpuRegister());
2443 locations->SetOut(Location::RequiresRegister());
2444 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002445 break;
2446
2447 default:
2448 LOG(FATAL) << "Unexpected type conversion from " << input_type
2449 << " to " << result_type;
2450 }
2451 break;
2452
Roland Levillaindff1f282014-11-05 14:15:05 +00002453 case Primitive::kPrimLong:
2454 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002455 case Primitive::kPrimBoolean:
2456 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002457 case Primitive::kPrimByte:
2458 case Primitive::kPrimShort:
2459 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002460 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002461 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002462 locations->SetInAt(0, Location::RegisterLocation(EAX));
2463 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2464 break;
2465
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002466 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002467 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002468 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002469 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002470 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2471 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2472
Vladimir Marko949c91f2015-01-27 10:48:44 +00002473 // The runtime helper puts the result in EAX, EDX.
2474 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002475 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002476 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002477
2478 default:
2479 LOG(FATAL) << "Unexpected type conversion from " << input_type
2480 << " to " << result_type;
2481 }
2482 break;
2483
Roland Levillain981e4542014-11-14 11:47:14 +00002484 case Primitive::kPrimChar:
2485 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002486 case Primitive::kPrimLong:
2487 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002488 case Primitive::kPrimBoolean:
2489 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002490 case Primitive::kPrimByte:
2491 case Primitive::kPrimShort:
2492 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002493 // Processing a Dex `int-to-char' instruction.
2494 locations->SetInAt(0, Location::Any());
2495 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2496 break;
2497
2498 default:
2499 LOG(FATAL) << "Unexpected type conversion from " << input_type
2500 << " to " << result_type;
2501 }
2502 break;
2503
Roland Levillaindff1f282014-11-05 14:15:05 +00002504 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002505 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002506 case Primitive::kPrimBoolean:
2507 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002508 case Primitive::kPrimByte:
2509 case Primitive::kPrimShort:
2510 case Primitive::kPrimInt:
2511 case Primitive::kPrimChar:
2512 // Processing a Dex `int-to-float' instruction.
2513 locations->SetInAt(0, Location::RequiresRegister());
2514 locations->SetOut(Location::RequiresFpuRegister());
2515 break;
2516
2517 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002518 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002519 locations->SetInAt(0, Location::Any());
2520 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002521 break;
2522
Roland Levillaincff13742014-11-17 14:32:17 +00002523 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002524 // Processing a Dex `double-to-float' instruction.
2525 locations->SetInAt(0, Location::RequiresFpuRegister());
2526 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002527 break;
2528
2529 default:
2530 LOG(FATAL) << "Unexpected type conversion from " << input_type
2531 << " to " << result_type;
2532 };
2533 break;
2534
Roland Levillaindff1f282014-11-05 14:15:05 +00002535 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002536 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002537 case Primitive::kPrimBoolean:
2538 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002539 case Primitive::kPrimByte:
2540 case Primitive::kPrimShort:
2541 case Primitive::kPrimInt:
2542 case Primitive::kPrimChar:
2543 // Processing a Dex `int-to-double' instruction.
2544 locations->SetInAt(0, Location::RequiresRegister());
2545 locations->SetOut(Location::RequiresFpuRegister());
2546 break;
2547
2548 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002549 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002550 locations->SetInAt(0, Location::Any());
2551 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002552 break;
2553
Roland Levillaincff13742014-11-17 14:32:17 +00002554 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002555 // Processing a Dex `float-to-double' instruction.
2556 locations->SetInAt(0, Location::RequiresFpuRegister());
2557 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002558 break;
2559
2560 default:
2561 LOG(FATAL) << "Unexpected type conversion from " << input_type
2562 << " to " << result_type;
2563 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002564 break;
2565
2566 default:
2567 LOG(FATAL) << "Unexpected type conversion from " << input_type
2568 << " to " << result_type;
2569 }
2570}
2571
2572void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2573 LocationSummary* locations = conversion->GetLocations();
2574 Location out = locations->Out();
2575 Location in = locations->InAt(0);
2576 Primitive::Type result_type = conversion->GetResultType();
2577 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002578 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002579 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002580 case Primitive::kPrimByte:
2581 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002582 case Primitive::kPrimLong:
2583 // Type conversion from long to byte is a result of code transformations.
2584 if (in.IsRegisterPair()) {
2585 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2586 } else {
2587 DCHECK(in.GetConstant()->IsLongConstant());
2588 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2589 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2590 }
2591 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002592 case Primitive::kPrimBoolean:
2593 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002594 case Primitive::kPrimShort:
2595 case Primitive::kPrimInt:
2596 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002597 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002598 if (in.IsRegister()) {
2599 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002600 } else {
2601 DCHECK(in.GetConstant()->IsIntConstant());
2602 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2603 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2604 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002605 break;
2606
2607 default:
2608 LOG(FATAL) << "Unexpected type conversion from " << input_type
2609 << " to " << result_type;
2610 }
2611 break;
2612
Roland Levillain01a8d712014-11-14 16:27:39 +00002613 case Primitive::kPrimShort:
2614 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002615 case Primitive::kPrimLong:
2616 // Type conversion from long to short is a result of code transformations.
2617 if (in.IsRegisterPair()) {
2618 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2619 } else if (in.IsDoubleStackSlot()) {
2620 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2621 } else {
2622 DCHECK(in.GetConstant()->IsLongConstant());
2623 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2624 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2625 }
2626 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002627 case Primitive::kPrimBoolean:
2628 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002629 case Primitive::kPrimByte:
2630 case Primitive::kPrimInt:
2631 case Primitive::kPrimChar:
2632 // Processing a Dex `int-to-short' instruction.
2633 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002634 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002635 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002636 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002637 } else {
2638 DCHECK(in.GetConstant()->IsIntConstant());
2639 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002640 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002641 }
2642 break;
2643
2644 default:
2645 LOG(FATAL) << "Unexpected type conversion from " << input_type
2646 << " to " << result_type;
2647 }
2648 break;
2649
Roland Levillain946e1432014-11-11 17:35:19 +00002650 case Primitive::kPrimInt:
2651 switch (input_type) {
2652 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002653 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002654 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002655 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002656 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002657 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002658 } else {
2659 DCHECK(in.IsConstant());
2660 DCHECK(in.GetConstant()->IsLongConstant());
2661 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002662 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002663 }
2664 break;
2665
Roland Levillain3f8f9362014-12-02 17:45:01 +00002666 case Primitive::kPrimFloat: {
2667 // Processing a Dex `float-to-int' instruction.
2668 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2669 Register output = out.AsRegister<Register>();
2670 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002671 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002672
2673 __ movl(output, Immediate(kPrimIntMax));
2674 // temp = int-to-float(output)
2675 __ cvtsi2ss(temp, output);
2676 // if input >= temp goto done
2677 __ comiss(input, temp);
2678 __ j(kAboveEqual, &done);
2679 // if input == NaN goto nan
2680 __ j(kUnordered, &nan);
2681 // output = float-to-int-truncate(input)
2682 __ cvttss2si(output, input);
2683 __ jmp(&done);
2684 __ Bind(&nan);
2685 // output = 0
2686 __ xorl(output, output);
2687 __ Bind(&done);
2688 break;
2689 }
2690
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002691 case Primitive::kPrimDouble: {
2692 // Processing a Dex `double-to-int' instruction.
2693 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2694 Register output = out.AsRegister<Register>();
2695 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002696 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002697
2698 __ movl(output, Immediate(kPrimIntMax));
2699 // temp = int-to-double(output)
2700 __ cvtsi2sd(temp, output);
2701 // if input >= temp goto done
2702 __ comisd(input, temp);
2703 __ j(kAboveEqual, &done);
2704 // if input == NaN goto nan
2705 __ j(kUnordered, &nan);
2706 // output = double-to-int-truncate(input)
2707 __ cvttsd2si(output, input);
2708 __ jmp(&done);
2709 __ Bind(&nan);
2710 // output = 0
2711 __ xorl(output, output);
2712 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002713 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002714 }
Roland Levillain946e1432014-11-11 17:35:19 +00002715
2716 default:
2717 LOG(FATAL) << "Unexpected type conversion from " << input_type
2718 << " to " << result_type;
2719 }
2720 break;
2721
Roland Levillaindff1f282014-11-05 14:15:05 +00002722 case Primitive::kPrimLong:
2723 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002724 case Primitive::kPrimBoolean:
2725 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002726 case Primitive::kPrimByte:
2727 case Primitive::kPrimShort:
2728 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002729 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002730 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002731 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2732 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002733 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002734 __ cdq();
2735 break;
2736
2737 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002738 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002739 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002740 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002741 break;
2742
Roland Levillaindff1f282014-11-05 14:15:05 +00002743 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002744 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002745 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002746 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002747 break;
2748
2749 default:
2750 LOG(FATAL) << "Unexpected type conversion from " << input_type
2751 << " to " << result_type;
2752 }
2753 break;
2754
Roland Levillain981e4542014-11-14 11:47:14 +00002755 case Primitive::kPrimChar:
2756 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002757 case Primitive::kPrimLong:
2758 // Type conversion from long to short is a result of code transformations.
2759 if (in.IsRegisterPair()) {
2760 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2761 } else if (in.IsDoubleStackSlot()) {
2762 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2763 } else {
2764 DCHECK(in.GetConstant()->IsLongConstant());
2765 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2766 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2767 }
2768 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002769 case Primitive::kPrimBoolean:
2770 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002771 case Primitive::kPrimByte:
2772 case Primitive::kPrimShort:
2773 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002774 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2775 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002776 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002777 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002778 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002779 } else {
2780 DCHECK(in.GetConstant()->IsIntConstant());
2781 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002782 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002783 }
2784 break;
2785
2786 default:
2787 LOG(FATAL) << "Unexpected type conversion from " << input_type
2788 << " to " << result_type;
2789 }
2790 break;
2791
Roland Levillaindff1f282014-11-05 14:15:05 +00002792 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002793 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002794 case Primitive::kPrimBoolean:
2795 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002796 case Primitive::kPrimByte:
2797 case Primitive::kPrimShort:
2798 case Primitive::kPrimInt:
2799 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002800 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002801 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002802 break;
2803
Roland Levillain6d0e4832014-11-27 18:31:21 +00002804 case Primitive::kPrimLong: {
2805 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002806 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002807
Roland Levillain232ade02015-04-20 15:14:36 +01002808 // Create stack space for the call to
2809 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2810 // TODO: enhance register allocator to ask for stack temporaries.
2811 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2812 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2813 __ subl(ESP, Immediate(adjustment));
2814 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002815
Roland Levillain232ade02015-04-20 15:14:36 +01002816 // Load the value to the FP stack, using temporaries if needed.
2817 PushOntoFPStack(in, 0, adjustment, false, true);
2818
2819 if (out.IsStackSlot()) {
2820 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2821 } else {
2822 __ fstps(Address(ESP, 0));
2823 Location stack_temp = Location::StackSlot(0);
2824 codegen_->Move32(out, stack_temp);
2825 }
2826
2827 // Remove the temporary stack space we allocated.
2828 if (adjustment != 0) {
2829 __ addl(ESP, Immediate(adjustment));
2830 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002831 break;
2832 }
2833
Roland Levillaincff13742014-11-17 14:32:17 +00002834 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002835 // Processing a Dex `double-to-float' instruction.
2836 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002837 break;
2838
2839 default:
2840 LOG(FATAL) << "Unexpected type conversion from " << input_type
2841 << " to " << result_type;
2842 };
2843 break;
2844
Roland Levillaindff1f282014-11-05 14:15:05 +00002845 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002846 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002847 case Primitive::kPrimBoolean:
2848 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002849 case Primitive::kPrimByte:
2850 case Primitive::kPrimShort:
2851 case Primitive::kPrimInt:
2852 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002853 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002854 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002855 break;
2856
Roland Levillain647b9ed2014-11-27 12:06:00 +00002857 case Primitive::kPrimLong: {
2858 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002859 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002860
Roland Levillain232ade02015-04-20 15:14:36 +01002861 // Create stack space for the call to
2862 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2863 // TODO: enhance register allocator to ask for stack temporaries.
2864 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2865 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2866 __ subl(ESP, Immediate(adjustment));
2867 }
2868
2869 // Load the value to the FP stack, using temporaries if needed.
2870 PushOntoFPStack(in, 0, adjustment, false, true);
2871
2872 if (out.IsDoubleStackSlot()) {
2873 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2874 } else {
2875 __ fstpl(Address(ESP, 0));
2876 Location stack_temp = Location::DoubleStackSlot(0);
2877 codegen_->Move64(out, stack_temp);
2878 }
2879
2880 // Remove the temporary stack space we allocated.
2881 if (adjustment != 0) {
2882 __ addl(ESP, Immediate(adjustment));
2883 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002884 break;
2885 }
2886
Roland Levillaincff13742014-11-17 14:32:17 +00002887 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002888 // Processing a Dex `float-to-double' instruction.
2889 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002890 break;
2891
2892 default:
2893 LOG(FATAL) << "Unexpected type conversion from " << input_type
2894 << " to " << result_type;
2895 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002896 break;
2897
2898 default:
2899 LOG(FATAL) << "Unexpected type conversion from " << input_type
2900 << " to " << result_type;
2901 }
2902}
2903
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002904void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002905 LocationSummary* locations =
2906 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002907 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002908 case Primitive::kPrimInt: {
2909 locations->SetInAt(0, Location::RequiresRegister());
2910 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2911 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2912 break;
2913 }
2914
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002915 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002916 locations->SetInAt(0, Location::RequiresRegister());
2917 locations->SetInAt(1, Location::Any());
2918 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002919 break;
2920 }
2921
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002922 case Primitive::kPrimFloat:
2923 case Primitive::kPrimDouble: {
2924 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002925 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2926 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002927 } else if (add->InputAt(1)->IsConstant()) {
2928 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002929 } else {
2930 locations->SetInAt(1, Location::Any());
2931 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002932 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002933 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002934 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002935
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002936 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002937 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2938 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002939 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002940}
2941
2942void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2943 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002944 Location first = locations->InAt(0);
2945 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002946 Location out = locations->Out();
2947
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002948 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002949 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002950 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002951 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2952 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002953 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2954 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002955 } else {
2956 __ leal(out.AsRegister<Register>(), Address(
2957 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2958 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002959 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002960 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2961 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2962 __ addl(out.AsRegister<Register>(), Immediate(value));
2963 } else {
2964 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2965 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002966 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002967 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002968 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002969 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002970 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002971 }
2972
2973 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002974 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002975 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2976 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002977 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002978 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2979 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002980 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002981 } else {
2982 DCHECK(second.IsConstant()) << second;
2983 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2984 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2985 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002986 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002987 break;
2988 }
2989
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002990 case Primitive::kPrimFloat: {
2991 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002992 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002993 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2994 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002995 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002996 __ addss(first.AsFpuRegister<XmmRegister>(),
2997 codegen_->LiteralFloatAddress(
2998 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2999 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3000 } else {
3001 DCHECK(second.IsStackSlot());
3002 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003003 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003004 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003005 }
3006
3007 case Primitive::kPrimDouble: {
3008 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003009 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003010 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3011 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003012 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003013 __ addsd(first.AsFpuRegister<XmmRegister>(),
3014 codegen_->LiteralDoubleAddress(
3015 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3016 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3017 } else {
3018 DCHECK(second.IsDoubleStackSlot());
3019 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003020 }
3021 break;
3022 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003023
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003024 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003025 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003026 }
3027}
3028
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003029void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003030 LocationSummary* locations =
3031 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003032 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003033 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003034 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003035 locations->SetInAt(0, Location::RequiresRegister());
3036 locations->SetInAt(1, Location::Any());
3037 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003038 break;
3039 }
Calin Juravle11351682014-10-23 15:38:15 +01003040 case Primitive::kPrimFloat:
3041 case Primitive::kPrimDouble: {
3042 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003043 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3044 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003045 } else if (sub->InputAt(1)->IsConstant()) {
3046 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003047 } else {
3048 locations->SetInAt(1, Location::Any());
3049 }
Calin Juravle11351682014-10-23 15:38:15 +01003050 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003051 break;
Calin Juravle11351682014-10-23 15:38:15 +01003052 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003053
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003054 default:
Calin Juravle11351682014-10-23 15:38:15 +01003055 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003056 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003057}
3058
3059void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3060 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003061 Location first = locations->InAt(0);
3062 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003063 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003064 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003065 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003066 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003067 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003068 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003069 __ subl(first.AsRegister<Register>(),
3070 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003071 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003072 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003073 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003074 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003075 }
3076
3077 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003078 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003079 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3080 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003081 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003082 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003083 __ sbbl(first.AsRegisterPairHigh<Register>(),
3084 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003085 } else {
3086 DCHECK(second.IsConstant()) << second;
3087 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3088 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3089 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003090 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003091 break;
3092 }
3093
Calin Juravle11351682014-10-23 15:38:15 +01003094 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003095 if (second.IsFpuRegister()) {
3096 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3097 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3098 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003099 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003100 __ subss(first.AsFpuRegister<XmmRegister>(),
3101 codegen_->LiteralFloatAddress(
3102 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3103 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3104 } else {
3105 DCHECK(second.IsStackSlot());
3106 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3107 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003108 break;
Calin Juravle11351682014-10-23 15:38:15 +01003109 }
3110
3111 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003112 if (second.IsFpuRegister()) {
3113 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3114 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3115 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003116 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003117 __ subsd(first.AsFpuRegister<XmmRegister>(),
3118 codegen_->LiteralDoubleAddress(
3119 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3120 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3121 } else {
3122 DCHECK(second.IsDoubleStackSlot());
3123 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3124 }
Calin Juravle11351682014-10-23 15:38:15 +01003125 break;
3126 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003127
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003128 default:
Calin Juravle11351682014-10-23 15:38:15 +01003129 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003130 }
3131}
3132
Calin Juravle34bacdf2014-10-07 20:23:36 +01003133void LocationsBuilderX86::VisitMul(HMul* mul) {
3134 LocationSummary* locations =
3135 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3136 switch (mul->GetResultType()) {
3137 case Primitive::kPrimInt:
3138 locations->SetInAt(0, Location::RequiresRegister());
3139 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003140 if (mul->InputAt(1)->IsIntConstant()) {
3141 // Can use 3 operand multiply.
3142 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3143 } else {
3144 locations->SetOut(Location::SameAsFirstInput());
3145 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003146 break;
3147 case Primitive::kPrimLong: {
3148 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003149 locations->SetInAt(1, Location::Any());
3150 locations->SetOut(Location::SameAsFirstInput());
3151 // Needed for imul on 32bits with 64bits output.
3152 locations->AddTemp(Location::RegisterLocation(EAX));
3153 locations->AddTemp(Location::RegisterLocation(EDX));
3154 break;
3155 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003156 case Primitive::kPrimFloat:
3157 case Primitive::kPrimDouble: {
3158 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003159 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3160 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003161 } else if (mul->InputAt(1)->IsConstant()) {
3162 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003163 } else {
3164 locations->SetInAt(1, Location::Any());
3165 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003166 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003167 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003168 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003169
3170 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003171 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003172 }
3173}
3174
3175void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3176 LocationSummary* locations = mul->GetLocations();
3177 Location first = locations->InAt(0);
3178 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003179 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003180
3181 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003182 case Primitive::kPrimInt:
3183 // The constant may have ended up in a register, so test explicitly to avoid
3184 // problems where the output may not be the same as the first operand.
3185 if (mul->InputAt(1)->IsIntConstant()) {
3186 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3187 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3188 } else if (second.IsRegister()) {
3189 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003190 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003191 } else {
3192 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003193 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003194 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003195 }
3196 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003197
3198 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003199 Register in1_hi = first.AsRegisterPairHigh<Register>();
3200 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003201 Register eax = locations->GetTemp(0).AsRegister<Register>();
3202 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003203
3204 DCHECK_EQ(EAX, eax);
3205 DCHECK_EQ(EDX, edx);
3206
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003207 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003208 // output: in1
3209 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3210 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3211 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003212 if (second.IsConstant()) {
3213 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003214
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003215 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3216 int32_t low_value = Low32Bits(value);
3217 int32_t high_value = High32Bits(value);
3218 Immediate low(low_value);
3219 Immediate high(high_value);
3220
3221 __ movl(eax, high);
3222 // eax <- in1.lo * in2.hi
3223 __ imull(eax, in1_lo);
3224 // in1.hi <- in1.hi * in2.lo
3225 __ imull(in1_hi, low);
3226 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3227 __ addl(in1_hi, eax);
3228 // move in2_lo to eax to prepare for double precision
3229 __ movl(eax, low);
3230 // edx:eax <- in1.lo * in2.lo
3231 __ mull(in1_lo);
3232 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3233 __ addl(in1_hi, edx);
3234 // in1.lo <- (in1.lo * in2.lo)[31:0];
3235 __ movl(in1_lo, eax);
3236 } else if (second.IsRegisterPair()) {
3237 Register in2_hi = second.AsRegisterPairHigh<Register>();
3238 Register in2_lo = second.AsRegisterPairLow<Register>();
3239
3240 __ movl(eax, in2_hi);
3241 // eax <- in1.lo * in2.hi
3242 __ imull(eax, in1_lo);
3243 // in1.hi <- in1.hi * in2.lo
3244 __ imull(in1_hi, in2_lo);
3245 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3246 __ addl(in1_hi, eax);
3247 // move in1_lo to eax to prepare for double precision
3248 __ movl(eax, in1_lo);
3249 // edx:eax <- in1.lo * in2.lo
3250 __ mull(in2_lo);
3251 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3252 __ addl(in1_hi, edx);
3253 // in1.lo <- (in1.lo * in2.lo)[31:0];
3254 __ movl(in1_lo, eax);
3255 } else {
3256 DCHECK(second.IsDoubleStackSlot()) << second;
3257 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3258 Address in2_lo(ESP, second.GetStackIndex());
3259
3260 __ movl(eax, in2_hi);
3261 // eax <- in1.lo * in2.hi
3262 __ imull(eax, in1_lo);
3263 // in1.hi <- in1.hi * in2.lo
3264 __ imull(in1_hi, in2_lo);
3265 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3266 __ addl(in1_hi, eax);
3267 // move in1_lo to eax to prepare for double precision
3268 __ movl(eax, in1_lo);
3269 // edx:eax <- in1.lo * in2.lo
3270 __ mull(in2_lo);
3271 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3272 __ addl(in1_hi, edx);
3273 // in1.lo <- (in1.lo * in2.lo)[31:0];
3274 __ movl(in1_lo, eax);
3275 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003276
3277 break;
3278 }
3279
Calin Juravleb5bfa962014-10-21 18:02:24 +01003280 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003281 DCHECK(first.Equals(locations->Out()));
3282 if (second.IsFpuRegister()) {
3283 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3284 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3285 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003286 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003287 __ mulss(first.AsFpuRegister<XmmRegister>(),
3288 codegen_->LiteralFloatAddress(
3289 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3290 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3291 } else {
3292 DCHECK(second.IsStackSlot());
3293 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3294 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003295 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003296 }
3297
3298 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003299 DCHECK(first.Equals(locations->Out()));
3300 if (second.IsFpuRegister()) {
3301 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3302 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3303 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003304 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003305 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3306 codegen_->LiteralDoubleAddress(
3307 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3308 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3309 } else {
3310 DCHECK(second.IsDoubleStackSlot());
3311 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3312 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003313 break;
3314 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003315
3316 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003317 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003318 }
3319}
3320
Roland Levillain232ade02015-04-20 15:14:36 +01003321void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3322 uint32_t temp_offset,
3323 uint32_t stack_adjustment,
3324 bool is_fp,
3325 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003326 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003327 DCHECK(!is_wide);
3328 if (is_fp) {
3329 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3330 } else {
3331 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3332 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003333 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003334 DCHECK(is_wide);
3335 if (is_fp) {
3336 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3337 } else {
3338 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3339 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003340 } else {
3341 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003342 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003343 Location stack_temp = Location::StackSlot(temp_offset);
3344 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003345 if (is_fp) {
3346 __ flds(Address(ESP, temp_offset));
3347 } else {
3348 __ filds(Address(ESP, temp_offset));
3349 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003350 } else {
3351 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3352 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003353 if (is_fp) {
3354 __ fldl(Address(ESP, temp_offset));
3355 } else {
3356 __ fildl(Address(ESP, temp_offset));
3357 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003358 }
3359 }
3360}
3361
3362void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3363 Primitive::Type type = rem->GetResultType();
3364 bool is_float = type == Primitive::kPrimFloat;
3365 size_t elem_size = Primitive::ComponentSize(type);
3366 LocationSummary* locations = rem->GetLocations();
3367 Location first = locations->InAt(0);
3368 Location second = locations->InAt(1);
3369 Location out = locations->Out();
3370
3371 // Create stack space for 2 elements.
3372 // TODO: enhance register allocator to ask for stack temporaries.
3373 __ subl(ESP, Immediate(2 * elem_size));
3374
3375 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003376 const bool is_wide = !is_float;
3377 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3378 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003379
3380 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003381 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003382 __ Bind(&retry);
3383 __ fprem();
3384
3385 // Move FP status to AX.
3386 __ fstsw();
3387
3388 // And see if the argument reduction is complete. This is signaled by the
3389 // C2 FPU flag bit set to 0.
3390 __ andl(EAX, Immediate(kC2ConditionMask));
3391 __ j(kNotEqual, &retry);
3392
3393 // We have settled on the final value. Retrieve it into an XMM register.
3394 // Store FP top of stack to real stack.
3395 if (is_float) {
3396 __ fsts(Address(ESP, 0));
3397 } else {
3398 __ fstl(Address(ESP, 0));
3399 }
3400
3401 // Pop the 2 items from the FP stack.
3402 __ fucompp();
3403
3404 // Load the value from the stack into an XMM register.
3405 DCHECK(out.IsFpuRegister()) << out;
3406 if (is_float) {
3407 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3408 } else {
3409 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3410 }
3411
3412 // And remove the temporary stack space we allocated.
3413 __ addl(ESP, Immediate(2 * elem_size));
3414}
3415
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003416
3417void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3418 DCHECK(instruction->IsDiv() || instruction->IsRem());
3419
3420 LocationSummary* locations = instruction->GetLocations();
3421 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003422 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003423
3424 Register out_register = locations->Out().AsRegister<Register>();
3425 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003426 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003427
3428 DCHECK(imm == 1 || imm == -1);
3429
3430 if (instruction->IsRem()) {
3431 __ xorl(out_register, out_register);
3432 } else {
3433 __ movl(out_register, input_register);
3434 if (imm == -1) {
3435 __ negl(out_register);
3436 }
3437 }
3438}
3439
3440
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003441void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003442 LocationSummary* locations = instruction->GetLocations();
3443
3444 Register out_register = locations->Out().AsRegister<Register>();
3445 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003446 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003447 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3448 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003449
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003450 Register num = locations->GetTemp(0).AsRegister<Register>();
3451
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003452 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003453 __ testl(input_register, input_register);
3454 __ cmovl(kGreaterEqual, num, input_register);
3455 int shift = CTZ(imm);
3456 __ sarl(num, Immediate(shift));
3457
3458 if (imm < 0) {
3459 __ negl(num);
3460 }
3461
3462 __ movl(out_register, num);
3463}
3464
3465void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3466 DCHECK(instruction->IsDiv() || instruction->IsRem());
3467
3468 LocationSummary* locations = instruction->GetLocations();
3469 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3470
3471 Register eax = locations->InAt(0).AsRegister<Register>();
3472 Register out = locations->Out().AsRegister<Register>();
3473 Register num;
3474 Register edx;
3475
3476 if (instruction->IsDiv()) {
3477 edx = locations->GetTemp(0).AsRegister<Register>();
3478 num = locations->GetTemp(1).AsRegister<Register>();
3479 } else {
3480 edx = locations->Out().AsRegister<Register>();
3481 num = locations->GetTemp(0).AsRegister<Register>();
3482 }
3483
3484 DCHECK_EQ(EAX, eax);
3485 DCHECK_EQ(EDX, edx);
3486 if (instruction->IsDiv()) {
3487 DCHECK_EQ(EAX, out);
3488 } else {
3489 DCHECK_EQ(EDX, out);
3490 }
3491
3492 int64_t magic;
3493 int shift;
3494 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3495
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003496 // Save the numerator.
3497 __ movl(num, eax);
3498
3499 // EAX = magic
3500 __ movl(eax, Immediate(magic));
3501
3502 // EDX:EAX = magic * numerator
3503 __ imull(num);
3504
3505 if (imm > 0 && magic < 0) {
3506 // EDX += num
3507 __ addl(edx, num);
3508 } else if (imm < 0 && magic > 0) {
3509 __ subl(edx, num);
3510 }
3511
3512 // Shift if needed.
3513 if (shift != 0) {
3514 __ sarl(edx, Immediate(shift));
3515 }
3516
3517 // EDX += 1 if EDX < 0
3518 __ movl(eax, edx);
3519 __ shrl(edx, Immediate(31));
3520 __ addl(edx, eax);
3521
3522 if (instruction->IsRem()) {
3523 __ movl(eax, num);
3524 __ imull(edx, Immediate(imm));
3525 __ subl(eax, edx);
3526 __ movl(edx, eax);
3527 } else {
3528 __ movl(eax, edx);
3529 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003530}
3531
Calin Juravlebacfec32014-11-14 15:54:36 +00003532void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3533 DCHECK(instruction->IsDiv() || instruction->IsRem());
3534
3535 LocationSummary* locations = instruction->GetLocations();
3536 Location out = locations->Out();
3537 Location first = locations->InAt(0);
3538 Location second = locations->InAt(1);
3539 bool is_div = instruction->IsDiv();
3540
3541 switch (instruction->GetResultType()) {
3542 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003543 DCHECK_EQ(EAX, first.AsRegister<Register>());
3544 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003545
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003546 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003547 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003548
3549 if (imm == 0) {
3550 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3551 } else if (imm == 1 || imm == -1) {
3552 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003553 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003554 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003555 } else {
3556 DCHECK(imm <= -2 || imm >= 2);
3557 GenerateDivRemWithAnyConstant(instruction);
3558 }
3559 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003560 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3561 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003562 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003563
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003564 Register second_reg = second.AsRegister<Register>();
3565 // 0x80000000/-1 triggers an arithmetic exception!
3566 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3567 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003568
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003569 __ cmpl(second_reg, Immediate(-1));
3570 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003571
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003572 // edx:eax <- sign-extended of eax
3573 __ cdq();
3574 // eax = quotient, edx = remainder
3575 __ idivl(second_reg);
3576 __ Bind(slow_path->GetExitLabel());
3577 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003578 break;
3579 }
3580
3581 case Primitive::kPrimLong: {
3582 InvokeRuntimeCallingConvention calling_convention;
3583 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3584 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3585 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3586 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3587 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3588 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3589
3590 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003591 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003592 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003593 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003594 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003595 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003596 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003597 break;
3598 }
3599
3600 default:
3601 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3602 }
3603}
3604
Calin Juravle7c4954d2014-10-28 16:57:40 +00003605void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003606 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003607 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003608 : LocationSummary::kNoCall;
3609 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3610
Calin Juravle7c4954d2014-10-28 16:57:40 +00003611 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003612 case Primitive::kPrimInt: {
3613 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003614 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003615 locations->SetOut(Location::SameAsFirstInput());
3616 // Intel uses edx:eax as the dividend.
3617 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003618 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3619 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3620 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003621 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003622 locations->AddTemp(Location::RequiresRegister());
3623 }
Calin Juravled0d48522014-11-04 16:40:20 +00003624 break;
3625 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003626 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003627 InvokeRuntimeCallingConvention calling_convention;
3628 locations->SetInAt(0, Location::RegisterPairLocation(
3629 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3630 locations->SetInAt(1, Location::RegisterPairLocation(
3631 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3632 // Runtime helper puts the result in EAX, EDX.
3633 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003634 break;
3635 }
3636 case Primitive::kPrimFloat:
3637 case Primitive::kPrimDouble: {
3638 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003639 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3640 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003641 } else if (div->InputAt(1)->IsConstant()) {
3642 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003643 } else {
3644 locations->SetInAt(1, Location::Any());
3645 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003646 locations->SetOut(Location::SameAsFirstInput());
3647 break;
3648 }
3649
3650 default:
3651 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3652 }
3653}
3654
3655void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3656 LocationSummary* locations = div->GetLocations();
3657 Location first = locations->InAt(0);
3658 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003659
3660 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003661 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003662 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003663 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003664 break;
3665 }
3666
3667 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003668 if (second.IsFpuRegister()) {
3669 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3670 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3671 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003672 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003673 __ divss(first.AsFpuRegister<XmmRegister>(),
3674 codegen_->LiteralFloatAddress(
3675 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3676 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3677 } else {
3678 DCHECK(second.IsStackSlot());
3679 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3680 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003681 break;
3682 }
3683
3684 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003685 if (second.IsFpuRegister()) {
3686 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3687 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3688 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003689 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003690 __ divsd(first.AsFpuRegister<XmmRegister>(),
3691 codegen_->LiteralDoubleAddress(
3692 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3693 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3694 } else {
3695 DCHECK(second.IsDoubleStackSlot());
3696 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3697 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003698 break;
3699 }
3700
3701 default:
3702 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3703 }
3704}
3705
Calin Juravlebacfec32014-11-14 15:54:36 +00003706void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003707 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003708
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003709 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003710 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003711 : LocationSummary::kNoCall;
3712 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003713
Calin Juravled2ec87d2014-12-08 14:24:46 +00003714 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003715 case Primitive::kPrimInt: {
3716 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003717 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003718 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003719 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3720 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3721 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003722 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003723 locations->AddTemp(Location::RequiresRegister());
3724 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003725 break;
3726 }
3727 case Primitive::kPrimLong: {
3728 InvokeRuntimeCallingConvention calling_convention;
3729 locations->SetInAt(0, Location::RegisterPairLocation(
3730 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3731 locations->SetInAt(1, Location::RegisterPairLocation(
3732 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3733 // Runtime helper puts the result in EAX, EDX.
3734 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3735 break;
3736 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003737 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003738 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003739 locations->SetInAt(0, Location::Any());
3740 locations->SetInAt(1, Location::Any());
3741 locations->SetOut(Location::RequiresFpuRegister());
3742 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003743 break;
3744 }
3745
3746 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003747 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003748 }
3749}
3750
3751void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3752 Primitive::Type type = rem->GetResultType();
3753 switch (type) {
3754 case Primitive::kPrimInt:
3755 case Primitive::kPrimLong: {
3756 GenerateDivRemIntegral(rem);
3757 break;
3758 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003759 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003760 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003761 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003762 break;
3763 }
3764 default:
3765 LOG(FATAL) << "Unexpected rem type " << type;
3766 }
3767}
3768
Calin Juravled0d48522014-11-04 16:40:20 +00003769void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003770 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003771 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003772 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003773 case Primitive::kPrimByte:
3774 case Primitive::kPrimChar:
3775 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003776 case Primitive::kPrimInt: {
3777 locations->SetInAt(0, Location::Any());
3778 break;
3779 }
3780 case Primitive::kPrimLong: {
3781 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3782 if (!instruction->IsConstant()) {
3783 locations->AddTemp(Location::RequiresRegister());
3784 }
3785 break;
3786 }
3787 default:
3788 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3789 }
Calin Juravled0d48522014-11-04 16:40:20 +00003790}
3791
3792void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003793 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003794 codegen_->AddSlowPath(slow_path);
3795
3796 LocationSummary* locations = instruction->GetLocations();
3797 Location value = locations->InAt(0);
3798
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003799 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003800 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003801 case Primitive::kPrimByte:
3802 case Primitive::kPrimChar:
3803 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003804 case Primitive::kPrimInt: {
3805 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003806 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003807 __ j(kEqual, slow_path->GetEntryLabel());
3808 } else if (value.IsStackSlot()) {
3809 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3810 __ j(kEqual, slow_path->GetEntryLabel());
3811 } else {
3812 DCHECK(value.IsConstant()) << value;
3813 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003814 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003815 }
3816 }
3817 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003818 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003819 case Primitive::kPrimLong: {
3820 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003821 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003822 __ movl(temp, value.AsRegisterPairLow<Register>());
3823 __ orl(temp, value.AsRegisterPairHigh<Register>());
3824 __ j(kEqual, slow_path->GetEntryLabel());
3825 } else {
3826 DCHECK(value.IsConstant()) << value;
3827 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3828 __ jmp(slow_path->GetEntryLabel());
3829 }
3830 }
3831 break;
3832 }
3833 default:
3834 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003835 }
Calin Juravled0d48522014-11-04 16:40:20 +00003836}
3837
Calin Juravle9aec02f2014-11-18 23:06:35 +00003838void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3839 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3840
3841 LocationSummary* locations =
3842 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3843
3844 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003845 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003846 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003847 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003848 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003849 // The shift count needs to be in CL or a constant.
3850 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003851 locations->SetOut(Location::SameAsFirstInput());
3852 break;
3853 }
3854 default:
3855 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3856 }
3857}
3858
3859void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3860 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3861
3862 LocationSummary* locations = op->GetLocations();
3863 Location first = locations->InAt(0);
3864 Location second = locations->InAt(1);
3865 DCHECK(first.Equals(locations->Out()));
3866
3867 switch (op->GetResultType()) {
3868 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003869 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003870 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003871 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003872 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003873 DCHECK_EQ(ECX, second_reg);
3874 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003875 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003876 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003877 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003878 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003879 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003880 }
3881 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003882 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003883 if (shift == 0) {
3884 return;
3885 }
3886 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003887 if (op->IsShl()) {
3888 __ shll(first_reg, imm);
3889 } else if (op->IsShr()) {
3890 __ sarl(first_reg, imm);
3891 } else {
3892 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003893 }
3894 }
3895 break;
3896 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003897 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003898 if (second.IsRegister()) {
3899 Register second_reg = second.AsRegister<Register>();
3900 DCHECK_EQ(ECX, second_reg);
3901 if (op->IsShl()) {
3902 GenerateShlLong(first, second_reg);
3903 } else if (op->IsShr()) {
3904 GenerateShrLong(first, second_reg);
3905 } else {
3906 GenerateUShrLong(first, second_reg);
3907 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003908 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003909 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003910 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003911 // Nothing to do if the shift is 0, as the input is already the output.
3912 if (shift != 0) {
3913 if (op->IsShl()) {
3914 GenerateShlLong(first, shift);
3915 } else if (op->IsShr()) {
3916 GenerateShrLong(first, shift);
3917 } else {
3918 GenerateUShrLong(first, shift);
3919 }
3920 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003921 }
3922 break;
3923 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003924 default:
3925 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3926 }
3927}
3928
Mark P Mendell73945692015-04-29 14:56:17 +00003929void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3930 Register low = loc.AsRegisterPairLow<Register>();
3931 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003932 if (shift == 1) {
3933 // This is just an addition.
3934 __ addl(low, low);
3935 __ adcl(high, high);
3936 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003937 // Shift by 32 is easy. High gets low, and low gets 0.
3938 codegen_->EmitParallelMoves(
3939 loc.ToLow(),
3940 loc.ToHigh(),
3941 Primitive::kPrimInt,
3942 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3943 loc.ToLow(),
3944 Primitive::kPrimInt);
3945 } else if (shift > 32) {
3946 // Low part becomes 0. High part is low part << (shift-32).
3947 __ movl(high, low);
3948 __ shll(high, Immediate(shift - 32));
3949 __ xorl(low, low);
3950 } else {
3951 // Between 1 and 31.
3952 __ shld(high, low, Immediate(shift));
3953 __ shll(low, Immediate(shift));
3954 }
3955}
3956
Calin Juravle9aec02f2014-11-18 23:06:35 +00003957void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003958 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003959 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3960 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3961 __ testl(shifter, Immediate(32));
3962 __ j(kEqual, &done);
3963 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3964 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3965 __ Bind(&done);
3966}
3967
Mark P Mendell73945692015-04-29 14:56:17 +00003968void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3969 Register low = loc.AsRegisterPairLow<Register>();
3970 Register high = loc.AsRegisterPairHigh<Register>();
3971 if (shift == 32) {
3972 // Need to copy the sign.
3973 DCHECK_NE(low, high);
3974 __ movl(low, high);
3975 __ sarl(high, Immediate(31));
3976 } else if (shift > 32) {
3977 DCHECK_NE(low, high);
3978 // High part becomes sign. Low part is shifted by shift - 32.
3979 __ movl(low, high);
3980 __ sarl(high, Immediate(31));
3981 __ sarl(low, Immediate(shift - 32));
3982 } else {
3983 // Between 1 and 31.
3984 __ shrd(low, high, Immediate(shift));
3985 __ sarl(high, Immediate(shift));
3986 }
3987}
3988
Calin Juravle9aec02f2014-11-18 23:06:35 +00003989void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003990 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003991 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3992 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3993 __ testl(shifter, Immediate(32));
3994 __ j(kEqual, &done);
3995 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3996 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3997 __ Bind(&done);
3998}
3999
Mark P Mendell73945692015-04-29 14:56:17 +00004000void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4001 Register low = loc.AsRegisterPairLow<Register>();
4002 Register high = loc.AsRegisterPairHigh<Register>();
4003 if (shift == 32) {
4004 // Shift by 32 is easy. Low gets high, and high gets 0.
4005 codegen_->EmitParallelMoves(
4006 loc.ToHigh(),
4007 loc.ToLow(),
4008 Primitive::kPrimInt,
4009 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4010 loc.ToHigh(),
4011 Primitive::kPrimInt);
4012 } else if (shift > 32) {
4013 // Low part is high >> (shift - 32). High part becomes 0.
4014 __ movl(low, high);
4015 __ shrl(low, Immediate(shift - 32));
4016 __ xorl(high, high);
4017 } else {
4018 // Between 1 and 31.
4019 __ shrd(low, high, Immediate(shift));
4020 __ shrl(high, Immediate(shift));
4021 }
4022}
4023
Calin Juravle9aec02f2014-11-18 23:06:35 +00004024void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004025 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004026 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4027 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4028 __ testl(shifter, Immediate(32));
4029 __ j(kEqual, &done);
4030 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4031 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4032 __ Bind(&done);
4033}
4034
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004035void LocationsBuilderX86::VisitRor(HRor* ror) {
4036 LocationSummary* locations =
4037 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4038
4039 switch (ror->GetResultType()) {
4040 case Primitive::kPrimLong:
4041 // Add the temporary needed.
4042 locations->AddTemp(Location::RequiresRegister());
4043 FALLTHROUGH_INTENDED;
4044 case Primitive::kPrimInt:
4045 locations->SetInAt(0, Location::RequiresRegister());
4046 // The shift count needs to be in CL (unless it is a constant).
4047 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4048 locations->SetOut(Location::SameAsFirstInput());
4049 break;
4050 default:
4051 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4052 UNREACHABLE();
4053 }
4054}
4055
4056void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4057 LocationSummary* locations = ror->GetLocations();
4058 Location first = locations->InAt(0);
4059 Location second = locations->InAt(1);
4060
4061 if (ror->GetResultType() == Primitive::kPrimInt) {
4062 Register first_reg = first.AsRegister<Register>();
4063 if (second.IsRegister()) {
4064 Register second_reg = second.AsRegister<Register>();
4065 __ rorl(first_reg, second_reg);
4066 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004067 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004068 __ rorl(first_reg, imm);
4069 }
4070 return;
4071 }
4072
4073 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4074 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4075 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4076 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4077 if (second.IsRegister()) {
4078 Register second_reg = second.AsRegister<Register>();
4079 DCHECK_EQ(second_reg, ECX);
4080 __ movl(temp_reg, first_reg_hi);
4081 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4082 __ shrd(first_reg_lo, temp_reg, second_reg);
4083 __ movl(temp_reg, first_reg_hi);
4084 __ testl(second_reg, Immediate(32));
4085 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4086 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4087 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004088 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004089 if (shift_amt == 0) {
4090 // Already fine.
4091 return;
4092 }
4093 if (shift_amt == 32) {
4094 // Just swap.
4095 __ movl(temp_reg, first_reg_lo);
4096 __ movl(first_reg_lo, first_reg_hi);
4097 __ movl(first_reg_hi, temp_reg);
4098 return;
4099 }
4100
4101 Immediate imm(shift_amt);
4102 // Save the constents of the low value.
4103 __ movl(temp_reg, first_reg_lo);
4104
4105 // Shift right into low, feeding bits from high.
4106 __ shrd(first_reg_lo, first_reg_hi, imm);
4107
4108 // Shift right into high, feeding bits from the original low.
4109 __ shrd(first_reg_hi, temp_reg, imm);
4110
4111 // Swap if needed.
4112 if (shift_amt > 32) {
4113 __ movl(temp_reg, first_reg_lo);
4114 __ movl(first_reg_lo, first_reg_hi);
4115 __ movl(first_reg_hi, temp_reg);
4116 }
4117 }
4118}
4119
Calin Juravle9aec02f2014-11-18 23:06:35 +00004120void LocationsBuilderX86::VisitShl(HShl* shl) {
4121 HandleShift(shl);
4122}
4123
4124void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4125 HandleShift(shl);
4126}
4127
4128void LocationsBuilderX86::VisitShr(HShr* shr) {
4129 HandleShift(shr);
4130}
4131
4132void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4133 HandleShift(shr);
4134}
4135
4136void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4137 HandleShift(ushr);
4138}
4139
4140void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4141 HandleShift(ushr);
4142}
4143
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004144void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004145 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004146 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004147 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004148 if (instruction->IsStringAlloc()) {
4149 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4150 } else {
4151 InvokeRuntimeCallingConvention calling_convention;
4152 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Hiroshi Yamauchif7aaacd2017-01-12 02:58:38 +00004153 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
David Brazdil6de19382016-01-08 17:37:10 +00004154 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004155}
4156
4157void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004158 // Note: if heap poisoning is enabled, the entry point takes cares
4159 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004160 if (instruction->IsStringAlloc()) {
4161 // String is allocated through StringFactory. Call NewEmptyString entry point.
4162 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004163 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004164 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4165 __ call(Address(temp, code_offset.Int32Value()));
4166 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4167 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004168 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Hiroshi Yamauchif7aaacd2017-01-12 02:58:38 +00004169 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
David Brazdil6de19382016-01-08 17:37:10 +00004170 DCHECK(!codegen_->IsLeafMethod());
4171 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004172}
4173
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004174void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4175 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004176 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004177 locations->SetOut(Location::RegisterLocation(EAX));
4178 InvokeRuntimeCallingConvention calling_convention;
4179 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004180 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004181 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004182}
4183
4184void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4185 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004186 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex().index_));
Roland Levillain4d027112015-07-01 15:41:14 +01004187 // Note: if heap poisoning is enabled, the entry point takes cares
4188 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004189 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004190 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004191 DCHECK(!codegen_->IsLeafMethod());
4192}
4193
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004194void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004195 LocationSummary* locations =
4196 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004197 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4198 if (location.IsStackSlot()) {
4199 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4200 } else if (location.IsDoubleStackSlot()) {
4201 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004202 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004203 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004204}
4205
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004206void InstructionCodeGeneratorX86::VisitParameterValue(
4207 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4208}
4209
4210void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4211 LocationSummary* locations =
4212 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4213 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4214}
4215
4216void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004217}
4218
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004219void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4220 LocationSummary* locations =
4221 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4222 locations->SetInAt(0, Location::RequiresRegister());
4223 locations->SetOut(Location::RequiresRegister());
4224}
4225
4226void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4227 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004228 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004229 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004230 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004231 __ movl(locations->Out().AsRegister<Register>(),
4232 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004233 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004234 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004235 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004236 __ movl(locations->Out().AsRegister<Register>(),
4237 Address(locations->InAt(0).AsRegister<Register>(),
4238 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4239 // temp = temp->GetImtEntryAt(method_offset);
4240 __ movl(locations->Out().AsRegister<Register>(),
4241 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004242 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004243}
4244
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004245void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004246 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004247 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004248 locations->SetInAt(0, Location::RequiresRegister());
4249 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004250}
4251
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004252void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4253 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004254 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004255 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004256 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004257 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004258 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004259 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004260 break;
4261
4262 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004263 __ notl(out.AsRegisterPairLow<Register>());
4264 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004265 break;
4266
4267 default:
4268 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4269 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004270}
4271
David Brazdil66d126e2015-04-03 16:02:44 +01004272void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4273 LocationSummary* locations =
4274 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4275 locations->SetInAt(0, Location::RequiresRegister());
4276 locations->SetOut(Location::SameAsFirstInput());
4277}
4278
4279void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004280 LocationSummary* locations = bool_not->GetLocations();
4281 Location in = locations->InAt(0);
4282 Location out = locations->Out();
4283 DCHECK(in.Equals(out));
4284 __ xorl(out.AsRegister<Register>(), Immediate(1));
4285}
4286
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004287void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004288 LocationSummary* locations =
4289 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004290 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004291 case Primitive::kPrimBoolean:
4292 case Primitive::kPrimByte:
4293 case Primitive::kPrimShort:
4294 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004295 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004296 case Primitive::kPrimLong: {
4297 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004298 locations->SetInAt(1, Location::Any());
4299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4300 break;
4301 }
4302 case Primitive::kPrimFloat:
4303 case Primitive::kPrimDouble: {
4304 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004305 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4306 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4307 } else if (compare->InputAt(1)->IsConstant()) {
4308 locations->SetInAt(1, Location::RequiresFpuRegister());
4309 } else {
4310 locations->SetInAt(1, Location::Any());
4311 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004312 locations->SetOut(Location::RequiresRegister());
4313 break;
4314 }
4315 default:
4316 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4317 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004318}
4319
4320void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004321 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004322 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004323 Location left = locations->InAt(0);
4324 Location right = locations->InAt(1);
4325
Mark Mendell0c9497d2015-08-21 09:30:05 -04004326 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004327 Condition less_cond = kLess;
4328
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004329 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004330 case Primitive::kPrimBoolean:
4331 case Primitive::kPrimByte:
4332 case Primitive::kPrimShort:
4333 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004334 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004335 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004336 break;
4337 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004338 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004339 Register left_low = left.AsRegisterPairLow<Register>();
4340 Register left_high = left.AsRegisterPairHigh<Register>();
4341 int32_t val_low = 0;
4342 int32_t val_high = 0;
4343 bool right_is_const = false;
4344
4345 if (right.IsConstant()) {
4346 DCHECK(right.GetConstant()->IsLongConstant());
4347 right_is_const = true;
4348 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4349 val_low = Low32Bits(val);
4350 val_high = High32Bits(val);
4351 }
4352
Calin Juravleddb7df22014-11-25 20:56:51 +00004353 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004354 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004355 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004356 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004357 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004358 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004359 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004360 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004361 __ j(kLess, &less); // Signed compare.
4362 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004363 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004364 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004365 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004366 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004367 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004368 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004369 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004370 }
Aart Bika19616e2016-02-01 18:57:58 -08004371 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004372 break;
4373 }
4374 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004375 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004376 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004377 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004378 break;
4379 }
4380 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004381 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004382 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004383 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004384 break;
4385 }
4386 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004387 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004388 }
Aart Bika19616e2016-02-01 18:57:58 -08004389
Calin Juravleddb7df22014-11-25 20:56:51 +00004390 __ movl(out, Immediate(0));
4391 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004392 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004393
4394 __ Bind(&greater);
4395 __ movl(out, Immediate(1));
4396 __ jmp(&done);
4397
4398 __ Bind(&less);
4399 __ movl(out, Immediate(-1));
4400
4401 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004402}
4403
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004404void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004405 LocationSummary* locations =
4406 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004407 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004408 locations->SetInAt(i, Location::Any());
4409 }
4410 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004411}
4412
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004413void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004414 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004415}
4416
Roland Levillain7c1559a2015-12-15 10:55:36 +00004417void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004418 /*
4419 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4420 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4421 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4422 */
4423 switch (kind) {
4424 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004425 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004426 break;
4427 }
4428 case MemBarrierKind::kAnyStore:
4429 case MemBarrierKind::kLoadAny:
4430 case MemBarrierKind::kStoreStore: {
4431 // nop
4432 break;
4433 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004434 case MemBarrierKind::kNTStoreStore:
4435 // Non-Temporal Store/Store needs an explicit fence.
4436 MemoryFence(/* non-temporal */ true);
4437 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004438 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004439}
4440
Vladimir Markodc151b22015-10-15 18:02:30 +01004441HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4442 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004443 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004444 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4445
4446 // We disable pc-relative load when there is an irreducible loop, as the optimization
4447 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004448 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4449 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004450 if (GetGraph()->HasIrreducibleLoops() &&
4451 (dispatch_info.method_load_kind ==
4452 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4453 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4454 }
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00004455 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004456}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004457
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004458Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4459 Register temp) {
4460 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004461 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004462 if (!invoke->GetLocations()->Intrinsified()) {
4463 return location.AsRegister<Register>();
4464 }
4465 // For intrinsics we allow any location, so it may be on the stack.
4466 if (!location.IsRegister()) {
4467 __ movl(temp, Address(ESP, location.GetStackIndex()));
4468 return temp;
4469 }
4470 // For register locations, check if the register was saved. If so, get it from the stack.
4471 // Note: There is a chance that the register was saved but not overwritten, so we could
4472 // save one load. However, since this is just an intrinsic slow path we prefer this
4473 // simple and more robust approach rather that trying to determine if that's the case.
4474 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004475 if (slow_path != nullptr) {
4476 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4477 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4478 __ movl(temp, Address(ESP, stack_offset));
4479 return temp;
4480 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004481 }
4482 return location.AsRegister<Register>();
4483}
4484
Serguei Katkov288c7a82016-05-16 11:53:15 +06004485Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4486 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004487 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4488 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004489 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004490 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004491 uint32_t offset =
4492 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4493 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004494 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004495 }
Vladimir Marko58155012015-08-19 12:49:41 +00004496 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004497 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004498 break;
4499 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4500 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4501 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004502 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4503 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4504 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004505 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004506 // Bind a new fixup label at the end of the "movl" insn.
4507 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004508 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004509 break;
4510 }
Vladimir Marko58155012015-08-19 12:49:41 +00004511 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004512 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004513 Register method_reg;
4514 Register reg = temp.AsRegister<Register>();
4515 if (current_method.IsRegister()) {
4516 method_reg = current_method.AsRegister<Register>();
4517 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004518 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004519 DCHECK(!current_method.IsValid());
4520 method_reg = reg;
4521 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4522 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004523 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004524 __ movl(reg, Address(method_reg,
4525 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004526 // temp = temp[index_in_cache];
4527 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4528 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004529 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4530 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004531 }
Vladimir Marko58155012015-08-19 12:49:41 +00004532 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004533 return callee_method;
4534}
4535
4536void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4537 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004538
4539 switch (invoke->GetCodePtrLocation()) {
4540 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4541 __ call(GetFrameEntryLabel());
4542 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004543 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4544 // (callee_method + offset_of_quick_compiled_code)()
4545 __ call(Address(callee_method.AsRegister<Register>(),
4546 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004547 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004548 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004549 }
4550
4551 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004552}
4553
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004554void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4555 Register temp = temp_in.AsRegister<Register>();
4556 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4557 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004558
4559 // Use the calling convention instead of the location of the receiver, as
4560 // intrinsics may have put the receiver in a different register. In the intrinsics
4561 // slow path, the arguments have been moved to the right place, so here we are
4562 // guaranteed that the receiver is the first register of the calling convention.
4563 InvokeDexCallingConvention calling_convention;
4564 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004565 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004566 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004567 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004568 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004569 // Instead of simply (possibly) unpoisoning `temp` here, we should
4570 // emit a read barrier for the previous class reference load.
4571 // However this is not required in practice, as this is an
4572 // intermediate/temporary reference and because the current
4573 // concurrent copying collector keeps the from-space memory
4574 // intact/accessible until the end of the marking phase (the
4575 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004576 __ MaybeUnpoisonHeapReference(temp);
4577 // temp = temp->GetMethodAt(method_offset);
4578 __ movl(temp, Address(temp, method_offset));
4579 // call temp->GetEntryPoint();
4580 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004581 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004582}
4583
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004584void CodeGeneratorX86::RecordSimplePatch() {
4585 if (GetCompilerOptions().GetIncludePatchInformation()) {
4586 simple_patches_.emplace_back();
4587 __ Bind(&simple_patches_.back());
4588 }
4589}
4590
Vladimir Markoaad75c62016-10-03 08:46:48 +00004591void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4592 DCHECK(GetCompilerOptions().IsBootImage());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004593 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004594 __ Bind(&string_patches_.back().label);
4595}
4596
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004597void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004598 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004599 __ Bind(&type_patches_.back().label);
4600}
4601
Vladimir Markoaad75c62016-10-03 08:46:48 +00004602Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4603 DCHECK(!GetCompilerOptions().IsBootImage());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004604 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004605 return &string_patches_.back().label;
4606}
4607
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004608Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4609 uint32_t element_offset) {
4610 // Add the patch entry and bind its label at the end of the instruction.
4611 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4612 return &pc_relative_dex_cache_patches_.back().label;
4613}
4614
Vladimir Markoaad75c62016-10-03 08:46:48 +00004615// The label points to the end of the "movl" or another instruction but the literal offset
4616// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4617constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4618
4619template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4620inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
4621 const ArenaDeque<PatchInfo<Label>>& infos,
4622 ArenaVector<LinkerPatch>* linker_patches) {
4623 for (const PatchInfo<Label>& info : infos) {
4624 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4625 linker_patches->push_back(
4626 Factory(literal_offset, &info.dex_file, GetMethodAddressOffset(), info.index));
4627 }
4628}
4629
Vladimir Marko58155012015-08-19 12:49:41 +00004630void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4631 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004632 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004633 pc_relative_dex_cache_patches_.size() +
4634 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004635 string_patches_.size() +
4636 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004637 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004638 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4639 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004640 for (const Label& label : simple_patches_) {
4641 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4642 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4643 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004644 if (!GetCompilerOptions().IsBootImage()) {
4645 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4646 } else if (GetCompilerOptions().GetCompilePic()) {
4647 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004648 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004649 for (const PatchInfo<Label>& info : string_patches_) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004650 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004651 linker_patches->push_back(
4652 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004653 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004654 }
4655 if (GetCompilerOptions().GetCompilePic()) {
4656 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
4657 } else {
4658 for (const PatchInfo<Label>& info : type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004659 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004660 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004661 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004662 }
Vladimir Marko58155012015-08-19 12:49:41 +00004663}
4664
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004665void CodeGeneratorX86::MarkGCCard(Register temp,
4666 Register card,
4667 Register object,
4668 Register value,
4669 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004670 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004671 if (value_can_be_null) {
4672 __ testl(value, value);
4673 __ j(kEqual, &is_null);
4674 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004675 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004676 __ movl(temp, object);
4677 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004678 __ movb(Address(temp, card, TIMES_1, 0),
4679 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004680 if (value_can_be_null) {
4681 __ Bind(&is_null);
4682 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004683}
4684
Calin Juravle52c48962014-12-16 17:02:57 +00004685void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4686 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004687
4688 bool object_field_get_with_read_barrier =
4689 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004690 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004691 new (GetGraph()->GetArena()) LocationSummary(instruction,
4692 kEmitCompilerReadBarrier ?
4693 LocationSummary::kCallOnSlowPath :
4694 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004695 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004696 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004697 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004698 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004699
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004700 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4701 locations->SetOut(Location::RequiresFpuRegister());
4702 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004703 // The output overlaps in case of long: we don't want the low move
4704 // to overwrite the object's location. Likewise, in the case of
4705 // an object field get with read barriers enabled, we do not want
4706 // the move to overwrite the object's location, as we need it to emit
4707 // the read barrier.
4708 locations->SetOut(
4709 Location::RequiresRegister(),
4710 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4711 Location::kOutputOverlap :
4712 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004713 }
Calin Juravle52c48962014-12-16 17:02:57 +00004714
4715 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4716 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004717 // So we use an XMM register as a temp to achieve atomicity (first
4718 // load the temp into the XMM and then copy the XMM into the
4719 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004720 locations->AddTemp(Location::RequiresFpuRegister());
4721 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004722}
4723
Calin Juravle52c48962014-12-16 17:02:57 +00004724void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4725 const FieldInfo& field_info) {
4726 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004727
Calin Juravle52c48962014-12-16 17:02:57 +00004728 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004729 Location base_loc = locations->InAt(0);
4730 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004731 Location out = locations->Out();
4732 bool is_volatile = field_info.IsVolatile();
4733 Primitive::Type field_type = field_info.GetFieldType();
4734 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4735
4736 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004737 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004738 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004739 break;
4740 }
4741
4742 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004743 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004744 break;
4745 }
4746
4747 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004748 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004749 break;
4750 }
4751
4752 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004753 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004754 break;
4755 }
4756
4757 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004758 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004759 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004760
4761 case Primitive::kPrimNot: {
4762 // /* HeapReference<Object> */ out = *(base + offset)
4763 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004764 // Note that a potential implicit null check is handled in this
4765 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4766 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004767 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004768 if (is_volatile) {
4769 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4770 }
4771 } else {
4772 __ movl(out.AsRegister<Register>(), Address(base, offset));
4773 codegen_->MaybeRecordImplicitNullCheck(instruction);
4774 if (is_volatile) {
4775 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4776 }
4777 // If read barriers are enabled, emit read barriers other than
4778 // Baker's using a slow path (and also unpoison the loaded
4779 // reference, if heap poisoning is enabled).
4780 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4781 }
4782 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004783 }
4784
4785 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004786 if (is_volatile) {
4787 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4788 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004789 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004790 __ movd(out.AsRegisterPairLow<Register>(), temp);
4791 __ psrlq(temp, Immediate(32));
4792 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4793 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004794 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004795 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004796 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004797 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4798 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004799 break;
4800 }
4801
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004802 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004803 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004804 break;
4805 }
4806
4807 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004808 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004809 break;
4810 }
4811
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004812 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004813 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004814 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004815 }
Calin Juravle52c48962014-12-16 17:02:57 +00004816
Roland Levillain7c1559a2015-12-15 10:55:36 +00004817 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4818 // Potential implicit null checks, in the case of reference or
4819 // long fields, are handled in the previous switch statement.
4820 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004821 codegen_->MaybeRecordImplicitNullCheck(instruction);
4822 }
4823
Calin Juravle52c48962014-12-16 17:02:57 +00004824 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004825 if (field_type == Primitive::kPrimNot) {
4826 // Memory barriers, in the case of references, are also handled
4827 // in the previous switch statement.
4828 } else {
4829 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4830 }
Roland Levillain4d027112015-07-01 15:41:14 +01004831 }
Calin Juravle52c48962014-12-16 17:02:57 +00004832}
4833
4834void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4835 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4836
4837 LocationSummary* locations =
4838 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4839 locations->SetInAt(0, Location::RequiresRegister());
4840 bool is_volatile = field_info.IsVolatile();
4841 Primitive::Type field_type = field_info.GetFieldType();
4842 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4843 || (field_type == Primitive::kPrimByte);
4844
4845 // The register allocator does not support multiple
4846 // inputs that die at entry with one in a specific register.
4847 if (is_byte_type) {
4848 // Ensure the value is in a byte register.
4849 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004850 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004851 if (is_volatile && field_type == Primitive::kPrimDouble) {
4852 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4853 locations->SetInAt(1, Location::RequiresFpuRegister());
4854 } else {
4855 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4856 }
4857 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4858 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004859 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004860
Calin Juravle52c48962014-12-16 17:02:57 +00004861 // 64bits value can be atomically written to an address with movsd and an XMM register.
4862 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4863 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4864 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4865 // isolated cases when we need this it isn't worth adding the extra complexity.
4866 locations->AddTemp(Location::RequiresFpuRegister());
4867 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004868 } else {
4869 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4870
4871 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4872 // Temporary registers for the write barrier.
4873 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4874 // Ensure the card is in a byte register.
4875 locations->AddTemp(Location::RegisterLocation(ECX));
4876 }
Calin Juravle52c48962014-12-16 17:02:57 +00004877 }
4878}
4879
4880void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004881 const FieldInfo& field_info,
4882 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004883 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4884
4885 LocationSummary* locations = instruction->GetLocations();
4886 Register base = locations->InAt(0).AsRegister<Register>();
4887 Location value = locations->InAt(1);
4888 bool is_volatile = field_info.IsVolatile();
4889 Primitive::Type field_type = field_info.GetFieldType();
4890 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004891 bool needs_write_barrier =
4892 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004893
4894 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004895 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004896 }
4897
Mark Mendell81489372015-11-04 11:30:41 -05004898 bool maybe_record_implicit_null_check_done = false;
4899
Calin Juravle52c48962014-12-16 17:02:57 +00004900 switch (field_type) {
4901 case Primitive::kPrimBoolean:
4902 case Primitive::kPrimByte: {
4903 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4904 break;
4905 }
4906
4907 case Primitive::kPrimShort:
4908 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004909 if (value.IsConstant()) {
4910 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4911 __ movw(Address(base, offset), Immediate(v));
4912 } else {
4913 __ movw(Address(base, offset), value.AsRegister<Register>());
4914 }
Calin Juravle52c48962014-12-16 17:02:57 +00004915 break;
4916 }
4917
4918 case Primitive::kPrimInt:
4919 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004920 if (kPoisonHeapReferences && needs_write_barrier) {
4921 // Note that in the case where `value` is a null reference,
4922 // we do not enter this block, as the reference does not
4923 // need poisoning.
4924 DCHECK_EQ(field_type, Primitive::kPrimNot);
4925 Register temp = locations->GetTemp(0).AsRegister<Register>();
4926 __ movl(temp, value.AsRegister<Register>());
4927 __ PoisonHeapReference(temp);
4928 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004929 } else if (value.IsConstant()) {
4930 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4931 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004932 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004933 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004934 __ movl(Address(base, offset), value.AsRegister<Register>());
4935 }
Calin Juravle52c48962014-12-16 17:02:57 +00004936 break;
4937 }
4938
4939 case Primitive::kPrimLong: {
4940 if (is_volatile) {
4941 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4942 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4943 __ movd(temp1, value.AsRegisterPairLow<Register>());
4944 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4945 __ punpckldq(temp1, temp2);
4946 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004947 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004948 } else if (value.IsConstant()) {
4949 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4950 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4951 codegen_->MaybeRecordImplicitNullCheck(instruction);
4952 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004953 } else {
4954 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004955 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004956 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4957 }
Mark Mendell81489372015-11-04 11:30:41 -05004958 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004959 break;
4960 }
4961
4962 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004963 if (value.IsConstant()) {
4964 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4965 __ movl(Address(base, offset), Immediate(v));
4966 } else {
4967 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4968 }
Calin Juravle52c48962014-12-16 17:02:57 +00004969 break;
4970 }
4971
4972 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004973 if (value.IsConstant()) {
4974 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4975 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4976 codegen_->MaybeRecordImplicitNullCheck(instruction);
4977 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4978 maybe_record_implicit_null_check_done = true;
4979 } else {
4980 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4981 }
Calin Juravle52c48962014-12-16 17:02:57 +00004982 break;
4983 }
4984
4985 case Primitive::kPrimVoid:
4986 LOG(FATAL) << "Unreachable type " << field_type;
4987 UNREACHABLE();
4988 }
4989
Mark Mendell81489372015-11-04 11:30:41 -05004990 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004991 codegen_->MaybeRecordImplicitNullCheck(instruction);
4992 }
4993
Roland Levillain4d027112015-07-01 15:41:14 +01004994 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004995 Register temp = locations->GetTemp(0).AsRegister<Register>();
4996 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004997 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004998 }
4999
Calin Juravle52c48962014-12-16 17:02:57 +00005000 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005001 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005002 }
5003}
5004
5005void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5006 HandleFieldGet(instruction, instruction->GetFieldInfo());
5007}
5008
5009void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5010 HandleFieldGet(instruction, instruction->GetFieldInfo());
5011}
5012
5013void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5014 HandleFieldSet(instruction, instruction->GetFieldInfo());
5015}
5016
5017void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005018 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005019}
5020
5021void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5022 HandleFieldSet(instruction, instruction->GetFieldInfo());
5023}
5024
5025void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005026 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005027}
5028
5029void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5030 HandleFieldGet(instruction, instruction->GetFieldInfo());
5031}
5032
5033void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5034 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005035}
5036
Calin Juravlee460d1d2015-09-29 04:52:17 +01005037void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5038 HUnresolvedInstanceFieldGet* instruction) {
5039 FieldAccessCallingConventionX86 calling_convention;
5040 codegen_->CreateUnresolvedFieldLocationSummary(
5041 instruction, instruction->GetFieldType(), calling_convention);
5042}
5043
5044void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5045 HUnresolvedInstanceFieldGet* instruction) {
5046 FieldAccessCallingConventionX86 calling_convention;
5047 codegen_->GenerateUnresolvedFieldAccess(instruction,
5048 instruction->GetFieldType(),
5049 instruction->GetFieldIndex(),
5050 instruction->GetDexPc(),
5051 calling_convention);
5052}
5053
5054void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5055 HUnresolvedInstanceFieldSet* instruction) {
5056 FieldAccessCallingConventionX86 calling_convention;
5057 codegen_->CreateUnresolvedFieldLocationSummary(
5058 instruction, instruction->GetFieldType(), calling_convention);
5059}
5060
5061void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5062 HUnresolvedInstanceFieldSet* instruction) {
5063 FieldAccessCallingConventionX86 calling_convention;
5064 codegen_->GenerateUnresolvedFieldAccess(instruction,
5065 instruction->GetFieldType(),
5066 instruction->GetFieldIndex(),
5067 instruction->GetDexPc(),
5068 calling_convention);
5069}
5070
5071void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5072 HUnresolvedStaticFieldGet* instruction) {
5073 FieldAccessCallingConventionX86 calling_convention;
5074 codegen_->CreateUnresolvedFieldLocationSummary(
5075 instruction, instruction->GetFieldType(), calling_convention);
5076}
5077
5078void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5079 HUnresolvedStaticFieldGet* instruction) {
5080 FieldAccessCallingConventionX86 calling_convention;
5081 codegen_->GenerateUnresolvedFieldAccess(instruction,
5082 instruction->GetFieldType(),
5083 instruction->GetFieldIndex(),
5084 instruction->GetDexPc(),
5085 calling_convention);
5086}
5087
5088void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5089 HUnresolvedStaticFieldSet* instruction) {
5090 FieldAccessCallingConventionX86 calling_convention;
5091 codegen_->CreateUnresolvedFieldLocationSummary(
5092 instruction, instruction->GetFieldType(), calling_convention);
5093}
5094
5095void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5096 HUnresolvedStaticFieldSet* instruction) {
5097 FieldAccessCallingConventionX86 calling_convention;
5098 codegen_->GenerateUnresolvedFieldAccess(instruction,
5099 instruction->GetFieldType(),
5100 instruction->GetFieldIndex(),
5101 instruction->GetDexPc(),
5102 calling_convention);
5103}
5104
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005105void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005106 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5107 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5108 ? Location::RequiresRegister()
5109 : Location::Any();
5110 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005111}
5112
Calin Juravle2ae48182016-03-16 14:05:09 +00005113void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5114 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005115 return;
5116 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005117 LocationSummary* locations = instruction->GetLocations();
5118 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005119
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005120 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005121 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005122}
5123
Calin Juravle2ae48182016-03-16 14:05:09 +00005124void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005125 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005126 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005127
5128 LocationSummary* locations = instruction->GetLocations();
5129 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005130
5131 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005132 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005133 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005134 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005135 } else {
5136 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005137 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005138 __ jmp(slow_path->GetEntryLabel());
5139 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005140 }
5141 __ j(kEqual, slow_path->GetEntryLabel());
5142}
5143
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005144void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005145 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005146}
5147
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005148void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005149 bool object_array_get_with_read_barrier =
5150 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005151 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005152 new (GetGraph()->GetArena()) LocationSummary(instruction,
5153 object_array_get_with_read_barrier ?
5154 LocationSummary::kCallOnSlowPath :
5155 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005156 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005157 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005158 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005159 locations->SetInAt(0, Location::RequiresRegister());
5160 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005161 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5162 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5163 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005164 // The output overlaps in case of long: we don't want the low move
5165 // to overwrite the array's location. Likewise, in the case of an
5166 // object array get with read barriers enabled, we do not want the
5167 // move to overwrite the array's location, as we need it to emit
5168 // the read barrier.
5169 locations->SetOut(
5170 Location::RequiresRegister(),
5171 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5172 Location::kOutputOverlap :
5173 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005174 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005175}
5176
5177void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5178 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005179 Location obj_loc = locations->InAt(0);
5180 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005181 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005182 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005183 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005184
Calin Juravle77520bc2015-01-12 18:45:46 +00005185 Primitive::Type type = instruction->GetType();
5186 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005187 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005188 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005189 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005190 break;
5191 }
5192
5193 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005194 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005195 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005196 break;
5197 }
5198
5199 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005200 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005201 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005202 break;
5203 }
5204
5205 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005206 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005207 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5208 // Branch cases into compressed and uncompressed for each index's type.
5209 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5210 NearLabel done, not_compressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005211 __ testl(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005212 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005213 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5214 "Expecting 0=compressed, 1=uncompressed");
5215 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005216 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5217 __ jmp(&done);
5218 __ Bind(&not_compressed);
5219 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5220 __ Bind(&done);
5221 } else {
5222 // Common case for charAt of array of char or when string compression's
5223 // feature is turned off.
5224 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5225 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005226 break;
5227 }
5228
Roland Levillain7c1559a2015-12-15 10:55:36 +00005229 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005230 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005231 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005232 break;
5233 }
5234
Roland Levillain7c1559a2015-12-15 10:55:36 +00005235 case Primitive::kPrimNot: {
5236 static_assert(
5237 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5238 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005239 // /* HeapReference<Object> */ out =
5240 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5241 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005242 // Note that a potential implicit null check is handled in this
5243 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5244 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005245 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005246 } else {
5247 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005248 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5249 codegen_->MaybeRecordImplicitNullCheck(instruction);
5250 // If read barriers are enabled, emit read barriers other than
5251 // Baker's using a slow path (and also unpoison the loaded
5252 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005253 if (index.IsConstant()) {
5254 uint32_t offset =
5255 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005256 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5257 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005258 codegen_->MaybeGenerateReadBarrierSlow(
5259 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5260 }
5261 }
5262 break;
5263 }
5264
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005265 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005266 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005267 __ movl(out_loc.AsRegisterPairLow<Register>(),
5268 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5269 codegen_->MaybeRecordImplicitNullCheck(instruction);
5270 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5271 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005272 break;
5273 }
5274
Mark Mendell7c8d0092015-01-26 11:21:33 -05005275 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005276 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005277 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005278 break;
5279 }
5280
5281 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005282 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005283 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005284 break;
5285 }
5286
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005287 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005288 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005289 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005290 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005291
Roland Levillain7c1559a2015-12-15 10:55:36 +00005292 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5293 // Potential implicit null checks, in the case of reference or
5294 // long arrays, are handled in the previous switch statement.
5295 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005296 codegen_->MaybeRecordImplicitNullCheck(instruction);
5297 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005298}
5299
5300void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005301 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005302
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005303 bool needs_write_barrier =
5304 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005305 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005306
Nicolas Geoffray39468442014-09-02 15:17:15 +01005307 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5308 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005309 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005310 LocationSummary::kCallOnSlowPath :
5311 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005312
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005313 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5314 || (value_type == Primitive::kPrimByte);
5315 // We need the inputs to be different than the output in case of long operation.
5316 // In case of a byte operation, the register allocator does not support multiple
5317 // inputs that die at entry with one in a specific register.
5318 locations->SetInAt(0, Location::RequiresRegister());
5319 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5320 if (is_byte_type) {
5321 // Ensure the value is in a byte register.
5322 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5323 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005324 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005325 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005326 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5327 }
5328 if (needs_write_barrier) {
5329 // Temporary registers for the write barrier.
5330 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5331 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005332 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005333 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005334}
5335
5336void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5337 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005338 Location array_loc = locations->InAt(0);
5339 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005340 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005341 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005342 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005343 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5344 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5345 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005346 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005347 bool needs_write_barrier =
5348 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005349
5350 switch (value_type) {
5351 case Primitive::kPrimBoolean:
5352 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005353 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005354 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005355 if (value.IsRegister()) {
5356 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005357 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005358 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005359 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005360 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361 break;
5362 }
5363
5364 case Primitive::kPrimShort:
5365 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005366 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005367 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005368 if (value.IsRegister()) {
5369 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005370 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005371 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005372 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005373 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374 break;
5375 }
5376
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005377 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005378 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005379 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005380
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005381 if (!value.IsRegister()) {
5382 // Just setting null.
5383 DCHECK(instruction->InputAt(2)->IsNullConstant());
5384 DCHECK(value.IsConstant()) << value;
5385 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005386 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005387 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005388 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005389 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005391
5392 DCHECK(needs_write_barrier);
5393 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005394 // We cannot use a NearLabel for `done`, as its range may be too
5395 // short when Baker read barriers are enabled.
5396 Label done;
5397 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005398 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005399 Location temp_loc = locations->GetTemp(0);
5400 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005401 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005402 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5403 codegen_->AddSlowPath(slow_path);
5404 if (instruction->GetValueCanBeNull()) {
5405 __ testl(register_value, register_value);
5406 __ j(kNotEqual, &not_null);
5407 __ movl(address, Immediate(0));
5408 codegen_->MaybeRecordImplicitNullCheck(instruction);
5409 __ jmp(&done);
5410 __ Bind(&not_null);
5411 }
5412
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005413 // Note that when Baker read barriers are enabled, the type
5414 // checks are performed without read barriers. This is fine,
5415 // even in the case where a class object is in the from-space
5416 // after the flip, as a comparison involving such a type would
5417 // not produce a false positive; it may of course produce a
5418 // false negative, in which case we would take the ArraySet
5419 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005420
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005421 // /* HeapReference<Class> */ temp = array->klass_
5422 __ movl(temp, Address(array, class_offset));
5423 codegen_->MaybeRecordImplicitNullCheck(instruction);
5424 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005425
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005426 // /* HeapReference<Class> */ temp = temp->component_type_
5427 __ movl(temp, Address(temp, component_offset));
5428 // If heap poisoning is enabled, no need to unpoison `temp`
5429 // nor the object reference in `register_value->klass`, as
5430 // we are comparing two poisoned references.
5431 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005432
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005433 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5434 __ j(kEqual, &do_put);
5435 // If heap poisoning is enabled, the `temp` reference has
5436 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005437 __ MaybeUnpoisonHeapReference(temp);
5438
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005439 // If heap poisoning is enabled, no need to unpoison the
5440 // heap reference loaded below, as it is only used for a
5441 // comparison with null.
5442 __ cmpl(Address(temp, super_offset), Immediate(0));
5443 __ j(kNotEqual, slow_path->GetEntryLabel());
5444 __ Bind(&do_put);
5445 } else {
5446 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005447 }
5448 }
5449
5450 if (kPoisonHeapReferences) {
5451 __ movl(temp, register_value);
5452 __ PoisonHeapReference(temp);
5453 __ movl(address, temp);
5454 } else {
5455 __ movl(address, register_value);
5456 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005457 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005458 codegen_->MaybeRecordImplicitNullCheck(instruction);
5459 }
5460
5461 Register card = locations->GetTemp(1).AsRegister<Register>();
5462 codegen_->MarkGCCard(
5463 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5464 __ Bind(&done);
5465
5466 if (slow_path != nullptr) {
5467 __ Bind(slow_path->GetExitLabel());
5468 }
5469
5470 break;
5471 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005472
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005473 case Primitive::kPrimInt: {
5474 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005475 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005476 if (value.IsRegister()) {
5477 __ movl(address, value.AsRegister<Register>());
5478 } else {
5479 DCHECK(value.IsConstant()) << value;
5480 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5481 __ movl(address, Immediate(v));
5482 }
5483 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005484 break;
5485 }
5486
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005487 case Primitive::kPrimLong: {
5488 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005489 if (value.IsRegisterPair()) {
5490 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5491 value.AsRegisterPairLow<Register>());
5492 codegen_->MaybeRecordImplicitNullCheck(instruction);
5493 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5494 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005495 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005496 DCHECK(value.IsConstant());
5497 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5498 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5499 Immediate(Low32Bits(val)));
5500 codegen_->MaybeRecordImplicitNullCheck(instruction);
5501 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5502 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005503 }
5504 break;
5505 }
5506
Mark Mendell7c8d0092015-01-26 11:21:33 -05005507 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005508 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005509 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005510 if (value.IsFpuRegister()) {
5511 __ movss(address, value.AsFpuRegister<XmmRegister>());
5512 } else {
5513 DCHECK(value.IsConstant());
5514 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5515 __ movl(address, Immediate(v));
5516 }
5517 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005518 break;
5519 }
5520
5521 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005522 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005523 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005524 if (value.IsFpuRegister()) {
5525 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5526 } else {
5527 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005528 Address address_hi =
5529 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005530 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5531 __ movl(address, Immediate(Low32Bits(v)));
5532 codegen_->MaybeRecordImplicitNullCheck(instruction);
5533 __ movl(address_hi, Immediate(High32Bits(v)));
5534 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005535 break;
5536 }
5537
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005538 case Primitive::kPrimVoid:
5539 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005540 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005541 }
5542}
5543
5544void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5545 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005546 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005547 if (!instruction->IsEmittedAtUseSite()) {
5548 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5549 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005550}
5551
5552void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005553 if (instruction->IsEmittedAtUseSite()) {
5554 return;
5555 }
5556
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005557 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005558 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005559 Register obj = locations->InAt(0).AsRegister<Register>();
5560 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005561 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005562 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005563 // Mask out most significant bit in case the array is String's array of char.
5564 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005565 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005566 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005567}
5568
5569void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005570 RegisterSet caller_saves = RegisterSet::Empty();
5571 InvokeRuntimeCallingConvention calling_convention;
5572 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5573 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5574 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005575 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005576 HInstruction* length = instruction->InputAt(1);
5577 if (!length->IsEmittedAtUseSite()) {
5578 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5579 }
jessicahandojo4877b792016-09-08 19:49:13 -07005580 // Need register to see array's length.
5581 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5582 locations->AddTemp(Location::RequiresRegister());
5583 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005584}
5585
5586void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005587 const bool is_string_compressed_char_at =
5588 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005589 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005590 Location index_loc = locations->InAt(0);
5591 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005592 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005593 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005594
Mark Mendell99dbd682015-04-22 16:18:52 -04005595 if (length_loc.IsConstant()) {
5596 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5597 if (index_loc.IsConstant()) {
5598 // BCE will remove the bounds check if we are guarenteed to pass.
5599 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5600 if (index < 0 || index >= length) {
5601 codegen_->AddSlowPath(slow_path);
5602 __ jmp(slow_path->GetEntryLabel());
5603 } else {
5604 // Some optimization after BCE may have generated this, and we should not
5605 // generate a bounds check if it is a valid range.
5606 }
5607 return;
5608 }
5609
5610 // We have to reverse the jump condition because the length is the constant.
5611 Register index_reg = index_loc.AsRegister<Register>();
5612 __ cmpl(index_reg, Immediate(length));
5613 codegen_->AddSlowPath(slow_path);
5614 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005615 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005616 HInstruction* array_length = instruction->InputAt(1);
5617 if (array_length->IsEmittedAtUseSite()) {
5618 // Address the length field in the array.
5619 DCHECK(array_length->IsArrayLength());
5620 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5621 Location array_loc = array_length->GetLocations()->InAt(0);
5622 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005623 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005624 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5625 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005626 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5627 __ movl(length_reg, array_len);
5628 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005629 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005630 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005631 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005632 // Checking bounds for general case:
5633 // Array of char or string's array with feature compression off.
5634 if (index_loc.IsConstant()) {
5635 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5636 __ cmpl(array_len, Immediate(value));
5637 } else {
5638 __ cmpl(array_len, index_loc.AsRegister<Register>());
5639 }
5640 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005641 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005642 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005643 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005644 }
5645 codegen_->AddSlowPath(slow_path);
5646 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005647 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005648}
5649
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005650void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005651 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005652}
5653
5654void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005655 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5656}
5657
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005658void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005659 LocationSummary* locations =
5660 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005661 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005662}
5663
5664void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005665 HBasicBlock* block = instruction->GetBlock();
5666 if (block->GetLoopInformation() != nullptr) {
5667 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5668 // The back edge will generate the suspend check.
5669 return;
5670 }
5671 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5672 // The goto will generate the suspend check.
5673 return;
5674 }
5675 GenerateSuspendCheck(instruction, nullptr);
5676}
5677
5678void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5679 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005680 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005681 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5682 if (slow_path == nullptr) {
5683 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5684 instruction->SetSlowPath(slow_path);
5685 codegen_->AddSlowPath(slow_path);
5686 if (successor != nullptr) {
5687 DCHECK(successor->IsLoopHeader());
5688 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5689 }
5690 } else {
5691 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5692 }
5693
Andreas Gampe542451c2016-07-26 09:02:02 -07005694 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005695 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005696 if (successor == nullptr) {
5697 __ j(kNotEqual, slow_path->GetEntryLabel());
5698 __ Bind(slow_path->GetReturnLabel());
5699 } else {
5700 __ j(kEqual, codegen_->GetLabelOf(successor));
5701 __ jmp(slow_path->GetEntryLabel());
5702 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005703}
5704
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005705X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5706 return codegen_->GetAssembler();
5707}
5708
Mark Mendell7c8d0092015-01-26 11:21:33 -05005709void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005710 ScratchRegisterScope ensure_scratch(
5711 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5712 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5713 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5714 __ movl(temp_reg, Address(ESP, src + stack_offset));
5715 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005716}
5717
5718void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005719 ScratchRegisterScope ensure_scratch(
5720 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5721 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5722 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5723 __ movl(temp_reg, Address(ESP, src + stack_offset));
5724 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5725 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5726 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005727}
5728
5729void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005730 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005731 Location source = move->GetSource();
5732 Location destination = move->GetDestination();
5733
5734 if (source.IsRegister()) {
5735 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005736 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005737 } else if (destination.IsFpuRegister()) {
5738 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005739 } else {
5740 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005741 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005742 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005743 } else if (source.IsRegisterPair()) {
5744 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5745 // Create stack space for 2 elements.
5746 __ subl(ESP, Immediate(2 * elem_size));
5747 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5748 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5749 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5750 // And remove the temporary stack space we allocated.
5751 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005752 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005753 if (destination.IsRegister()) {
5754 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5755 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005756 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005757 } else if (destination.IsRegisterPair()) {
5758 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5759 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5760 __ psrlq(src_reg, Immediate(32));
5761 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005762 } else if (destination.IsStackSlot()) {
5763 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5764 } else {
5765 DCHECK(destination.IsDoubleStackSlot());
5766 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5767 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005768 } else if (source.IsStackSlot()) {
5769 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005770 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005771 } else if (destination.IsFpuRegister()) {
5772 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005773 } else {
5774 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005775 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5776 }
5777 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005778 if (destination.IsRegisterPair()) {
5779 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5780 __ movl(destination.AsRegisterPairHigh<Register>(),
5781 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5782 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005783 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5784 } else {
5785 DCHECK(destination.IsDoubleStackSlot()) << destination;
5786 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005787 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005788 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005789 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005790 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005791 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005792 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005793 if (value == 0) {
5794 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5795 } else {
5796 __ movl(destination.AsRegister<Register>(), Immediate(value));
5797 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005798 } else {
5799 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005800 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005801 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005802 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005803 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005804 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005805 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005806 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005807 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5808 if (value == 0) {
5809 // Easy handling of 0.0.
5810 __ xorps(dest, dest);
5811 } else {
5812 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005813 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5814 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5815 __ movl(temp, Immediate(value));
5816 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005817 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005818 } else {
5819 DCHECK(destination.IsStackSlot()) << destination;
5820 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5821 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005822 } else if (constant->IsLongConstant()) {
5823 int64_t value = constant->AsLongConstant()->GetValue();
5824 int32_t low_value = Low32Bits(value);
5825 int32_t high_value = High32Bits(value);
5826 Immediate low(low_value);
5827 Immediate high(high_value);
5828 if (destination.IsDoubleStackSlot()) {
5829 __ movl(Address(ESP, destination.GetStackIndex()), low);
5830 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5831 } else {
5832 __ movl(destination.AsRegisterPairLow<Register>(), low);
5833 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5834 }
5835 } else {
5836 DCHECK(constant->IsDoubleConstant());
5837 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005838 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005839 int32_t low_value = Low32Bits(value);
5840 int32_t high_value = High32Bits(value);
5841 Immediate low(low_value);
5842 Immediate high(high_value);
5843 if (destination.IsFpuRegister()) {
5844 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5845 if (value == 0) {
5846 // Easy handling of 0.0.
5847 __ xorpd(dest, dest);
5848 } else {
5849 __ pushl(high);
5850 __ pushl(low);
5851 __ movsd(dest, Address(ESP, 0));
5852 __ addl(ESP, Immediate(8));
5853 }
5854 } else {
5855 DCHECK(destination.IsDoubleStackSlot()) << destination;
5856 __ movl(Address(ESP, destination.GetStackIndex()), low);
5857 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5858 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005859 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005860 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005861 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005862 }
5863}
5864
Mark Mendella5c19ce2015-04-01 12:51:05 -04005865void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005866 Register suggested_scratch = reg == EAX ? EBX : EAX;
5867 ScratchRegisterScope ensure_scratch(
5868 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5869
5870 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5871 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5872 __ movl(Address(ESP, mem + stack_offset), reg);
5873 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005874}
5875
Mark Mendell7c8d0092015-01-26 11:21:33 -05005876void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005877 ScratchRegisterScope ensure_scratch(
5878 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5879
5880 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5881 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5882 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5883 __ movss(Address(ESP, mem + stack_offset), reg);
5884 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005885}
5886
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005887void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005888 ScratchRegisterScope ensure_scratch1(
5889 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005890
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005891 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5892 ScratchRegisterScope ensure_scratch2(
5893 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005894
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005895 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5896 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5897 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5898 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5899 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5900 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005901}
5902
5903void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005904 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005905 Location source = move->GetSource();
5906 Location destination = move->GetDestination();
5907
5908 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005909 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5910 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5911 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5912 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5913 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005914 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005915 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005916 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005917 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005918 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5919 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005920 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5921 // Use XOR Swap algorithm to avoid a temporary.
5922 DCHECK_NE(source.reg(), destination.reg());
5923 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5924 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5925 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5926 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5927 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5928 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5929 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005930 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5931 // Take advantage of the 16 bytes in the XMM register.
5932 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5933 Address stack(ESP, destination.GetStackIndex());
5934 // Load the double into the high doubleword.
5935 __ movhpd(reg, stack);
5936
5937 // Store the low double into the destination.
5938 __ movsd(stack, reg);
5939
5940 // Move the high double to the low double.
5941 __ psrldq(reg, Immediate(8));
5942 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5943 // Take advantage of the 16 bytes in the XMM register.
5944 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5945 Address stack(ESP, source.GetStackIndex());
5946 // Load the double into the high doubleword.
5947 __ movhpd(reg, stack);
5948
5949 // Store the low double into the destination.
5950 __ movsd(stack, reg);
5951
5952 // Move the high double to the low double.
5953 __ psrldq(reg, Immediate(8));
5954 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5955 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5956 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005957 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005958 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005959 }
5960}
5961
5962void ParallelMoveResolverX86::SpillScratch(int reg) {
5963 __ pushl(static_cast<Register>(reg));
5964}
5965
5966void ParallelMoveResolverX86::RestoreScratch(int reg) {
5967 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005968}
5969
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005970HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5971 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005972 switch (desired_class_load_kind) {
5973 case HLoadClass::LoadKind::kReferrersClass:
5974 break;
5975 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5976 DCHECK(!GetCompilerOptions().GetCompilePic());
5977 break;
5978 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5979 DCHECK(GetCompilerOptions().GetCompilePic());
5980 FALLTHROUGH_INTENDED;
5981 case HLoadClass::LoadKind::kDexCachePcRelative:
5982 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5983 // We disable pc-relative load when there is an irreducible loop, as the optimization
5984 // is incompatible with it.
5985 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5986 // with irreducible loops.
5987 if (GetGraph()->HasIrreducibleLoops()) {
5988 return HLoadClass::LoadKind::kDexCacheViaMethod;
5989 }
5990 break;
5991 case HLoadClass::LoadKind::kBootImageAddress:
5992 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005993 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005994 DCHECK(Runtime::Current()->UseJitCompilation());
5995 break;
5996 case HLoadClass::LoadKind::kDexCacheViaMethod:
5997 break;
5998 }
5999 return desired_class_load_kind;
6000}
6001
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006002void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006003 if (cls->NeedsAccessCheck()) {
6004 InvokeRuntimeCallingConvention calling_convention;
6005 CodeGenerator::CreateLoadClassLocationSummary(
6006 cls,
6007 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6008 Location::RegisterLocation(EAX),
6009 /* code_generator_supports_read_barrier */ true);
6010 return;
6011 }
6012
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006013 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6014 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006015 ? LocationSummary::kCallOnSlowPath
6016 : LocationSummary::kNoCall;
6017 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006018 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006019 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006020 }
6021
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006022 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6023 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6024 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6025 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6026 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6027 locations->SetInAt(0, Location::RequiresRegister());
6028 }
6029 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006030}
6031
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006032Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6033 dex::TypeIndex dex_index,
6034 uint64_t address) {
6035 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index), address);
6036 // Add a patch entry and return the label.
6037 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6038 PatchInfo<Label>* info = &jit_class_patches_.back();
6039 return &info->label;
6040}
6041
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006042void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006043 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006044 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08006045 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescuba45db02016-07-12 22:53:02 +01006046 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006047 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006048 return;
6049 }
6050
Roland Levillain0d5a2812015-11-13 10:07:31 +00006051 Location out_loc = locations->Out();
6052 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006053
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006054 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006055 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6056 ? kWithoutReadBarrier
6057 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006058 switch (cls->GetLoadKind()) {
6059 case HLoadClass::LoadKind::kReferrersClass: {
6060 DCHECK(!cls->CanCallRuntime());
6061 DCHECK(!cls->MustGenerateClinitCheck());
6062 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6063 Register current_method = locations->InAt(0).AsRegister<Register>();
6064 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006065 cls,
6066 out_loc,
6067 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006068 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006069 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006070 break;
6071 }
6072 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006073 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006074 __ movl(out, Immediate(/* placeholder */ 0));
6075 codegen_->RecordTypePatch(cls);
6076 break;
6077 }
6078 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006079 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006080 Register method_address = locations->InAt(0).AsRegister<Register>();
6081 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6082 codegen_->RecordTypePatch(cls);
6083 break;
6084 }
6085 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006086 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006087 DCHECK_NE(cls->GetAddress(), 0u);
6088 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6089 __ movl(out, Immediate(address));
6090 codegen_->RecordSimplePatch();
6091 break;
6092 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006093 case HLoadClass::LoadKind::kJitTableAddress: {
6094 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6095 Label* fixup_label = codegen_->NewJitRootClassPatch(
6096 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetAddress());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006097 // /* GcRoot<mirror::Class> */ out = *address
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006098 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006099 break;
6100 }
6101 case HLoadClass::LoadKind::kDexCachePcRelative: {
6102 Register base_reg = locations->InAt(0).AsRegister<Register>();
6103 uint32_t offset = cls->GetDexCacheElementOffset();
6104 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6105 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006106 GenerateGcRootFieldLoad(cls,
6107 out_loc,
6108 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
6109 fixup_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006110 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006111 generate_null_check = !cls->IsInDexCache();
6112 break;
6113 }
6114 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6115 // /* GcRoot<mirror::Class>[] */ out =
6116 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6117 Register current_method = locations->InAt(0).AsRegister<Register>();
6118 __ movl(out, Address(current_method,
6119 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6120 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006121 GenerateGcRootFieldLoad(cls,
6122 out_loc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006123 Address(out,
6124 CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_)),
Roland Levillain00468f32016-10-27 18:02:48 +01006125 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006126 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006127 generate_null_check = !cls->IsInDexCache();
6128 break;
6129 }
6130 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006131
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006132 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6133 DCHECK(cls->CanCallRuntime());
6134 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6135 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6136 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006137
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006138 if (generate_null_check) {
6139 __ testl(out, out);
6140 __ j(kEqual, slow_path->GetEntryLabel());
6141 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006142
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006143 if (cls->MustGenerateClinitCheck()) {
6144 GenerateClassInitializationCheck(slow_path, out);
6145 } else {
6146 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006147 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006148 }
6149}
6150
6151void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6152 LocationSummary* locations =
6153 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6154 locations->SetInAt(0, Location::RequiresRegister());
6155 if (check->HasUses()) {
6156 locations->SetOut(Location::SameAsFirstInput());
6157 }
6158}
6159
6160void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006161 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006162 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006163 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006164 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006165 GenerateClassInitializationCheck(slow_path,
6166 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006167}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006168
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006169void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006170 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006171 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6172 Immediate(mirror::Class::kStatusInitialized));
6173 __ j(kLess, slow_path->GetEntryLabel());
6174 __ Bind(slow_path->GetExitLabel());
6175 // No need for memory fence, thanks to the X86 memory model.
6176}
6177
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006178HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6179 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006180 switch (desired_string_load_kind) {
6181 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6182 DCHECK(!GetCompilerOptions().GetCompilePic());
6183 break;
6184 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6185 DCHECK(GetCompilerOptions().GetCompilePic());
6186 FALLTHROUGH_INTENDED;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006187 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006188 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006189 // We disable pc-relative load when there is an irreducible loop, as the optimization
6190 // is incompatible with it.
6191 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6192 // with irreducible loops.
6193 if (GetGraph()->HasIrreducibleLoops()) {
6194 return HLoadString::LoadKind::kDexCacheViaMethod;
6195 }
6196 break;
6197 case HLoadString::LoadKind::kBootImageAddress:
6198 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006199 case HLoadString::LoadKind::kDexCacheViaMethod:
6200 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006201 case HLoadString::LoadKind::kJitTableAddress:
6202 DCHECK(Runtime::Current()->UseJitCompilation());
6203 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006204 }
6205 return desired_string_load_kind;
6206}
6207
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006208void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006209 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006210 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006211 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006212 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006213 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006214 locations->SetInAt(0, Location::RequiresRegister());
6215 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006216 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6217 locations->SetOut(Location::RegisterLocation(EAX));
6218 } else {
6219 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006220 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6221 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6222 // Rely on the pResolveString and/or marking to save everything.
6223 RegisterSet caller_saves = RegisterSet::Empty();
6224 InvokeRuntimeCallingConvention calling_convention;
6225 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6226 locations->SetCustomSlowPathCallerSaves(caller_saves);
6227 } else {
6228 // For non-Baker read barrier we have a temp-clobbering call.
6229 }
6230 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006231 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006232}
6233
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006234Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006235 dex::StringIndex dex_index,
6236 Handle<mirror::String> handle) {
6237 jit_string_roots_.Overwrite(
6238 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006239 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006240 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006241 PatchInfo<Label>* info = &jit_string_patches_.back();
6242 return &info->label;
6243}
6244
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006245// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6246// move.
6247void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006248 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006249 Location out_loc = locations->Out();
6250 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006251
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006252 switch (load->GetLoadKind()) {
6253 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006254 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006255 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006256 return; // No dex cache slow path.
6257 }
6258 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006259 Register method_address = locations->InAt(0).AsRegister<Register>();
6260 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006261 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006262 return; // No dex cache slow path.
6263 }
6264 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006265 uint32_t address = dchecked_integral_cast<uint32_t>(
6266 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6267 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006268 __ movl(out, Immediate(address));
6269 codegen_->RecordSimplePatch();
6270 return; // No dex cache slow path.
6271 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006272 case HLoadString::LoadKind::kBssEntry: {
6273 Register method_address = locations->InAt(0).AsRegister<Register>();
6274 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6275 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006276 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006277 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006278 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6279 codegen_->AddSlowPath(slow_path);
6280 __ testl(out, out);
6281 __ j(kEqual, slow_path->GetEntryLabel());
6282 __ Bind(slow_path->GetExitLabel());
6283 return;
6284 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006285 case HLoadString::LoadKind::kJitTableAddress: {
6286 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6287 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006288 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006289 // /* GcRoot<mirror::String> */ out = *address
6290 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6291 return;
6292 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006293 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006294 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006295 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006296
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006297 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006298 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006299 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006300 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006301 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6302 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006303}
6304
David Brazdilcb1c0552015-08-04 16:22:25 +01006305static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006306 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006307}
6308
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006309void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6310 LocationSummary* locations =
6311 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6312 locations->SetOut(Location::RequiresRegister());
6313}
6314
6315void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006316 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6317}
6318
6319void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6320 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6321}
6322
6323void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6324 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006325}
6326
6327void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6328 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006329 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006330 InvokeRuntimeCallingConvention calling_convention;
6331 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6332}
6333
6334void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006335 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006336 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006337}
6338
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006339// Temp is used for read barrier.
6340static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6341 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006342 !kUseBakerReadBarrier &&
6343 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006344 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006345 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6346 return 1;
6347 }
6348 return 0;
6349}
6350
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006351// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006352// interface pointer, one for loading the current interface.
6353// The other checks have one temp for loading the object's class.
6354static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6355 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6356 return 2;
6357 }
6358 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006359}
6360
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006361void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006362 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006363 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006364 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006365 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006366 case TypeCheckKind::kExactCheck:
6367 case TypeCheckKind::kAbstractClassCheck:
6368 case TypeCheckKind::kClassHierarchyCheck:
6369 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006370 call_kind =
6371 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006372 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006373 break;
6374 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006375 case TypeCheckKind::kUnresolvedCheck:
6376 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006377 call_kind = LocationSummary::kCallOnSlowPath;
6378 break;
6379 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006380
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006381 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006382 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006383 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006384 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006385 locations->SetInAt(0, Location::RequiresRegister());
6386 locations->SetInAt(1, Location::Any());
6387 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6388 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006389 // When read barriers are enabled, we need a temporary register for some cases.
6390 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006391}
6392
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006393void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006394 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006395 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006396 Location obj_loc = locations->InAt(0);
6397 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006398 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006399 Location out_loc = locations->Out();
6400 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006401 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6402 DCHECK_LE(num_temps, 1u);
6403 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006404 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006405 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6406 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6407 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006408 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006409 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006410
6411 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006412 // Avoid null check if we know obj is not null.
6413 if (instruction->MustDoNullCheck()) {
6414 __ testl(obj, obj);
6415 __ j(kEqual, &zero);
6416 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006417
Roland Levillain7c1559a2015-12-15 10:55:36 +00006418 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006419 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006420 // /* HeapReference<Class> */ out = obj->klass_
6421 GenerateReferenceLoadTwoRegisters(instruction,
6422 out_loc,
6423 obj_loc,
6424 class_offset,
6425 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006426 if (cls.IsRegister()) {
6427 __ cmpl(out, cls.AsRegister<Register>());
6428 } else {
6429 DCHECK(cls.IsStackSlot()) << cls;
6430 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6431 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006432
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006433 // Classes must be equal for the instanceof to succeed.
6434 __ j(kNotEqual, &zero);
6435 __ movl(out, Immediate(1));
6436 __ jmp(&done);
6437 break;
6438 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006439
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006440 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006441 // /* HeapReference<Class> */ out = obj->klass_
6442 GenerateReferenceLoadTwoRegisters(instruction,
6443 out_loc,
6444 obj_loc,
6445 class_offset,
6446 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006447 // If the class is abstract, we eagerly fetch the super class of the
6448 // object to avoid doing a comparison we know will fail.
6449 NearLabel loop;
6450 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006451 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006452 GenerateReferenceLoadOneRegister(instruction,
6453 out_loc,
6454 super_offset,
6455 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006456 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006457 __ testl(out, out);
6458 // If `out` is null, we use it for the result, and jump to `done`.
6459 __ j(kEqual, &done);
6460 if (cls.IsRegister()) {
6461 __ cmpl(out, cls.AsRegister<Register>());
6462 } else {
6463 DCHECK(cls.IsStackSlot()) << cls;
6464 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6465 }
6466 __ j(kNotEqual, &loop);
6467 __ movl(out, Immediate(1));
6468 if (zero.IsLinked()) {
6469 __ jmp(&done);
6470 }
6471 break;
6472 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006473
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006474 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006475 // /* HeapReference<Class> */ out = obj->klass_
6476 GenerateReferenceLoadTwoRegisters(instruction,
6477 out_loc,
6478 obj_loc,
6479 class_offset,
6480 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006481 // Walk over the class hierarchy to find a match.
6482 NearLabel loop, success;
6483 __ Bind(&loop);
6484 if (cls.IsRegister()) {
6485 __ cmpl(out, cls.AsRegister<Register>());
6486 } else {
6487 DCHECK(cls.IsStackSlot()) << cls;
6488 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6489 }
6490 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006491 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006492 GenerateReferenceLoadOneRegister(instruction,
6493 out_loc,
6494 super_offset,
6495 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006496 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006497 __ testl(out, out);
6498 __ j(kNotEqual, &loop);
6499 // If `out` is null, we use it for the result, and jump to `done`.
6500 __ jmp(&done);
6501 __ Bind(&success);
6502 __ movl(out, Immediate(1));
6503 if (zero.IsLinked()) {
6504 __ jmp(&done);
6505 }
6506 break;
6507 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006508
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006509 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006510 // /* HeapReference<Class> */ out = obj->klass_
6511 GenerateReferenceLoadTwoRegisters(instruction,
6512 out_loc,
6513 obj_loc,
6514 class_offset,
6515 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006516 // Do an exact check.
6517 NearLabel exact_check;
6518 if (cls.IsRegister()) {
6519 __ cmpl(out, cls.AsRegister<Register>());
6520 } else {
6521 DCHECK(cls.IsStackSlot()) << cls;
6522 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6523 }
6524 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006525 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006526 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006527 GenerateReferenceLoadOneRegister(instruction,
6528 out_loc,
6529 component_offset,
6530 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006531 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006532 __ testl(out, out);
6533 // If `out` is null, we use it for the result, and jump to `done`.
6534 __ j(kEqual, &done);
6535 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6536 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006537 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006538 __ movl(out, Immediate(1));
6539 __ jmp(&done);
6540 break;
6541 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006542
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006543 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006544 // No read barrier since the slow path will retry upon failure.
6545 // /* HeapReference<Class> */ out = obj->klass_
6546 GenerateReferenceLoadTwoRegisters(instruction,
6547 out_loc,
6548 obj_loc,
6549 class_offset,
6550 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006551 if (cls.IsRegister()) {
6552 __ cmpl(out, cls.AsRegister<Register>());
6553 } else {
6554 DCHECK(cls.IsStackSlot()) << cls;
6555 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6556 }
6557 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006558 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6559 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006560 codegen_->AddSlowPath(slow_path);
6561 __ j(kNotEqual, slow_path->GetEntryLabel());
6562 __ movl(out, Immediate(1));
6563 if (zero.IsLinked()) {
6564 __ jmp(&done);
6565 }
6566 break;
6567 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006568
Calin Juravle98893e12015-10-02 21:05:03 +01006569 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006570 case TypeCheckKind::kInterfaceCheck: {
6571 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006572 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006573 // cases.
6574 //
6575 // We cannot directly call the InstanceofNonTrivial runtime
6576 // entry point without resorting to a type checking slow path
6577 // here (i.e. by calling InvokeRuntime directly), as it would
6578 // require to assign fixed registers for the inputs of this
6579 // HInstanceOf instruction (following the runtime calling
6580 // convention), which might be cluttered by the potential first
6581 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006582 //
6583 // TODO: Introduce a new runtime entry point taking the object
6584 // to test (instead of its class) as argument, and let it deal
6585 // with the read barrier issues. This will let us refactor this
6586 // case of the `switch` code as it was previously (with a direct
6587 // call to the runtime not using a type checking slow path).
6588 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006589 DCHECK(locations->OnlyCallsOnSlowPath());
6590 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6591 /* is_fatal */ false);
6592 codegen_->AddSlowPath(slow_path);
6593 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006594 if (zero.IsLinked()) {
6595 __ jmp(&done);
6596 }
6597 break;
6598 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006599 }
6600
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006601 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006602 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006603 __ xorl(out, out);
6604 }
6605
6606 if (done.IsLinked()) {
6607 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006608 }
6609
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006610 if (slow_path != nullptr) {
6611 __ Bind(slow_path->GetExitLabel());
6612 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006613}
6614
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006615static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6616 switch (type_check_kind) {
6617 case TypeCheckKind::kExactCheck:
6618 case TypeCheckKind::kAbstractClassCheck:
6619 case TypeCheckKind::kClassHierarchyCheck:
6620 case TypeCheckKind::kArrayObjectCheck:
6621 return !throws_into_catch && !kEmitCompilerReadBarrier;
6622 case TypeCheckKind::kInterfaceCheck:
6623 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6624 case TypeCheckKind::kArrayCheck:
6625 case TypeCheckKind::kUnresolvedCheck:
6626 return false;
6627 }
6628 LOG(FATAL) << "Unreachable";
6629 UNREACHABLE();
6630}
6631
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006632void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006633 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006634 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006635 LocationSummary::CallKind call_kind =
6636 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6637 ? LocationSummary::kNoCall
6638 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006639 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6640 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006641 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6642 // Require a register for the interface check since there is a loop that compares the class to
6643 // a memory address.
6644 locations->SetInAt(1, Location::RequiresRegister());
6645 } else {
6646 locations->SetInAt(1, Location::Any());
6647 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006648 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6649 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006650 // When read barriers are enabled, we need an additional temporary register for some cases.
6651 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6652}
6653
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006654void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006655 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006656 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006657 Location obj_loc = locations->InAt(0);
6658 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006659 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006660 Location temp_loc = locations->GetTemp(0);
6661 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006662 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6663 DCHECK_GE(num_temps, 1u);
6664 DCHECK_LE(num_temps, 2u);
6665 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6666 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6667 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6668 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6669 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6670 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6671 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6672 const uint32_t object_array_data_offset =
6673 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006674
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006675 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6676 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6677 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006678 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006679 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6680
Roland Levillain0d5a2812015-11-13 10:07:31 +00006681 SlowPathCode* type_check_slow_path =
6682 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6683 is_type_check_slow_path_fatal);
6684 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006685
Roland Levillain0d5a2812015-11-13 10:07:31 +00006686 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006687 // Avoid null check if we know obj is not null.
6688 if (instruction->MustDoNullCheck()) {
6689 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006690 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006691 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006692
Roland Levillain0d5a2812015-11-13 10:07:31 +00006693 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006694 case TypeCheckKind::kExactCheck:
6695 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006696 // /* HeapReference<Class> */ temp = obj->klass_
6697 GenerateReferenceLoadTwoRegisters(instruction,
6698 temp_loc,
6699 obj_loc,
6700 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006701 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006702
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006703 if (cls.IsRegister()) {
6704 __ cmpl(temp, cls.AsRegister<Register>());
6705 } else {
6706 DCHECK(cls.IsStackSlot()) << cls;
6707 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6708 }
6709 // Jump to slow path for throwing the exception or doing a
6710 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006711 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006712 break;
6713 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006714
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006715 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006716 // /* HeapReference<Class> */ temp = obj->klass_
6717 GenerateReferenceLoadTwoRegisters(instruction,
6718 temp_loc,
6719 obj_loc,
6720 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006721 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006722
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006723 // If the class is abstract, we eagerly fetch the super class of the
6724 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006725 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006726 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006727 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006728 GenerateReferenceLoadOneRegister(instruction,
6729 temp_loc,
6730 super_offset,
6731 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006732 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006733
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006734 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6735 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006736 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006737 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006738
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006739 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006740 if (cls.IsRegister()) {
6741 __ cmpl(temp, cls.AsRegister<Register>());
6742 } else {
6743 DCHECK(cls.IsStackSlot()) << cls;
6744 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6745 }
6746 __ j(kNotEqual, &loop);
6747 break;
6748 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006749
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006750 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006751 // /* HeapReference<Class> */ temp = obj->klass_
6752 GenerateReferenceLoadTwoRegisters(instruction,
6753 temp_loc,
6754 obj_loc,
6755 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006756 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006757
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006758 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006759 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006760 __ Bind(&loop);
6761 if (cls.IsRegister()) {
6762 __ cmpl(temp, cls.AsRegister<Register>());
6763 } else {
6764 DCHECK(cls.IsStackSlot()) << cls;
6765 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6766 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006767 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006768
Roland Levillain0d5a2812015-11-13 10:07:31 +00006769 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006770 GenerateReferenceLoadOneRegister(instruction,
6771 temp_loc,
6772 super_offset,
6773 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006774 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006775
6776 // If the class reference currently in `temp` is not null, jump
6777 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006778 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006779 __ j(kNotZero, &loop);
6780 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006781 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006782 break;
6783 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006784
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006785 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006786 // /* HeapReference<Class> */ temp = obj->klass_
6787 GenerateReferenceLoadTwoRegisters(instruction,
6788 temp_loc,
6789 obj_loc,
6790 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006791 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006792
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006793 // Do an exact check.
6794 if (cls.IsRegister()) {
6795 __ cmpl(temp, cls.AsRegister<Register>());
6796 } else {
6797 DCHECK(cls.IsStackSlot()) << cls;
6798 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6799 }
6800 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006801
6802 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006803 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006804 GenerateReferenceLoadOneRegister(instruction,
6805 temp_loc,
6806 component_offset,
6807 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006808 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006809
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006810 // If the component type is null (i.e. the object not an array), jump to the slow path to
6811 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006812 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006813 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006814
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006815 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006816 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006817 break;
6818 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006819
Calin Juravle98893e12015-10-02 21:05:03 +01006820 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006821 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006822 // We cannot directly call the CheckCast runtime entry point
6823 // without resorting to a type checking slow path here (i.e. by
6824 // calling InvokeRuntime directly), as it would require to
6825 // assign fixed registers for the inputs of this HInstanceOf
6826 // instruction (following the runtime calling convention), which
6827 // might be cluttered by the potential first read barrier
6828 // emission at the beginning of this method.
6829 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006830 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006831
6832 case TypeCheckKind::kInterfaceCheck: {
6833 // Fast path for the interface check. Since we compare with a memory location in the inner
6834 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6835 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006836 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006837 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006838 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6839 // doing this.
6840 // /* HeapReference<Class> */ temp = obj->klass_
6841 GenerateReferenceLoadTwoRegisters(instruction,
6842 temp_loc,
6843 obj_loc,
6844 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006845 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006846
6847 // /* HeapReference<Class> */ temp = temp->iftable_
6848 GenerateReferenceLoadTwoRegisters(instruction,
6849 temp_loc,
6850 temp_loc,
6851 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006852 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006853 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006854 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006855 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006856 NearLabel start_loop;
6857 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006858 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006859 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006860 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6861 // Go to next interface if the classes do not match.
6862 __ cmpl(cls.AsRegister<Register>(),
6863 CodeGeneratorX86::ArrayAddress(temp,
6864 maybe_temp2_loc,
6865 TIMES_4,
6866 object_array_data_offset));
6867 __ j(kNotEqual, &start_loop);
6868 } else {
6869 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006870 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006871 break;
6872 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006873 }
6874 __ Bind(&done);
6875
Roland Levillain0d5a2812015-11-13 10:07:31 +00006876 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006877}
6878
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006879void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6880 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006881 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006882 InvokeRuntimeCallingConvention calling_convention;
6883 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6884}
6885
6886void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006887 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6888 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006889 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006890 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006891 if (instruction->IsEnter()) {
6892 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6893 } else {
6894 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6895 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006896}
6897
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006898void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6899void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6900void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6901
6902void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6903 LocationSummary* locations =
6904 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6905 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6906 || instruction->GetResultType() == Primitive::kPrimLong);
6907 locations->SetInAt(0, Location::RequiresRegister());
6908 locations->SetInAt(1, Location::Any());
6909 locations->SetOut(Location::SameAsFirstInput());
6910}
6911
6912void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6913 HandleBitwiseOperation(instruction);
6914}
6915
6916void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6917 HandleBitwiseOperation(instruction);
6918}
6919
6920void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6921 HandleBitwiseOperation(instruction);
6922}
6923
6924void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6925 LocationSummary* locations = instruction->GetLocations();
6926 Location first = locations->InAt(0);
6927 Location second = locations->InAt(1);
6928 DCHECK(first.Equals(locations->Out()));
6929
6930 if (instruction->GetResultType() == Primitive::kPrimInt) {
6931 if (second.IsRegister()) {
6932 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006933 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006934 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006935 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006936 } else {
6937 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006938 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006939 }
6940 } else if (second.IsConstant()) {
6941 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006942 __ andl(first.AsRegister<Register>(),
6943 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006944 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006945 __ orl(first.AsRegister<Register>(),
6946 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006947 } else {
6948 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006949 __ xorl(first.AsRegister<Register>(),
6950 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006951 }
6952 } else {
6953 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006954 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006955 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006956 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006957 } else {
6958 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006959 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006960 }
6961 }
6962 } else {
6963 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6964 if (second.IsRegisterPair()) {
6965 if (instruction->IsAnd()) {
6966 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6967 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6968 } else if (instruction->IsOr()) {
6969 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6970 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6971 } else {
6972 DCHECK(instruction->IsXor());
6973 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6974 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6975 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006976 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006977 if (instruction->IsAnd()) {
6978 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6979 __ andl(first.AsRegisterPairHigh<Register>(),
6980 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6981 } else if (instruction->IsOr()) {
6982 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6983 __ orl(first.AsRegisterPairHigh<Register>(),
6984 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6985 } else {
6986 DCHECK(instruction->IsXor());
6987 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6988 __ xorl(first.AsRegisterPairHigh<Register>(),
6989 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6990 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006991 } else {
6992 DCHECK(second.IsConstant()) << second;
6993 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006994 int32_t low_value = Low32Bits(value);
6995 int32_t high_value = High32Bits(value);
6996 Immediate low(low_value);
6997 Immediate high(high_value);
6998 Register first_low = first.AsRegisterPairLow<Register>();
6999 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007000 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007001 if (low_value == 0) {
7002 __ xorl(first_low, first_low);
7003 } else if (low_value != -1) {
7004 __ andl(first_low, low);
7005 }
7006 if (high_value == 0) {
7007 __ xorl(first_high, first_high);
7008 } else if (high_value != -1) {
7009 __ andl(first_high, high);
7010 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007011 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007012 if (low_value != 0) {
7013 __ orl(first_low, low);
7014 }
7015 if (high_value != 0) {
7016 __ orl(first_high, high);
7017 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007018 } else {
7019 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007020 if (low_value != 0) {
7021 __ xorl(first_low, low);
7022 }
7023 if (high_value != 0) {
7024 __ xorl(first_high, high);
7025 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007026 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007027 }
7028 }
7029}
7030
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007031void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7032 HInstruction* instruction,
7033 Location out,
7034 uint32_t offset,
7035 Location maybe_temp,
7036 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007037 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007038 if (read_barrier_option == kWithReadBarrier) {
7039 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007040 if (kUseBakerReadBarrier) {
7041 // Load with fast path based Baker's read barrier.
7042 // /* HeapReference<Object> */ out = *(out + offset)
7043 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007044 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007045 } else {
7046 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007047 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007048 // in the following move operation, as we will need it for the
7049 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007050 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007051 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007052 // /* HeapReference<Object> */ out = *(out + offset)
7053 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007054 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007055 }
7056 } else {
7057 // Plain load with no read barrier.
7058 // /* HeapReference<Object> */ out = *(out + offset)
7059 __ movl(out_reg, Address(out_reg, offset));
7060 __ MaybeUnpoisonHeapReference(out_reg);
7061 }
7062}
7063
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007064void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7065 HInstruction* instruction,
7066 Location out,
7067 Location obj,
7068 uint32_t offset,
7069 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007070 Register out_reg = out.AsRegister<Register>();
7071 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007072 if (read_barrier_option == kWithReadBarrier) {
7073 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007074 if (kUseBakerReadBarrier) {
7075 // Load with fast path based Baker's read barrier.
7076 // /* HeapReference<Object> */ out = *(obj + offset)
7077 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007078 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007079 } else {
7080 // Load with slow path based read barrier.
7081 // /* HeapReference<Object> */ out = *(obj + offset)
7082 __ movl(out_reg, Address(obj_reg, offset));
7083 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7084 }
7085 } else {
7086 // Plain load with no read barrier.
7087 // /* HeapReference<Object> */ out = *(obj + offset)
7088 __ movl(out_reg, Address(obj_reg, offset));
7089 __ MaybeUnpoisonHeapReference(out_reg);
7090 }
7091}
7092
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007093void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7094 HInstruction* instruction,
7095 Location root,
7096 const Address& address,
7097 Label* fixup_label,
7098 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007099 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007100 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007101 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007102 if (kUseBakerReadBarrier) {
7103 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7104 // Baker's read barrier are used:
7105 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007106 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00007107 // if (Thread::Current()->GetIsGcMarking()) {
7108 // root = ReadBarrier::Mark(root)
7109 // }
7110
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007111 // /* GcRoot<mirror::Object> */ root = *address
7112 __ movl(root_reg, address);
7113 if (fixup_label != nullptr) {
7114 __ Bind(fixup_label);
7115 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007116 static_assert(
7117 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7118 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7119 "have different sizes.");
7120 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7121 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7122 "have different sizes.");
7123
Vladimir Marko953437b2016-08-24 08:30:46 +00007124 // Slow path marking the GC root `root`.
7125 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007126 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007127 codegen_->AddSlowPath(slow_path);
7128
Andreas Gampe542451c2016-07-26 09:02:02 -07007129 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00007130 Immediate(0));
7131 __ j(kNotEqual, slow_path->GetEntryLabel());
7132 __ Bind(slow_path->GetExitLabel());
7133 } else {
7134 // GC root loaded through a slow path for read barriers other
7135 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007136 // /* GcRoot<mirror::Object>* */ root = address
7137 __ leal(root_reg, address);
7138 if (fixup_label != nullptr) {
7139 __ Bind(fixup_label);
7140 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007141 // /* mirror::Object* */ root = root->Read()
7142 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7143 }
7144 } else {
7145 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007146 // /* GcRoot<mirror::Object> */ root = *address
7147 __ movl(root_reg, address);
7148 if (fixup_label != nullptr) {
7149 __ Bind(fixup_label);
7150 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007151 // Note that GC roots are not affected by heap poisoning, thus we
7152 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007153 }
7154}
7155
7156void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7157 Location ref,
7158 Register obj,
7159 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007160 bool needs_null_check) {
7161 DCHECK(kEmitCompilerReadBarrier);
7162 DCHECK(kUseBakerReadBarrier);
7163
7164 // /* HeapReference<Object> */ ref = *(obj + offset)
7165 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007166 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007167}
7168
7169void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7170 Location ref,
7171 Register obj,
7172 uint32_t data_offset,
7173 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007174 bool needs_null_check) {
7175 DCHECK(kEmitCompilerReadBarrier);
7176 DCHECK(kUseBakerReadBarrier);
7177
Roland Levillain3d312422016-06-23 13:53:42 +01007178 static_assert(
7179 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7180 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007181 // /* HeapReference<Object> */ ref =
7182 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007183 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007184 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007185}
7186
7187void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7188 Location ref,
7189 Register obj,
7190 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007191 bool needs_null_check,
7192 bool always_update_field,
7193 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007194 DCHECK(kEmitCompilerReadBarrier);
7195 DCHECK(kUseBakerReadBarrier);
7196
7197 // In slow path based read barriers, the read barrier call is
7198 // inserted after the original load. However, in fast path based
7199 // Baker's read barriers, we need to perform the load of
7200 // mirror::Object::monitor_ *before* the original reference load.
7201 // This load-load ordering is required by the read barrier.
7202 // The fast path/slow path (for Baker's algorithm) should look like:
7203 //
7204 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7205 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7206 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007207 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007208 // if (is_gray) {
7209 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7210 // }
7211 //
7212 // Note: the original implementation in ReadBarrier::Barrier is
7213 // slightly more complex as:
7214 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007215 // the high-bits of rb_state, which are expected to be all zeroes
7216 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7217 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007218 // - it performs additional checks that we do not do here for
7219 // performance reasons.
7220
7221 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007222 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7223
Vladimir Marko953437b2016-08-24 08:30:46 +00007224 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007225 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7226 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007227 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7228 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7229 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7230
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007231 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007232 // ref = ReadBarrier::Mark(ref);
7233 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7234 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007235 if (needs_null_check) {
7236 MaybeRecordImplicitNullCheck(instruction);
7237 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007238
7239 // Load fence to prevent load-load reordering.
7240 // Note that this is a no-op, thanks to the x86 memory model.
7241 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7242
7243 // The actual reference load.
7244 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007245 __ movl(ref_reg, src); // Flags are unaffected.
7246
7247 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7248 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007249 SlowPathCode* slow_path;
7250 if (always_update_field) {
7251 DCHECK(temp != nullptr);
7252 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7253 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7254 } else {
7255 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7256 instruction, ref, /* unpoison_ref_before_marking */ true);
7257 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007258 AddSlowPath(slow_path);
7259
7260 // We have done the "if" of the gray bit check above, now branch based on the flags.
7261 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007262
7263 // Object* ref = ref_addr->AsMirrorPtr()
7264 __ MaybeUnpoisonHeapReference(ref_reg);
7265
Roland Levillain7c1559a2015-12-15 10:55:36 +00007266 __ Bind(slow_path->GetExitLabel());
7267}
7268
7269void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7270 Location out,
7271 Location ref,
7272 Location obj,
7273 uint32_t offset,
7274 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007275 DCHECK(kEmitCompilerReadBarrier);
7276
Roland Levillain7c1559a2015-12-15 10:55:36 +00007277 // Insert a slow path based read barrier *after* the reference load.
7278 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007279 // If heap poisoning is enabled, the unpoisoning of the loaded
7280 // reference will be carried out by the runtime within the slow
7281 // path.
7282 //
7283 // Note that `ref` currently does not get unpoisoned (when heap
7284 // poisoning is enabled), which is alright as the `ref` argument is
7285 // not used by the artReadBarrierSlow entry point.
7286 //
7287 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7288 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7289 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7290 AddSlowPath(slow_path);
7291
Roland Levillain0d5a2812015-11-13 10:07:31 +00007292 __ jmp(slow_path->GetEntryLabel());
7293 __ Bind(slow_path->GetExitLabel());
7294}
7295
Roland Levillain7c1559a2015-12-15 10:55:36 +00007296void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7297 Location out,
7298 Location ref,
7299 Location obj,
7300 uint32_t offset,
7301 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007302 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007303 // Baker's read barriers shall be handled by the fast path
7304 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7305 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007306 // If heap poisoning is enabled, unpoisoning will be taken care of
7307 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007308 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007309 } else if (kPoisonHeapReferences) {
7310 __ UnpoisonHeapReference(out.AsRegister<Register>());
7311 }
7312}
7313
Roland Levillain7c1559a2015-12-15 10:55:36 +00007314void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7315 Location out,
7316 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007317 DCHECK(kEmitCompilerReadBarrier);
7318
Roland Levillain7c1559a2015-12-15 10:55:36 +00007319 // Insert a slow path based read barrier *after* the GC root load.
7320 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007321 // Note that GC roots are not affected by heap poisoning, so we do
7322 // not need to do anything special for this here.
7323 SlowPathCode* slow_path =
7324 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7325 AddSlowPath(slow_path);
7326
Roland Levillain0d5a2812015-11-13 10:07:31 +00007327 __ jmp(slow_path->GetEntryLabel());
7328 __ Bind(slow_path->GetExitLabel());
7329}
7330
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007331void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007332 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007333 LOG(FATAL) << "Unreachable";
7334}
7335
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007336void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007337 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007338 LOG(FATAL) << "Unreachable";
7339}
7340
Mark Mendellfe57faa2015-09-18 09:26:15 -04007341// Simple implementation of packed switch - generate cascaded compare/jumps.
7342void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7343 LocationSummary* locations =
7344 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7345 locations->SetInAt(0, Location::RequiresRegister());
7346}
7347
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007348void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7349 int32_t lower_bound,
7350 uint32_t num_entries,
7351 HBasicBlock* switch_block,
7352 HBasicBlock* default_block) {
7353 // Figure out the correct compare values and jump conditions.
7354 // Handle the first compare/branch as a special case because it might
7355 // jump to the default case.
7356 DCHECK_GT(num_entries, 2u);
7357 Condition first_condition;
7358 uint32_t index;
7359 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7360 if (lower_bound != 0) {
7361 first_condition = kLess;
7362 __ cmpl(value_reg, Immediate(lower_bound));
7363 __ j(first_condition, codegen_->GetLabelOf(default_block));
7364 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007365
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007366 index = 1;
7367 } else {
7368 // Handle all the compare/jumps below.
7369 first_condition = kBelow;
7370 index = 0;
7371 }
7372
7373 // Handle the rest of the compare/jumps.
7374 for (; index + 1 < num_entries; index += 2) {
7375 int32_t compare_to_value = lower_bound + index + 1;
7376 __ cmpl(value_reg, Immediate(compare_to_value));
7377 // Jump to successors[index] if value < case_value[index].
7378 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7379 // Jump to successors[index + 1] if value == case_value[index + 1].
7380 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7381 }
7382
7383 if (index != num_entries) {
7384 // There are an odd number of entries. Handle the last one.
7385 DCHECK_EQ(index + 1, num_entries);
7386 __ cmpl(value_reg, Immediate(lower_bound + index));
7387 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007388 }
7389
7390 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007391 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7392 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007393 }
7394}
7395
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007396void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7397 int32_t lower_bound = switch_instr->GetStartValue();
7398 uint32_t num_entries = switch_instr->GetNumEntries();
7399 LocationSummary* locations = switch_instr->GetLocations();
7400 Register value_reg = locations->InAt(0).AsRegister<Register>();
7401
7402 GenPackedSwitchWithCompares(value_reg,
7403 lower_bound,
7404 num_entries,
7405 switch_instr->GetBlock(),
7406 switch_instr->GetDefaultBlock());
7407}
7408
Mark Mendell805b3b52015-09-18 14:10:29 -04007409void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7410 LocationSummary* locations =
7411 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7412 locations->SetInAt(0, Location::RequiresRegister());
7413
7414 // Constant area pointer.
7415 locations->SetInAt(1, Location::RequiresRegister());
7416
7417 // And the temporary we need.
7418 locations->AddTemp(Location::RequiresRegister());
7419}
7420
7421void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7422 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007423 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007424 LocationSummary* locations = switch_instr->GetLocations();
7425 Register value_reg = locations->InAt(0).AsRegister<Register>();
7426 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7427
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007428 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7429 GenPackedSwitchWithCompares(value_reg,
7430 lower_bound,
7431 num_entries,
7432 switch_instr->GetBlock(),
7433 default_block);
7434 return;
7435 }
7436
Mark Mendell805b3b52015-09-18 14:10:29 -04007437 // Optimizing has a jump area.
7438 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7439 Register constant_area = locations->InAt(1).AsRegister<Register>();
7440
7441 // Remove the bias, if needed.
7442 if (lower_bound != 0) {
7443 __ leal(temp_reg, Address(value_reg, -lower_bound));
7444 value_reg = temp_reg;
7445 }
7446
7447 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007448 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007449 __ cmpl(value_reg, Immediate(num_entries - 1));
7450 __ j(kAbove, codegen_->GetLabelOf(default_block));
7451
7452 // We are in the range of the table.
7453 // Load (target-constant_area) from the jump table, indexing by the value.
7454 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7455
7456 // Compute the actual target address by adding in constant_area.
7457 __ addl(temp_reg, constant_area);
7458
7459 // And jump.
7460 __ jmp(temp_reg);
7461}
7462
Mark Mendell0616ae02015-04-17 12:49:27 -04007463void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7464 HX86ComputeBaseMethodAddress* insn) {
7465 LocationSummary* locations =
7466 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7467 locations->SetOut(Location::RequiresRegister());
7468}
7469
7470void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7471 HX86ComputeBaseMethodAddress* insn) {
7472 LocationSummary* locations = insn->GetLocations();
7473 Register reg = locations->Out().AsRegister<Register>();
7474
7475 // Generate call to next instruction.
7476 Label next_instruction;
7477 __ call(&next_instruction);
7478 __ Bind(&next_instruction);
7479
7480 // Remember this offset for later use with constant area.
7481 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7482
7483 // Grab the return address off the stack.
7484 __ popl(reg);
7485}
7486
7487void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7488 HX86LoadFromConstantTable* insn) {
7489 LocationSummary* locations =
7490 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7491
7492 locations->SetInAt(0, Location::RequiresRegister());
7493 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7494
7495 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007496 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007497 return;
7498 }
7499
7500 switch (insn->GetType()) {
7501 case Primitive::kPrimFloat:
7502 case Primitive::kPrimDouble:
7503 locations->SetOut(Location::RequiresFpuRegister());
7504 break;
7505
7506 case Primitive::kPrimInt:
7507 locations->SetOut(Location::RequiresRegister());
7508 break;
7509
7510 default:
7511 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7512 }
7513}
7514
7515void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007516 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007517 return;
7518 }
7519
7520 LocationSummary* locations = insn->GetLocations();
7521 Location out = locations->Out();
7522 Register const_area = locations->InAt(0).AsRegister<Register>();
7523 HConstant *value = insn->GetConstant();
7524
7525 switch (insn->GetType()) {
7526 case Primitive::kPrimFloat:
7527 __ movss(out.AsFpuRegister<XmmRegister>(),
7528 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7529 break;
7530
7531 case Primitive::kPrimDouble:
7532 __ movsd(out.AsFpuRegister<XmmRegister>(),
7533 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7534 break;
7535
7536 case Primitive::kPrimInt:
7537 __ movl(out.AsRegister<Register>(),
7538 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7539 break;
7540
7541 default:
7542 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7543 }
7544}
7545
Mark Mendell0616ae02015-04-17 12:49:27 -04007546/**
7547 * Class to handle late fixup of offsets into constant area.
7548 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007549class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007550 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007551 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7552 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7553
7554 protected:
7555 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7556
7557 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007558
7559 private:
7560 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7561 // Patch the correct offset for the instruction. The place to patch is the
7562 // last 4 bytes of the instruction.
7563 // The value to patch is the distance from the offset in the constant area
7564 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007565 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Mathieu Chartier6beced42016-11-15 15:51:31 -08007566 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();
Mark Mendell0616ae02015-04-17 12:49:27 -04007567
7568 // Patch in the right value.
7569 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7570 }
7571
Mark Mendell0616ae02015-04-17 12:49:27 -04007572 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007573 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007574};
7575
Mark Mendell805b3b52015-09-18 14:10:29 -04007576/**
7577 * Class to handle late fixup of offsets to a jump table that will be created in the
7578 * constant area.
7579 */
7580class JumpTableRIPFixup : public RIPFixup {
7581 public:
7582 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7583 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7584
7585 void CreateJumpTable() {
7586 X86Assembler* assembler = codegen_->GetAssembler();
7587
7588 // Ensure that the reference to the jump table has the correct offset.
7589 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7590 SetOffset(offset_in_constant_table);
7591
7592 // The label values in the jump table are computed relative to the
7593 // instruction addressing the constant area.
7594 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7595
7596 // Populate the jump table with the correct values for the jump table.
7597 int32_t num_entries = switch_instr_->GetNumEntries();
7598 HBasicBlock* block = switch_instr_->GetBlock();
7599 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7600 // The value that we want is the target offset - the position of the table.
7601 for (int32_t i = 0; i < num_entries; i++) {
7602 HBasicBlock* b = successors[i];
7603 Label* l = codegen_->GetLabelOf(b);
7604 DCHECK(l->IsBound());
7605 int32_t offset_to_block = l->Position() - relative_offset;
7606 assembler->AppendInt32(offset_to_block);
7607 }
7608 }
7609
7610 private:
7611 const HX86PackedSwitch* switch_instr_;
7612};
7613
7614void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7615 // Generate the constant area if needed.
7616 X86Assembler* assembler = GetAssembler();
7617 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7618 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7619 // byte values.
7620 assembler->Align(4, 0);
7621 constant_area_start_ = assembler->CodeSize();
7622
7623 // Populate any jump tables.
7624 for (auto jump_table : fixups_to_jump_tables_) {
7625 jump_table->CreateJumpTable();
7626 }
7627
7628 // And now add the constant area to the generated code.
7629 assembler->AddConstantArea();
7630 }
7631
7632 // And finish up.
7633 CodeGenerator::Finalize(allocator);
7634}
7635
Mark Mendell0616ae02015-04-17 12:49:27 -04007636Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7637 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7638 return Address(reg, kDummy32BitOffset, fixup);
7639}
7640
7641Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7642 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7643 return Address(reg, kDummy32BitOffset, fixup);
7644}
7645
7646Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7647 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7648 return Address(reg, kDummy32BitOffset, fixup);
7649}
7650
7651Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7652 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7653 return Address(reg, kDummy32BitOffset, fixup);
7654}
7655
Aart Bika19616e2016-02-01 18:57:58 -08007656void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7657 if (value == 0) {
7658 __ xorl(dest, dest);
7659 } else {
7660 __ movl(dest, Immediate(value));
7661 }
7662}
7663
7664void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7665 if (value == 0) {
7666 __ testl(dest, dest);
7667 } else {
7668 __ cmpl(dest, Immediate(value));
7669 }
7670}
7671
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007672void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7673 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007674 GenerateIntCompare(lhs_reg, rhs);
7675}
7676
7677void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007678 if (rhs.IsConstant()) {
7679 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007680 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007681 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007682 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007683 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007684 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007685 }
7686}
7687
7688Address CodeGeneratorX86::ArrayAddress(Register obj,
7689 Location index,
7690 ScaleFactor scale,
7691 uint32_t data_offset) {
7692 return index.IsConstant() ?
7693 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7694 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7695}
7696
Mark Mendell805b3b52015-09-18 14:10:29 -04007697Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7698 Register reg,
7699 Register value) {
7700 // Create a fixup to be used to create and address the jump table.
7701 JumpTableRIPFixup* table_fixup =
7702 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7703
7704 // We have to populate the jump tables.
7705 fixups_to_jump_tables_.push_back(table_fixup);
7706
7707 // We want a scaled address, as we are extracting the correct offset from the table.
7708 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7709}
7710
Andreas Gampe85b62f22015-09-09 13:15:38 -07007711// TODO: target as memory.
7712void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7713 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007714 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007715 return;
7716 }
7717
7718 DCHECK_NE(type, Primitive::kPrimVoid);
7719
7720 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7721 if (target.Equals(return_loc)) {
7722 return;
7723 }
7724
7725 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7726 // with the else branch.
7727 if (type == Primitive::kPrimLong) {
7728 HParallelMove parallel_move(GetGraph()->GetArena());
7729 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7730 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7731 GetMoveResolver()->EmitNativeCode(&parallel_move);
7732 } else {
7733 // Let the parallel move resolver take care of all of this.
7734 HParallelMove parallel_move(GetGraph()->GetArena());
7735 parallel_move.AddMove(return_loc, target, type, nullptr);
7736 GetMoveResolver()->EmitNativeCode(&parallel_move);
7737 }
7738}
7739
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007740void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7741 const uint8_t* roots_data,
7742 const PatchInfo<Label>& info,
7743 uint64_t index_in_table) const {
7744 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7745 uintptr_t address =
7746 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7747 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7748 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7749 dchecked_integral_cast<uint32_t>(address);
7750}
7751
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007752void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7753 for (const PatchInfo<Label>& info : jit_string_patches_) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007754 const auto& it = jit_string_roots_.find(
7755 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007756 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007757 PatchJitRootUse(code, roots_data, info, it->second);
7758 }
7759
7760 for (const PatchInfo<Label>& info : jit_class_patches_) {
7761 const auto& it = jit_class_roots_.find(
7762 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7763 DCHECK(it != jit_class_roots_.end());
7764 PatchJitRootUse(code, roots_data, info, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007765 }
7766}
7767
Roland Levillain4d027112015-07-01 15:41:14 +01007768#undef __
7769
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007770} // namespace x86
7771} // namespace art