blob: 51e902a824d5c3a5c98803dc01ffe1285fe35d81 [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;
228 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
229 __ 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 Marko5233f932015-09-29 19:01:15 +01001009 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -04001010 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001011 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001012 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1013 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001014 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001015 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001016 constant_area_start_(-1),
1017 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1018 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001019 // Use a fake return address register to mimic Quick.
1020 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001021}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001022
David Brazdil58282f42016-01-14 12:45:10 +00001023void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001024 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001025 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001026}
1027
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001028InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001029 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001030 assembler_(codegen->GetAssembler()),
1031 codegen_(codegen) {}
1032
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001033static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001034 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001035}
1036
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001037void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001038 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001039 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001040 bool skip_overflow_check =
1041 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001042 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001043
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001044 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001045 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001046 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001047 }
1048
Mark Mendell5f874182015-03-04 15:42:45 -05001049 if (HasEmptyFrame()) {
1050 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001051 }
Mark Mendell5f874182015-03-04 15:42:45 -05001052
1053 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1054 Register reg = kCoreCalleeSaves[i];
1055 if (allocated_registers_.ContainsCoreRegister(reg)) {
1056 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001057 __ cfi().AdjustCFAOffset(kX86WordSize);
1058 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001059 }
1060 }
1061
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001062 int adjust = GetFrameSize() - FrameEntrySpillSize();
1063 __ subl(ESP, Immediate(adjust));
1064 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001065 // Save the current method if we need it. Note that we do not
1066 // do this in HCurrentMethod, as the instruction might have been removed
1067 // in the SSA graph.
1068 if (RequiresCurrentMethod()) {
1069 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1070 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001071}
1072
1073void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001074 __ cfi().RememberState();
1075 if (!HasEmptyFrame()) {
1076 int adjust = GetFrameSize() - FrameEntrySpillSize();
1077 __ addl(ESP, Immediate(adjust));
1078 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001079
David Srbeckyc34dc932015-04-12 09:27:43 +01001080 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1081 Register reg = kCoreCalleeSaves[i];
1082 if (allocated_registers_.ContainsCoreRegister(reg)) {
1083 __ popl(reg);
1084 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1085 __ cfi().Restore(DWARFReg(reg));
1086 }
Mark Mendell5f874182015-03-04 15:42:45 -05001087 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001088 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001089 __ ret();
1090 __ cfi().RestoreState();
1091 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001092}
1093
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001094void CodeGeneratorX86::Bind(HBasicBlock* block) {
1095 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001096}
1097
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001098Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1099 switch (type) {
1100 case Primitive::kPrimBoolean:
1101 case Primitive::kPrimByte:
1102 case Primitive::kPrimChar:
1103 case Primitive::kPrimShort:
1104 case Primitive::kPrimInt:
1105 case Primitive::kPrimNot:
1106 return Location::RegisterLocation(EAX);
1107
1108 case Primitive::kPrimLong:
1109 return Location::RegisterPairLocation(EAX, EDX);
1110
1111 case Primitive::kPrimVoid:
1112 return Location::NoLocation();
1113
1114 case Primitive::kPrimDouble:
1115 case Primitive::kPrimFloat:
1116 return Location::FpuRegisterLocation(XMM0);
1117 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001118
1119 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001120}
1121
1122Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1123 return Location::RegisterLocation(kMethodRegisterArgument);
1124}
1125
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001126Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001127 switch (type) {
1128 case Primitive::kPrimBoolean:
1129 case Primitive::kPrimByte:
1130 case Primitive::kPrimChar:
1131 case Primitive::kPrimShort:
1132 case Primitive::kPrimInt:
1133 case Primitive::kPrimNot: {
1134 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001135 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001136 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001137 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001138 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001139 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001140 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001141 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001142
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001143 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001144 uint32_t index = gp_index_;
1145 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001146 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001147 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001148 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1149 calling_convention.GetRegisterPairAt(index));
1150 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001151 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001152 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1153 }
1154 }
1155
1156 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001157 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001158 stack_index_++;
1159 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1160 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1161 } else {
1162 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1163 }
1164 }
1165
1166 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001167 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001168 stack_index_ += 2;
1169 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1170 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1171 } else {
1172 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001173 }
1174 }
1175
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001176 case Primitive::kPrimVoid:
1177 LOG(FATAL) << "Unexpected parameter type " << type;
1178 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001179 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001180 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001181}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001182
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001183void CodeGeneratorX86::Move32(Location destination, Location source) {
1184 if (source.Equals(destination)) {
1185 return;
1186 }
1187 if (destination.IsRegister()) {
1188 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001189 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001190 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001191 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001192 } else {
1193 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001194 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001195 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001196 } else if (destination.IsFpuRegister()) {
1197 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001198 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001199 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001200 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001201 } else {
1202 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001203 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001204 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001205 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001206 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001207 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001208 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001209 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001210 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001211 } else if (source.IsConstant()) {
1212 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001213 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001214 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001215 } else {
1216 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001217 __ pushl(Address(ESP, source.GetStackIndex()));
1218 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001219 }
1220 }
1221}
1222
1223void CodeGeneratorX86::Move64(Location destination, Location source) {
1224 if (source.Equals(destination)) {
1225 return;
1226 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001227 if (destination.IsRegisterPair()) {
1228 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001229 EmitParallelMoves(
1230 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1231 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001232 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001233 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001234 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1235 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001236 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001237 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1238 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1239 __ psrlq(src_reg, Immediate(32));
1240 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001241 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001242 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001243 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001244 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1245 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001246 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1247 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001249 if (source.IsFpuRegister()) {
1250 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1251 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001252 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001253 } else if (source.IsRegisterPair()) {
1254 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1255 // Create stack space for 2 elements.
1256 __ subl(ESP, Immediate(2 * elem_size));
1257 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1258 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1259 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1260 // And remove the temporary stack space we allocated.
1261 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001262 } else {
1263 LOG(FATAL) << "Unimplemented";
1264 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001265 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001266 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001267 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001268 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001269 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001270 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001271 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001272 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001273 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001274 } else if (source.IsConstant()) {
1275 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001276 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1277 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001278 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001279 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1280 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001281 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001282 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001283 EmitParallelMoves(
1284 Location::StackSlot(source.GetStackIndex()),
1285 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001286 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001287 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001288 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1289 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001290 }
1291 }
1292}
1293
Calin Juravle175dc732015-08-25 15:42:32 +01001294void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1295 DCHECK(location.IsRegister());
1296 __ movl(location.AsRegister<Register>(), Immediate(value));
1297}
1298
Calin Juravlee460d1d2015-09-29 04:52:17 +01001299void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001300 HParallelMove move(GetGraph()->GetArena());
1301 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1302 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1303 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001304 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001305 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001306 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001307 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001308}
1309
1310void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1311 if (location.IsRegister()) {
1312 locations->AddTemp(location);
1313 } else if (location.IsRegisterPair()) {
1314 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1315 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1316 } else {
1317 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1318 }
1319}
1320
David Brazdilfc6a86a2015-06-26 10:33:45 +00001321void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001322 DCHECK(!successor->IsExitBlock());
1323
1324 HBasicBlock* block = got->GetBlock();
1325 HInstruction* previous = got->GetPrevious();
1326
1327 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001328 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001329 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1330 return;
1331 }
1332
1333 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1334 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1335 }
1336 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001337 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001338 }
1339}
1340
David Brazdilfc6a86a2015-06-26 10:33:45 +00001341void LocationsBuilderX86::VisitGoto(HGoto* got) {
1342 got->SetLocations(nullptr);
1343}
1344
1345void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1346 HandleGoto(got, got->GetSuccessor());
1347}
1348
1349void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1350 try_boundary->SetLocations(nullptr);
1351}
1352
1353void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1354 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1355 if (!successor->IsExitBlock()) {
1356 HandleGoto(try_boundary, successor);
1357 }
1358}
1359
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001360void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001361 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001362}
1363
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001364void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001365}
1366
Mark Mendell152408f2015-12-31 12:28:50 -05001367template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001368void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001369 LabelType* true_label,
1370 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001371 if (cond->IsFPConditionTrueIfNaN()) {
1372 __ j(kUnordered, true_label);
1373 } else if (cond->IsFPConditionFalseIfNaN()) {
1374 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001375 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001376 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001377}
1378
Mark Mendell152408f2015-12-31 12:28:50 -05001379template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001380void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001381 LabelType* true_label,
1382 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001383 LocationSummary* locations = cond->GetLocations();
1384 Location left = locations->InAt(0);
1385 Location right = locations->InAt(1);
1386 IfCondition if_cond = cond->GetCondition();
1387
Mark Mendellc4701932015-04-10 13:18:51 -04001388 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001389 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001390 IfCondition true_high_cond = if_cond;
1391 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001392 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001393
1394 // Set the conditions for the test, remembering that == needs to be
1395 // decided using the low words.
1396 switch (if_cond) {
1397 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001398 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001399 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001400 break;
1401 case kCondLT:
1402 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001403 break;
1404 case kCondLE:
1405 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001406 break;
1407 case kCondGT:
1408 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001409 break;
1410 case kCondGE:
1411 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001412 break;
Aart Bike9f37602015-10-09 11:15:55 -07001413 case kCondB:
1414 false_high_cond = kCondA;
1415 break;
1416 case kCondBE:
1417 true_high_cond = kCondB;
1418 break;
1419 case kCondA:
1420 false_high_cond = kCondB;
1421 break;
1422 case kCondAE:
1423 true_high_cond = kCondA;
1424 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001425 }
1426
1427 if (right.IsConstant()) {
1428 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001429 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001430 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001431
Aart Bika19616e2016-02-01 18:57:58 -08001432 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001433 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001434 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001435 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001436 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001437 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001438 __ j(X86Condition(true_high_cond), true_label);
1439 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001440 }
1441 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001442 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001443 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001444 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001445 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001446
1447 __ cmpl(left_high, right_high);
1448 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001449 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001450 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001451 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001452 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001453 __ j(X86Condition(true_high_cond), true_label);
1454 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001455 }
1456 // Must be equal high, so compare the lows.
1457 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001458 } else {
1459 DCHECK(right.IsDoubleStackSlot());
1460 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1461 if (if_cond == kCondNE) {
1462 __ j(X86Condition(true_high_cond), true_label);
1463 } else if (if_cond == kCondEQ) {
1464 __ j(X86Condition(false_high_cond), false_label);
1465 } else {
1466 __ j(X86Condition(true_high_cond), true_label);
1467 __ j(X86Condition(false_high_cond), false_label);
1468 }
1469 // Must be equal high, so compare the lows.
1470 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001471 }
1472 // The last comparison might be unsigned.
1473 __ j(final_condition, true_label);
1474}
1475
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001476void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1477 Location rhs,
1478 HInstruction* insn,
1479 bool is_double) {
1480 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1481 if (is_double) {
1482 if (rhs.IsFpuRegister()) {
1483 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1484 } else if (const_area != nullptr) {
1485 DCHECK(const_area->IsEmittedAtUseSite());
1486 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1487 codegen_->LiteralDoubleAddress(
1488 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1489 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1490 } else {
1491 DCHECK(rhs.IsDoubleStackSlot());
1492 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1493 }
1494 } else {
1495 if (rhs.IsFpuRegister()) {
1496 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1497 } else if (const_area != nullptr) {
1498 DCHECK(const_area->IsEmittedAtUseSite());
1499 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1500 codegen_->LiteralFloatAddress(
1501 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1502 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1503 } else {
1504 DCHECK(rhs.IsStackSlot());
1505 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1506 }
1507 }
1508}
1509
Mark Mendell152408f2015-12-31 12:28:50 -05001510template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001511void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001512 LabelType* true_target_in,
1513 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001514 // Generated branching requires both targets to be explicit. If either of the
1515 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001516 LabelType fallthrough_target;
1517 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1518 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001519
Mark Mendellc4701932015-04-10 13:18:51 -04001520 LocationSummary* locations = condition->GetLocations();
1521 Location left = locations->InAt(0);
1522 Location right = locations->InAt(1);
1523
Mark Mendellc4701932015-04-10 13:18:51 -04001524 Primitive::Type type = condition->InputAt(0)->GetType();
1525 switch (type) {
1526 case Primitive::kPrimLong:
1527 GenerateLongComparesAndJumps(condition, true_target, false_target);
1528 break;
1529 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001530 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001531 GenerateFPJumps(condition, true_target, false_target);
1532 break;
1533 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001534 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001535 GenerateFPJumps(condition, true_target, false_target);
1536 break;
1537 default:
1538 LOG(FATAL) << "Unexpected compare type " << type;
1539 }
1540
David Brazdil0debae72015-11-12 18:37:00 +00001541 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001542 __ jmp(false_target);
1543 }
David Brazdil0debae72015-11-12 18:37:00 +00001544
1545 if (fallthrough_target.IsLinked()) {
1546 __ Bind(&fallthrough_target);
1547 }
Mark Mendellc4701932015-04-10 13:18:51 -04001548}
1549
David Brazdil0debae72015-11-12 18:37:00 +00001550static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1551 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1552 // are set only strictly before `branch`. We can't use the eflags on long/FP
1553 // conditions if they are materialized due to the complex branching.
1554 return cond->IsCondition() &&
1555 cond->GetNext() == branch &&
1556 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1557 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1558}
1559
Mark Mendell152408f2015-12-31 12:28:50 -05001560template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001561void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001562 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001563 LabelType* true_target,
1564 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001565 HInstruction* cond = instruction->InputAt(condition_input_index);
1566
1567 if (true_target == nullptr && false_target == nullptr) {
1568 // Nothing to do. The code always falls through.
1569 return;
1570 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001571 // Constant condition, statically compared against "true" (integer value 1).
1572 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001573 if (true_target != nullptr) {
1574 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001575 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001576 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001577 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001578 if (false_target != nullptr) {
1579 __ jmp(false_target);
1580 }
1581 }
1582 return;
1583 }
1584
1585 // The following code generates these patterns:
1586 // (1) true_target == nullptr && false_target != nullptr
1587 // - opposite condition true => branch to false_target
1588 // (2) true_target != nullptr && false_target == nullptr
1589 // - condition true => branch to true_target
1590 // (3) true_target != nullptr && false_target != nullptr
1591 // - condition true => branch to true_target
1592 // - branch to false_target
1593 if (IsBooleanValueOrMaterializedCondition(cond)) {
1594 if (AreEflagsSetFrom(cond, instruction)) {
1595 if (true_target == nullptr) {
1596 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1597 } else {
1598 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1599 }
1600 } else {
1601 // Materialized condition, compare against 0.
1602 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1603 if (lhs.IsRegister()) {
1604 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1605 } else {
1606 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1607 }
1608 if (true_target == nullptr) {
1609 __ j(kEqual, false_target);
1610 } else {
1611 __ j(kNotEqual, true_target);
1612 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001613 }
1614 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001615 // Condition has not been materialized, use its inputs as the comparison and
1616 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001617 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001618
1619 // If this is a long or FP comparison that has been folded into
1620 // the HCondition, generate the comparison directly.
1621 Primitive::Type type = condition->InputAt(0)->GetType();
1622 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1623 GenerateCompareTestAndBranch(condition, true_target, false_target);
1624 return;
1625 }
1626
1627 Location lhs = condition->GetLocations()->InAt(0);
1628 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001629 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001630 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001631 if (true_target == nullptr) {
1632 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1633 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001634 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001635 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001636 }
David Brazdil0debae72015-11-12 18:37:00 +00001637
1638 // If neither branch falls through (case 3), the conditional branch to `true_target`
1639 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1640 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001641 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001642 }
1643}
1644
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001645void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001646 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1647 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001648 locations->SetInAt(0, Location::Any());
1649 }
1650}
1651
1652void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001653 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1654 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1655 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1656 nullptr : codegen_->GetLabelOf(true_successor);
1657 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1658 nullptr : codegen_->GetLabelOf(false_successor);
1659 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001660}
1661
1662void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1663 LocationSummary* locations = new (GetGraph()->GetArena())
1664 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001665 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001666 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001667 locations->SetInAt(0, Location::Any());
1668 }
1669}
1670
1671void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001672 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001673 GenerateTestAndBranch<Label>(deoptimize,
1674 /* condition_input_index */ 0,
1675 slow_path->GetEntryLabel(),
1676 /* false_target */ nullptr);
1677}
1678
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001679static bool SelectCanUseCMOV(HSelect* select) {
1680 // There are no conditional move instructions for XMMs.
1681 if (Primitive::IsFloatingPointType(select->GetType())) {
1682 return false;
1683 }
1684
1685 // A FP condition doesn't generate the single CC that we need.
1686 // In 32 bit mode, a long condition doesn't generate a single CC either.
1687 HInstruction* condition = select->GetCondition();
1688 if (condition->IsCondition()) {
1689 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1690 if (compare_type == Primitive::kPrimLong ||
1691 Primitive::IsFloatingPointType(compare_type)) {
1692 return false;
1693 }
1694 }
1695
1696 // We can generate a CMOV for this Select.
1697 return true;
1698}
1699
David Brazdil74eb1b22015-12-14 11:44:01 +00001700void LocationsBuilderX86::VisitSelect(HSelect* select) {
1701 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001702 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001703 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001704 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001705 } else {
1706 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001707 if (SelectCanUseCMOV(select)) {
1708 if (select->InputAt(1)->IsConstant()) {
1709 // Cmov can't handle a constant value.
1710 locations->SetInAt(1, Location::RequiresRegister());
1711 } else {
1712 locations->SetInAt(1, Location::Any());
1713 }
1714 } else {
1715 locations->SetInAt(1, Location::Any());
1716 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001717 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001718 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1719 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001720 }
1721 locations->SetOut(Location::SameAsFirstInput());
1722}
1723
1724void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1725 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001726 DCHECK(locations->InAt(0).Equals(locations->Out()));
1727 if (SelectCanUseCMOV(select)) {
1728 // If both the condition and the source types are integer, we can generate
1729 // a CMOV to implement Select.
1730
1731 HInstruction* select_condition = select->GetCondition();
1732 Condition cond = kNotEqual;
1733
1734 // Figure out how to test the 'condition'.
1735 if (select_condition->IsCondition()) {
1736 HCondition* condition = select_condition->AsCondition();
1737 if (!condition->IsEmittedAtUseSite()) {
1738 // This was a previously materialized condition.
1739 // Can we use the existing condition code?
1740 if (AreEflagsSetFrom(condition, select)) {
1741 // Materialization was the previous instruction. Condition codes are right.
1742 cond = X86Condition(condition->GetCondition());
1743 } else {
1744 // No, we have to recreate the condition code.
1745 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1746 __ testl(cond_reg, cond_reg);
1747 }
1748 } else {
1749 // We can't handle FP or long here.
1750 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1751 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1752 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001753 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001754 cond = X86Condition(condition->GetCondition());
1755 }
1756 } else {
1757 // Must be a boolean condition, which needs to be compared to 0.
1758 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1759 __ testl(cond_reg, cond_reg);
1760 }
1761
1762 // If the condition is true, overwrite the output, which already contains false.
1763 Location false_loc = locations->InAt(0);
1764 Location true_loc = locations->InAt(1);
1765 if (select->GetType() == Primitive::kPrimLong) {
1766 // 64 bit conditional move.
1767 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1768 Register false_low = false_loc.AsRegisterPairLow<Register>();
1769 if (true_loc.IsRegisterPair()) {
1770 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1771 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1772 } else {
1773 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1774 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1775 }
1776 } else {
1777 // 32 bit conditional move.
1778 Register false_reg = false_loc.AsRegister<Register>();
1779 if (true_loc.IsRegister()) {
1780 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1781 } else {
1782 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1783 }
1784 }
1785 } else {
1786 NearLabel false_target;
1787 GenerateTestAndBranch<NearLabel>(
1788 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1789 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1790 __ Bind(&false_target);
1791 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001792}
1793
David Srbecky0cf44932015-12-09 14:09:59 +00001794void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1795 new (GetGraph()->GetArena()) LocationSummary(info);
1796}
1797
David Srbeckyd28f4a02016-03-14 17:14:24 +00001798void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1799 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001800}
1801
1802void CodeGeneratorX86::GenerateNop() {
1803 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001804}
1805
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001806void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001807 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001808 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001809 // Handle the long/FP comparisons made in instruction simplification.
1810 switch (cond->InputAt(0)->GetType()) {
1811 case Primitive::kPrimLong: {
1812 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001813 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001814 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001815 locations->SetOut(Location::RequiresRegister());
1816 }
1817 break;
1818 }
1819 case Primitive::kPrimFloat:
1820 case Primitive::kPrimDouble: {
1821 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001822 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1823 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1824 } else if (cond->InputAt(1)->IsConstant()) {
1825 locations->SetInAt(1, Location::RequiresFpuRegister());
1826 } else {
1827 locations->SetInAt(1, Location::Any());
1828 }
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 default:
1835 locations->SetInAt(0, Location::RequiresRegister());
1836 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001837 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001838 // We need a byte register.
1839 locations->SetOut(Location::RegisterLocation(ECX));
1840 }
1841 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001842 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001843}
1844
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001845void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001846 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001847 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001848 }
Mark Mendellc4701932015-04-10 13:18:51 -04001849
1850 LocationSummary* locations = cond->GetLocations();
1851 Location lhs = locations->InAt(0);
1852 Location rhs = locations->InAt(1);
1853 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001854 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001855
1856 switch (cond->InputAt(0)->GetType()) {
1857 default: {
1858 // Integer case.
1859
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001860 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001861 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001862 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001863 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001864 return;
1865 }
1866 case Primitive::kPrimLong:
1867 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1868 break;
1869 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001870 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001871 GenerateFPJumps(cond, &true_label, &false_label);
1872 break;
1873 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001874 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001875 GenerateFPJumps(cond, &true_label, &false_label);
1876 break;
1877 }
1878
1879 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001880 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001881
Roland Levillain4fa13f62015-07-06 18:11:54 +01001882 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001883 __ Bind(&false_label);
1884 __ xorl(reg, reg);
1885 __ jmp(&done_label);
1886
Roland Levillain4fa13f62015-07-06 18:11:54 +01001887 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001888 __ Bind(&true_label);
1889 __ movl(reg, Immediate(1));
1890 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001891}
1892
1893void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001894 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001895}
1896
1897void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001898 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001899}
1900
1901void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001902 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001903}
1904
1905void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001906 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001907}
1908
1909void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001910 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001911}
1912
1913void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001914 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001915}
1916
1917void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001918 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001919}
1920
1921void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001922 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001923}
1924
1925void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001926 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001927}
1928
1929void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001930 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001931}
1932
1933void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001934 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001935}
1936
1937void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001938 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001939}
1940
Aart Bike9f37602015-10-09 11:15:55 -07001941void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001942 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001943}
1944
1945void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001946 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001947}
1948
1949void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001950 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001951}
1952
1953void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001954 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001955}
1956
1957void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001958 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001959}
1960
1961void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001962 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001963}
1964
1965void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001966 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001967}
1968
1969void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001970 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001971}
1972
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001973void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001974 LocationSummary* locations =
1975 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001976 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001977}
1978
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001979void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001980 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001981}
1982
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001983void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1984 LocationSummary* locations =
1985 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1986 locations->SetOut(Location::ConstantLocation(constant));
1987}
1988
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001989void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001990 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001991}
1992
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001993void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001994 LocationSummary* locations =
1995 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001996 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001997}
1998
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001999void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002000 // Will be generated at use site.
2001}
2002
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002003void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2004 LocationSummary* locations =
2005 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2006 locations->SetOut(Location::ConstantLocation(constant));
2007}
2008
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002009void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002010 // Will be generated at use site.
2011}
2012
2013void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2014 LocationSummary* locations =
2015 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2016 locations->SetOut(Location::ConstantLocation(constant));
2017}
2018
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002019void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002020 // Will be generated at use site.
2021}
2022
Calin Juravle27df7582015-04-17 19:12:31 +01002023void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2024 memory_barrier->SetLocations(nullptr);
2025}
2026
2027void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002028 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002029}
2030
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002031void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002032 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002033}
2034
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002035void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002036 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002037}
2038
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002039void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002040 LocationSummary* locations =
2041 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002042 switch (ret->InputAt(0)->GetType()) {
2043 case Primitive::kPrimBoolean:
2044 case Primitive::kPrimByte:
2045 case Primitive::kPrimChar:
2046 case Primitive::kPrimShort:
2047 case Primitive::kPrimInt:
2048 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002049 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002050 break;
2051
2052 case Primitive::kPrimLong:
2053 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002054 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002055 break;
2056
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002057 case Primitive::kPrimFloat:
2058 case Primitive::kPrimDouble:
2059 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002060 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002061 break;
2062
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002063 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002064 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002065 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002066}
2067
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002068void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002069 if (kIsDebugBuild) {
2070 switch (ret->InputAt(0)->GetType()) {
2071 case Primitive::kPrimBoolean:
2072 case Primitive::kPrimByte:
2073 case Primitive::kPrimChar:
2074 case Primitive::kPrimShort:
2075 case Primitive::kPrimInt:
2076 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002077 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002078 break;
2079
2080 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002081 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2082 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002083 break;
2084
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002085 case Primitive::kPrimFloat:
2086 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002087 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002088 break;
2089
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002090 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002091 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002092 }
2093 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002094 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002095}
2096
Calin Juravle175dc732015-08-25 15:42:32 +01002097void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2098 // The trampoline uses the same calling convention as dex calling conventions,
2099 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2100 // the method_idx.
2101 HandleInvoke(invoke);
2102}
2103
2104void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2105 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2106}
2107
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002108void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002109 // Explicit clinit checks triggered by static invokes must have been pruned by
2110 // art::PrepareForRegisterAllocation.
2111 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002112
Mark Mendellfb8d2792015-03-31 22:16:59 -04002113 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002114 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002115 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002116 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002117 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002118 return;
2119 }
2120
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002121 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002122
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002123 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2124 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002125 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002126 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002127}
2128
Mark Mendell09ed1a32015-03-25 08:30:06 -04002129static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2130 if (invoke->GetLocations()->Intrinsified()) {
2131 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2132 intrinsic.Dispatch(invoke);
2133 return true;
2134 }
2135 return false;
2136}
2137
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002138void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002139 // Explicit clinit checks triggered by static invokes must have been pruned by
2140 // art::PrepareForRegisterAllocation.
2141 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002142
Mark Mendell09ed1a32015-03-25 08:30:06 -04002143 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2144 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002145 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002146
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002147 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002148 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002149 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002150 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002151}
2152
2153void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002154 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2155 if (intrinsic.TryDispatch(invoke)) {
2156 return;
2157 }
2158
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002159 HandleInvoke(invoke);
2160}
2161
2162void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002163 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002164 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002165}
2166
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002167void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002168 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2169 return;
2170 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002171
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002172 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002173 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002174 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002175}
2176
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002177void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002178 // This call to HandleInvoke allocates a temporary (core) register
2179 // which is also used to transfer the hidden argument from FP to
2180 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002181 HandleInvoke(invoke);
2182 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002183 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002184}
2185
2186void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2187 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002188 LocationSummary* locations = invoke->GetLocations();
2189 Register temp = locations->GetTemp(0).AsRegister<Register>();
2190 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002191 Location receiver = locations->InAt(0);
2192 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2193
Roland Levillain0d5a2812015-11-13 10:07:31 +00002194 // Set the hidden argument. This is safe to do this here, as XMM7
2195 // won't be modified thereafter, before the `call` instruction.
2196 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002197 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002198 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002199
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002200 if (receiver.IsStackSlot()) {
2201 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002202 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002203 __ movl(temp, Address(temp, class_offset));
2204 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002205 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002206 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002207 }
Roland Levillain4d027112015-07-01 15:41:14 +01002208 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002209 // Instead of simply (possibly) unpoisoning `temp` here, we should
2210 // emit a read barrier for the previous class reference load.
2211 // However this is not required in practice, as this is an
2212 // intermediate/temporary reference and because the current
2213 // concurrent copying collector keeps the from-space memory
2214 // intact/accessible until the end of the marking phase (the
2215 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002216 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002217 // temp = temp->GetAddressOfIMT()
2218 __ movl(temp,
2219 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002220 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002221 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002222 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002223 __ movl(temp, Address(temp, method_offset));
2224 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002225 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002226 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002227
2228 DCHECK(!codegen_->IsLeafMethod());
2229 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2230}
2231
Roland Levillain88cb1752014-10-20 16:36:47 +01002232void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2233 LocationSummary* locations =
2234 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2235 switch (neg->GetResultType()) {
2236 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002237 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002238 locations->SetInAt(0, Location::RequiresRegister());
2239 locations->SetOut(Location::SameAsFirstInput());
2240 break;
2241
Roland Levillain88cb1752014-10-20 16:36:47 +01002242 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002243 locations->SetInAt(0, Location::RequiresFpuRegister());
2244 locations->SetOut(Location::SameAsFirstInput());
2245 locations->AddTemp(Location::RequiresRegister());
2246 locations->AddTemp(Location::RequiresFpuRegister());
2247 break;
2248
Roland Levillain88cb1752014-10-20 16:36:47 +01002249 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002250 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002251 locations->SetOut(Location::SameAsFirstInput());
2252 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002253 break;
2254
2255 default:
2256 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2257 }
2258}
2259
2260void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2261 LocationSummary* locations = neg->GetLocations();
2262 Location out = locations->Out();
2263 Location in = locations->InAt(0);
2264 switch (neg->GetResultType()) {
2265 case Primitive::kPrimInt:
2266 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002267 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002268 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002269 break;
2270
2271 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002272 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002273 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002274 __ negl(out.AsRegisterPairLow<Register>());
2275 // Negation is similar to subtraction from zero. The least
2276 // significant byte triggers a borrow when it is different from
2277 // zero; to take it into account, add 1 to the most significant
2278 // byte if the carry flag (CF) is set to 1 after the first NEGL
2279 // operation.
2280 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2281 __ negl(out.AsRegisterPairHigh<Register>());
2282 break;
2283
Roland Levillain5368c212014-11-27 15:03:41 +00002284 case Primitive::kPrimFloat: {
2285 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002286 Register constant = locations->GetTemp(0).AsRegister<Register>();
2287 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002288 // Implement float negation with an exclusive or with value
2289 // 0x80000000 (mask for bit 31, representing the sign of a
2290 // single-precision floating-point number).
2291 __ movl(constant, Immediate(INT32_C(0x80000000)));
2292 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002293 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002294 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002295 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002296
Roland Levillain5368c212014-11-27 15:03:41 +00002297 case Primitive::kPrimDouble: {
2298 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002299 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002300 // Implement double negation with an exclusive or with value
2301 // 0x8000000000000000 (mask for bit 63, representing the sign of
2302 // a double-precision floating-point number).
2303 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002304 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002305 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002306 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002307
2308 default:
2309 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2310 }
2311}
2312
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002313void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2314 LocationSummary* locations =
2315 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2316 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2317 locations->SetInAt(0, Location::RequiresFpuRegister());
2318 locations->SetInAt(1, Location::RequiresRegister());
2319 locations->SetOut(Location::SameAsFirstInput());
2320 locations->AddTemp(Location::RequiresFpuRegister());
2321}
2322
2323void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2324 LocationSummary* locations = neg->GetLocations();
2325 Location out = locations->Out();
2326 DCHECK(locations->InAt(0).Equals(out));
2327
2328 Register constant_area = locations->InAt(1).AsRegister<Register>();
2329 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2330 if (neg->GetType() == Primitive::kPrimFloat) {
2331 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2332 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2333 } else {
2334 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2335 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2336 }
2337}
2338
Roland Levillaindff1f282014-11-05 14:15:05 +00002339void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002340 Primitive::Type result_type = conversion->GetResultType();
2341 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002342 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002343
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002344 // The float-to-long and double-to-long type conversions rely on a
2345 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002346 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002347 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2348 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002349 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002350 : LocationSummary::kNoCall;
2351 LocationSummary* locations =
2352 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2353
David Brazdilb2bd1c52015-03-25 11:17:37 +00002354 // The Java language does not allow treating boolean as an integral type but
2355 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002356
Roland Levillaindff1f282014-11-05 14:15:05 +00002357 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002358 case Primitive::kPrimByte:
2359 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002360 case Primitive::kPrimLong: {
2361 // Type conversion from long to byte is a result of code transformations.
2362 HInstruction* input = conversion->InputAt(0);
2363 Location input_location = input->IsConstant()
2364 ? Location::ConstantLocation(input->AsConstant())
2365 : Location::RegisterPairLocation(EAX, EDX);
2366 locations->SetInAt(0, input_location);
2367 // Make the output overlap to please the register allocator. This greatly simplifies
2368 // the validation of the linear scan implementation
2369 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2370 break;
2371 }
David Brazdil46e2a392015-03-16 17:31:52 +00002372 case Primitive::kPrimBoolean:
2373 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002374 case Primitive::kPrimShort:
2375 case Primitive::kPrimInt:
2376 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002377 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002378 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2379 // Make the output overlap to please the register allocator. This greatly simplifies
2380 // the validation of the linear scan implementation
2381 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002382 break;
2383
2384 default:
2385 LOG(FATAL) << "Unexpected type conversion from " << input_type
2386 << " to " << result_type;
2387 }
2388 break;
2389
Roland Levillain01a8d712014-11-14 16:27:39 +00002390 case Primitive::kPrimShort:
2391 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002392 case Primitive::kPrimLong:
2393 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002394 case Primitive::kPrimBoolean:
2395 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002396 case Primitive::kPrimByte:
2397 case Primitive::kPrimInt:
2398 case Primitive::kPrimChar:
2399 // Processing a Dex `int-to-short' instruction.
2400 locations->SetInAt(0, Location::Any());
2401 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2402 break;
2403
2404 default:
2405 LOG(FATAL) << "Unexpected type conversion from " << input_type
2406 << " to " << result_type;
2407 }
2408 break;
2409
Roland Levillain946e1432014-11-11 17:35:19 +00002410 case Primitive::kPrimInt:
2411 switch (input_type) {
2412 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002413 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002414 locations->SetInAt(0, Location::Any());
2415 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2416 break;
2417
2418 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002419 // Processing a Dex `float-to-int' instruction.
2420 locations->SetInAt(0, Location::RequiresFpuRegister());
2421 locations->SetOut(Location::RequiresRegister());
2422 locations->AddTemp(Location::RequiresFpuRegister());
2423 break;
2424
Roland Levillain946e1432014-11-11 17:35:19 +00002425 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002426 // Processing a Dex `double-to-int' instruction.
2427 locations->SetInAt(0, Location::RequiresFpuRegister());
2428 locations->SetOut(Location::RequiresRegister());
2429 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002430 break;
2431
2432 default:
2433 LOG(FATAL) << "Unexpected type conversion from " << input_type
2434 << " to " << result_type;
2435 }
2436 break;
2437
Roland Levillaindff1f282014-11-05 14:15:05 +00002438 case Primitive::kPrimLong:
2439 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002440 case Primitive::kPrimBoolean:
2441 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002442 case Primitive::kPrimByte:
2443 case Primitive::kPrimShort:
2444 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002445 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002446 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002447 locations->SetInAt(0, Location::RegisterLocation(EAX));
2448 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2449 break;
2450
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002451 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002452 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002453 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002454 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002455 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2456 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2457
Vladimir Marko949c91f2015-01-27 10:48:44 +00002458 // The runtime helper puts the result in EAX, EDX.
2459 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002460 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002461 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002462
2463 default:
2464 LOG(FATAL) << "Unexpected type conversion from " << input_type
2465 << " to " << result_type;
2466 }
2467 break;
2468
Roland Levillain981e4542014-11-14 11:47:14 +00002469 case Primitive::kPrimChar:
2470 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002471 case Primitive::kPrimLong:
2472 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002473 case Primitive::kPrimBoolean:
2474 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002475 case Primitive::kPrimByte:
2476 case Primitive::kPrimShort:
2477 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002478 // Processing a Dex `int-to-char' instruction.
2479 locations->SetInAt(0, Location::Any());
2480 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2481 break;
2482
2483 default:
2484 LOG(FATAL) << "Unexpected type conversion from " << input_type
2485 << " to " << result_type;
2486 }
2487 break;
2488
Roland Levillaindff1f282014-11-05 14:15:05 +00002489 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002490 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002491 case Primitive::kPrimBoolean:
2492 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002493 case Primitive::kPrimByte:
2494 case Primitive::kPrimShort:
2495 case Primitive::kPrimInt:
2496 case Primitive::kPrimChar:
2497 // Processing a Dex `int-to-float' instruction.
2498 locations->SetInAt(0, Location::RequiresRegister());
2499 locations->SetOut(Location::RequiresFpuRegister());
2500 break;
2501
2502 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002503 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002504 locations->SetInAt(0, Location::Any());
2505 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002506 break;
2507
Roland Levillaincff13742014-11-17 14:32:17 +00002508 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002509 // Processing a Dex `double-to-float' instruction.
2510 locations->SetInAt(0, Location::RequiresFpuRegister());
2511 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002512 break;
2513
2514 default:
2515 LOG(FATAL) << "Unexpected type conversion from " << input_type
2516 << " to " << result_type;
2517 };
2518 break;
2519
Roland Levillaindff1f282014-11-05 14:15:05 +00002520 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002521 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002522 case Primitive::kPrimBoolean:
2523 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002524 case Primitive::kPrimByte:
2525 case Primitive::kPrimShort:
2526 case Primitive::kPrimInt:
2527 case Primitive::kPrimChar:
2528 // Processing a Dex `int-to-double' instruction.
2529 locations->SetInAt(0, Location::RequiresRegister());
2530 locations->SetOut(Location::RequiresFpuRegister());
2531 break;
2532
2533 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002534 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002535 locations->SetInAt(0, Location::Any());
2536 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002537 break;
2538
Roland Levillaincff13742014-11-17 14:32:17 +00002539 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002540 // Processing a Dex `float-to-double' instruction.
2541 locations->SetInAt(0, Location::RequiresFpuRegister());
2542 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002543 break;
2544
2545 default:
2546 LOG(FATAL) << "Unexpected type conversion from " << input_type
2547 << " to " << result_type;
2548 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002549 break;
2550
2551 default:
2552 LOG(FATAL) << "Unexpected type conversion from " << input_type
2553 << " to " << result_type;
2554 }
2555}
2556
2557void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2558 LocationSummary* locations = conversion->GetLocations();
2559 Location out = locations->Out();
2560 Location in = locations->InAt(0);
2561 Primitive::Type result_type = conversion->GetResultType();
2562 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002563 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002564 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002565 case Primitive::kPrimByte:
2566 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002567 case Primitive::kPrimLong:
2568 // Type conversion from long to byte is a result of code transformations.
2569 if (in.IsRegisterPair()) {
2570 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2571 } else {
2572 DCHECK(in.GetConstant()->IsLongConstant());
2573 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2574 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2575 }
2576 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002577 case Primitive::kPrimBoolean:
2578 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002579 case Primitive::kPrimShort:
2580 case Primitive::kPrimInt:
2581 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002582 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002583 if (in.IsRegister()) {
2584 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002585 } else {
2586 DCHECK(in.GetConstant()->IsIntConstant());
2587 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2588 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2589 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002590 break;
2591
2592 default:
2593 LOG(FATAL) << "Unexpected type conversion from " << input_type
2594 << " to " << result_type;
2595 }
2596 break;
2597
Roland Levillain01a8d712014-11-14 16:27:39 +00002598 case Primitive::kPrimShort:
2599 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002600 case Primitive::kPrimLong:
2601 // Type conversion from long to short is a result of code transformations.
2602 if (in.IsRegisterPair()) {
2603 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2604 } else if (in.IsDoubleStackSlot()) {
2605 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2606 } else {
2607 DCHECK(in.GetConstant()->IsLongConstant());
2608 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2609 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2610 }
2611 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002612 case Primitive::kPrimBoolean:
2613 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002614 case Primitive::kPrimByte:
2615 case Primitive::kPrimInt:
2616 case Primitive::kPrimChar:
2617 // Processing a Dex `int-to-short' instruction.
2618 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002619 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002620 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002621 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002622 } else {
2623 DCHECK(in.GetConstant()->IsIntConstant());
2624 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002625 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002626 }
2627 break;
2628
2629 default:
2630 LOG(FATAL) << "Unexpected type conversion from " << input_type
2631 << " to " << result_type;
2632 }
2633 break;
2634
Roland Levillain946e1432014-11-11 17:35:19 +00002635 case Primitive::kPrimInt:
2636 switch (input_type) {
2637 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002638 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002639 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002640 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002641 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002642 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002643 } else {
2644 DCHECK(in.IsConstant());
2645 DCHECK(in.GetConstant()->IsLongConstant());
2646 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002647 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002648 }
2649 break;
2650
Roland Levillain3f8f9362014-12-02 17:45:01 +00002651 case Primitive::kPrimFloat: {
2652 // Processing a Dex `float-to-int' instruction.
2653 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2654 Register output = out.AsRegister<Register>();
2655 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002656 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002657
2658 __ movl(output, Immediate(kPrimIntMax));
2659 // temp = int-to-float(output)
2660 __ cvtsi2ss(temp, output);
2661 // if input >= temp goto done
2662 __ comiss(input, temp);
2663 __ j(kAboveEqual, &done);
2664 // if input == NaN goto nan
2665 __ j(kUnordered, &nan);
2666 // output = float-to-int-truncate(input)
2667 __ cvttss2si(output, input);
2668 __ jmp(&done);
2669 __ Bind(&nan);
2670 // output = 0
2671 __ xorl(output, output);
2672 __ Bind(&done);
2673 break;
2674 }
2675
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002676 case Primitive::kPrimDouble: {
2677 // Processing a Dex `double-to-int' instruction.
2678 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2679 Register output = out.AsRegister<Register>();
2680 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002681 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002682
2683 __ movl(output, Immediate(kPrimIntMax));
2684 // temp = int-to-double(output)
2685 __ cvtsi2sd(temp, output);
2686 // if input >= temp goto done
2687 __ comisd(input, temp);
2688 __ j(kAboveEqual, &done);
2689 // if input == NaN goto nan
2690 __ j(kUnordered, &nan);
2691 // output = double-to-int-truncate(input)
2692 __ cvttsd2si(output, input);
2693 __ jmp(&done);
2694 __ Bind(&nan);
2695 // output = 0
2696 __ xorl(output, output);
2697 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002698 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002699 }
Roland Levillain946e1432014-11-11 17:35:19 +00002700
2701 default:
2702 LOG(FATAL) << "Unexpected type conversion from " << input_type
2703 << " to " << result_type;
2704 }
2705 break;
2706
Roland Levillaindff1f282014-11-05 14:15:05 +00002707 case Primitive::kPrimLong:
2708 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002709 case Primitive::kPrimBoolean:
2710 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002711 case Primitive::kPrimByte:
2712 case Primitive::kPrimShort:
2713 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002714 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002715 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002716 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2717 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002718 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002719 __ cdq();
2720 break;
2721
2722 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002723 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002724 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002725 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002726 break;
2727
Roland Levillaindff1f282014-11-05 14:15:05 +00002728 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002729 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002730 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002731 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002732 break;
2733
2734 default:
2735 LOG(FATAL) << "Unexpected type conversion from " << input_type
2736 << " to " << result_type;
2737 }
2738 break;
2739
Roland Levillain981e4542014-11-14 11:47:14 +00002740 case Primitive::kPrimChar:
2741 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002742 case Primitive::kPrimLong:
2743 // Type conversion from long to short is a result of code transformations.
2744 if (in.IsRegisterPair()) {
2745 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2746 } else if (in.IsDoubleStackSlot()) {
2747 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2748 } else {
2749 DCHECK(in.GetConstant()->IsLongConstant());
2750 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2751 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2752 }
2753 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002754 case Primitive::kPrimBoolean:
2755 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002756 case Primitive::kPrimByte:
2757 case Primitive::kPrimShort:
2758 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002759 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2760 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002761 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002762 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002763 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002764 } else {
2765 DCHECK(in.GetConstant()->IsIntConstant());
2766 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002767 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002768 }
2769 break;
2770
2771 default:
2772 LOG(FATAL) << "Unexpected type conversion from " << input_type
2773 << " to " << result_type;
2774 }
2775 break;
2776
Roland Levillaindff1f282014-11-05 14:15:05 +00002777 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002778 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002779 case Primitive::kPrimBoolean:
2780 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002781 case Primitive::kPrimByte:
2782 case Primitive::kPrimShort:
2783 case Primitive::kPrimInt:
2784 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002785 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002786 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002787 break;
2788
Roland Levillain6d0e4832014-11-27 18:31:21 +00002789 case Primitive::kPrimLong: {
2790 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002791 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002792
Roland Levillain232ade02015-04-20 15:14:36 +01002793 // Create stack space for the call to
2794 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2795 // TODO: enhance register allocator to ask for stack temporaries.
2796 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2797 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2798 __ subl(ESP, Immediate(adjustment));
2799 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002800
Roland Levillain232ade02015-04-20 15:14:36 +01002801 // Load the value to the FP stack, using temporaries if needed.
2802 PushOntoFPStack(in, 0, adjustment, false, true);
2803
2804 if (out.IsStackSlot()) {
2805 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2806 } else {
2807 __ fstps(Address(ESP, 0));
2808 Location stack_temp = Location::StackSlot(0);
2809 codegen_->Move32(out, stack_temp);
2810 }
2811
2812 // Remove the temporary stack space we allocated.
2813 if (adjustment != 0) {
2814 __ addl(ESP, Immediate(adjustment));
2815 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002816 break;
2817 }
2818
Roland Levillaincff13742014-11-17 14:32:17 +00002819 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002820 // Processing a Dex `double-to-float' instruction.
2821 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002822 break;
2823
2824 default:
2825 LOG(FATAL) << "Unexpected type conversion from " << input_type
2826 << " to " << result_type;
2827 };
2828 break;
2829
Roland Levillaindff1f282014-11-05 14:15:05 +00002830 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002831 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002832 case Primitive::kPrimBoolean:
2833 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002834 case Primitive::kPrimByte:
2835 case Primitive::kPrimShort:
2836 case Primitive::kPrimInt:
2837 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002838 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002839 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002840 break;
2841
Roland Levillain647b9ed2014-11-27 12:06:00 +00002842 case Primitive::kPrimLong: {
2843 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002844 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002845
Roland Levillain232ade02015-04-20 15:14:36 +01002846 // Create stack space for the call to
2847 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2848 // TODO: enhance register allocator to ask for stack temporaries.
2849 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2850 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2851 __ subl(ESP, Immediate(adjustment));
2852 }
2853
2854 // Load the value to the FP stack, using temporaries if needed.
2855 PushOntoFPStack(in, 0, adjustment, false, true);
2856
2857 if (out.IsDoubleStackSlot()) {
2858 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2859 } else {
2860 __ fstpl(Address(ESP, 0));
2861 Location stack_temp = Location::DoubleStackSlot(0);
2862 codegen_->Move64(out, stack_temp);
2863 }
2864
2865 // Remove the temporary stack space we allocated.
2866 if (adjustment != 0) {
2867 __ addl(ESP, Immediate(adjustment));
2868 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002869 break;
2870 }
2871
Roland Levillaincff13742014-11-17 14:32:17 +00002872 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002873 // Processing a Dex `float-to-double' instruction.
2874 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002875 break;
2876
2877 default:
2878 LOG(FATAL) << "Unexpected type conversion from " << input_type
2879 << " to " << result_type;
2880 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002881 break;
2882
2883 default:
2884 LOG(FATAL) << "Unexpected type conversion from " << input_type
2885 << " to " << result_type;
2886 }
2887}
2888
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002889void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002890 LocationSummary* locations =
2891 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002892 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002893 case Primitive::kPrimInt: {
2894 locations->SetInAt(0, Location::RequiresRegister());
2895 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2896 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2897 break;
2898 }
2899
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002900 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002901 locations->SetInAt(0, Location::RequiresRegister());
2902 locations->SetInAt(1, Location::Any());
2903 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002904 break;
2905 }
2906
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002907 case Primitive::kPrimFloat:
2908 case Primitive::kPrimDouble: {
2909 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002910 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2911 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002912 } else if (add->InputAt(1)->IsConstant()) {
2913 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002914 } else {
2915 locations->SetInAt(1, Location::Any());
2916 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002917 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002918 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002919 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002920
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002921 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002922 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2923 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002924 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002925}
2926
2927void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2928 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002929 Location first = locations->InAt(0);
2930 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002931 Location out = locations->Out();
2932
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002933 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002934 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002935 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002936 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2937 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002938 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2939 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002940 } else {
2941 __ leal(out.AsRegister<Register>(), Address(
2942 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2943 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002944 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002945 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2946 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2947 __ addl(out.AsRegister<Register>(), Immediate(value));
2948 } else {
2949 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2950 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002951 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002952 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002953 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002954 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002955 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002956 }
2957
2958 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002959 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002960 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2961 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002962 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002963 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2964 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002965 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002966 } else {
2967 DCHECK(second.IsConstant()) << second;
2968 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2969 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2970 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002971 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002972 break;
2973 }
2974
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002975 case Primitive::kPrimFloat: {
2976 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002977 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002978 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2979 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002980 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002981 __ addss(first.AsFpuRegister<XmmRegister>(),
2982 codegen_->LiteralFloatAddress(
2983 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2984 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2985 } else {
2986 DCHECK(second.IsStackSlot());
2987 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002988 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002989 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002990 }
2991
2992 case Primitive::kPrimDouble: {
2993 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002994 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002995 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2996 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002997 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002998 __ addsd(first.AsFpuRegister<XmmRegister>(),
2999 codegen_->LiteralDoubleAddress(
3000 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3001 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3002 } else {
3003 DCHECK(second.IsDoubleStackSlot());
3004 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003005 }
3006 break;
3007 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003008
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003009 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003010 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003011 }
3012}
3013
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003014void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003015 LocationSummary* locations =
3016 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003017 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003018 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003019 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003020 locations->SetInAt(0, Location::RequiresRegister());
3021 locations->SetInAt(1, Location::Any());
3022 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003023 break;
3024 }
Calin Juravle11351682014-10-23 15:38:15 +01003025 case Primitive::kPrimFloat:
3026 case Primitive::kPrimDouble: {
3027 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003028 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3029 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003030 } else if (sub->InputAt(1)->IsConstant()) {
3031 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003032 } else {
3033 locations->SetInAt(1, Location::Any());
3034 }
Calin Juravle11351682014-10-23 15:38:15 +01003035 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003036 break;
Calin Juravle11351682014-10-23 15:38:15 +01003037 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003038
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003039 default:
Calin Juravle11351682014-10-23 15:38:15 +01003040 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003041 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003042}
3043
3044void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3045 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003046 Location first = locations->InAt(0);
3047 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003048 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003049 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003050 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003051 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003052 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003053 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003054 __ subl(first.AsRegister<Register>(),
3055 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003056 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003057 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003058 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003059 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003060 }
3061
3062 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003063 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003064 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3065 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003066 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003067 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003068 __ sbbl(first.AsRegisterPairHigh<Register>(),
3069 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003070 } else {
3071 DCHECK(second.IsConstant()) << second;
3072 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3073 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3074 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003075 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003076 break;
3077 }
3078
Calin Juravle11351682014-10-23 15:38:15 +01003079 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003080 if (second.IsFpuRegister()) {
3081 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3082 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3083 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003084 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003085 __ subss(first.AsFpuRegister<XmmRegister>(),
3086 codegen_->LiteralFloatAddress(
3087 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3088 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3089 } else {
3090 DCHECK(second.IsStackSlot());
3091 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3092 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003093 break;
Calin Juravle11351682014-10-23 15:38:15 +01003094 }
3095
3096 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003097 if (second.IsFpuRegister()) {
3098 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3099 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3100 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003101 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003102 __ subsd(first.AsFpuRegister<XmmRegister>(),
3103 codegen_->LiteralDoubleAddress(
3104 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3105 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3106 } else {
3107 DCHECK(second.IsDoubleStackSlot());
3108 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3109 }
Calin Juravle11351682014-10-23 15:38:15 +01003110 break;
3111 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003112
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003113 default:
Calin Juravle11351682014-10-23 15:38:15 +01003114 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003115 }
3116}
3117
Calin Juravle34bacdf2014-10-07 20:23:36 +01003118void LocationsBuilderX86::VisitMul(HMul* mul) {
3119 LocationSummary* locations =
3120 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3121 switch (mul->GetResultType()) {
3122 case Primitive::kPrimInt:
3123 locations->SetInAt(0, Location::RequiresRegister());
3124 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003125 if (mul->InputAt(1)->IsIntConstant()) {
3126 // Can use 3 operand multiply.
3127 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3128 } else {
3129 locations->SetOut(Location::SameAsFirstInput());
3130 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003131 break;
3132 case Primitive::kPrimLong: {
3133 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003134 locations->SetInAt(1, Location::Any());
3135 locations->SetOut(Location::SameAsFirstInput());
3136 // Needed for imul on 32bits with 64bits output.
3137 locations->AddTemp(Location::RegisterLocation(EAX));
3138 locations->AddTemp(Location::RegisterLocation(EDX));
3139 break;
3140 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003141 case Primitive::kPrimFloat:
3142 case Primitive::kPrimDouble: {
3143 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003144 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3145 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003146 } else if (mul->InputAt(1)->IsConstant()) {
3147 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003148 } else {
3149 locations->SetInAt(1, Location::Any());
3150 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003151 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003152 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003153 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003154
3155 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003156 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003157 }
3158}
3159
3160void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3161 LocationSummary* locations = mul->GetLocations();
3162 Location first = locations->InAt(0);
3163 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003164 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003165
3166 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003167 case Primitive::kPrimInt:
3168 // The constant may have ended up in a register, so test explicitly to avoid
3169 // problems where the output may not be the same as the first operand.
3170 if (mul->InputAt(1)->IsIntConstant()) {
3171 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3172 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3173 } else if (second.IsRegister()) {
3174 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003175 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003176 } else {
3177 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003178 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003179 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003180 }
3181 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003182
3183 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003184 Register in1_hi = first.AsRegisterPairHigh<Register>();
3185 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003186 Register eax = locations->GetTemp(0).AsRegister<Register>();
3187 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003188
3189 DCHECK_EQ(EAX, eax);
3190 DCHECK_EQ(EDX, edx);
3191
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003192 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003193 // output: in1
3194 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3195 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3196 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003197 if (second.IsConstant()) {
3198 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003199
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003200 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3201 int32_t low_value = Low32Bits(value);
3202 int32_t high_value = High32Bits(value);
3203 Immediate low(low_value);
3204 Immediate high(high_value);
3205
3206 __ movl(eax, high);
3207 // eax <- in1.lo * in2.hi
3208 __ imull(eax, in1_lo);
3209 // in1.hi <- in1.hi * in2.lo
3210 __ imull(in1_hi, low);
3211 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3212 __ addl(in1_hi, eax);
3213 // move in2_lo to eax to prepare for double precision
3214 __ movl(eax, low);
3215 // edx:eax <- in1.lo * in2.lo
3216 __ mull(in1_lo);
3217 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3218 __ addl(in1_hi, edx);
3219 // in1.lo <- (in1.lo * in2.lo)[31:0];
3220 __ movl(in1_lo, eax);
3221 } else if (second.IsRegisterPair()) {
3222 Register in2_hi = second.AsRegisterPairHigh<Register>();
3223 Register in2_lo = second.AsRegisterPairLow<Register>();
3224
3225 __ movl(eax, in2_hi);
3226 // eax <- in1.lo * in2.hi
3227 __ imull(eax, in1_lo);
3228 // in1.hi <- in1.hi * in2.lo
3229 __ imull(in1_hi, in2_lo);
3230 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3231 __ addl(in1_hi, eax);
3232 // move in1_lo to eax to prepare for double precision
3233 __ movl(eax, in1_lo);
3234 // edx:eax <- in1.lo * in2.lo
3235 __ mull(in2_lo);
3236 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3237 __ addl(in1_hi, edx);
3238 // in1.lo <- (in1.lo * in2.lo)[31:0];
3239 __ movl(in1_lo, eax);
3240 } else {
3241 DCHECK(second.IsDoubleStackSlot()) << second;
3242 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3243 Address in2_lo(ESP, second.GetStackIndex());
3244
3245 __ movl(eax, in2_hi);
3246 // eax <- in1.lo * in2.hi
3247 __ imull(eax, in1_lo);
3248 // in1.hi <- in1.hi * in2.lo
3249 __ imull(in1_hi, in2_lo);
3250 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3251 __ addl(in1_hi, eax);
3252 // move in1_lo to eax to prepare for double precision
3253 __ movl(eax, in1_lo);
3254 // edx:eax <- in1.lo * in2.lo
3255 __ mull(in2_lo);
3256 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3257 __ addl(in1_hi, edx);
3258 // in1.lo <- (in1.lo * in2.lo)[31:0];
3259 __ movl(in1_lo, eax);
3260 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003261
3262 break;
3263 }
3264
Calin Juravleb5bfa962014-10-21 18:02:24 +01003265 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003266 DCHECK(first.Equals(locations->Out()));
3267 if (second.IsFpuRegister()) {
3268 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3269 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3270 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003271 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003272 __ mulss(first.AsFpuRegister<XmmRegister>(),
3273 codegen_->LiteralFloatAddress(
3274 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3275 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3276 } else {
3277 DCHECK(second.IsStackSlot());
3278 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3279 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003280 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003281 }
3282
3283 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003284 DCHECK(first.Equals(locations->Out()));
3285 if (second.IsFpuRegister()) {
3286 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3287 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3288 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003289 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003290 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3291 codegen_->LiteralDoubleAddress(
3292 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3293 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3294 } else {
3295 DCHECK(second.IsDoubleStackSlot());
3296 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3297 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003298 break;
3299 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003300
3301 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003302 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003303 }
3304}
3305
Roland Levillain232ade02015-04-20 15:14:36 +01003306void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3307 uint32_t temp_offset,
3308 uint32_t stack_adjustment,
3309 bool is_fp,
3310 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003311 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003312 DCHECK(!is_wide);
3313 if (is_fp) {
3314 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3315 } else {
3316 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3317 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003318 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003319 DCHECK(is_wide);
3320 if (is_fp) {
3321 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3322 } else {
3323 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3324 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003325 } else {
3326 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003327 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003328 Location stack_temp = Location::StackSlot(temp_offset);
3329 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003330 if (is_fp) {
3331 __ flds(Address(ESP, temp_offset));
3332 } else {
3333 __ filds(Address(ESP, temp_offset));
3334 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003335 } else {
3336 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3337 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003338 if (is_fp) {
3339 __ fldl(Address(ESP, temp_offset));
3340 } else {
3341 __ fildl(Address(ESP, temp_offset));
3342 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003343 }
3344 }
3345}
3346
3347void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3348 Primitive::Type type = rem->GetResultType();
3349 bool is_float = type == Primitive::kPrimFloat;
3350 size_t elem_size = Primitive::ComponentSize(type);
3351 LocationSummary* locations = rem->GetLocations();
3352 Location first = locations->InAt(0);
3353 Location second = locations->InAt(1);
3354 Location out = locations->Out();
3355
3356 // Create stack space for 2 elements.
3357 // TODO: enhance register allocator to ask for stack temporaries.
3358 __ subl(ESP, Immediate(2 * elem_size));
3359
3360 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003361 const bool is_wide = !is_float;
3362 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3363 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003364
3365 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003366 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003367 __ Bind(&retry);
3368 __ fprem();
3369
3370 // Move FP status to AX.
3371 __ fstsw();
3372
3373 // And see if the argument reduction is complete. This is signaled by the
3374 // C2 FPU flag bit set to 0.
3375 __ andl(EAX, Immediate(kC2ConditionMask));
3376 __ j(kNotEqual, &retry);
3377
3378 // We have settled on the final value. Retrieve it into an XMM register.
3379 // Store FP top of stack to real stack.
3380 if (is_float) {
3381 __ fsts(Address(ESP, 0));
3382 } else {
3383 __ fstl(Address(ESP, 0));
3384 }
3385
3386 // Pop the 2 items from the FP stack.
3387 __ fucompp();
3388
3389 // Load the value from the stack into an XMM register.
3390 DCHECK(out.IsFpuRegister()) << out;
3391 if (is_float) {
3392 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3393 } else {
3394 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3395 }
3396
3397 // And remove the temporary stack space we allocated.
3398 __ addl(ESP, Immediate(2 * elem_size));
3399}
3400
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003401
3402void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3403 DCHECK(instruction->IsDiv() || instruction->IsRem());
3404
3405 LocationSummary* locations = instruction->GetLocations();
3406 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003407 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003408
3409 Register out_register = locations->Out().AsRegister<Register>();
3410 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003411 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003412
3413 DCHECK(imm == 1 || imm == -1);
3414
3415 if (instruction->IsRem()) {
3416 __ xorl(out_register, out_register);
3417 } else {
3418 __ movl(out_register, input_register);
3419 if (imm == -1) {
3420 __ negl(out_register);
3421 }
3422 }
3423}
3424
3425
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003426void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003427 LocationSummary* locations = instruction->GetLocations();
3428
3429 Register out_register = locations->Out().AsRegister<Register>();
3430 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003431 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003432 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3433 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003434
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003435 Register num = locations->GetTemp(0).AsRegister<Register>();
3436
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003437 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003438 __ testl(input_register, input_register);
3439 __ cmovl(kGreaterEqual, num, input_register);
3440 int shift = CTZ(imm);
3441 __ sarl(num, Immediate(shift));
3442
3443 if (imm < 0) {
3444 __ negl(num);
3445 }
3446
3447 __ movl(out_register, num);
3448}
3449
3450void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3451 DCHECK(instruction->IsDiv() || instruction->IsRem());
3452
3453 LocationSummary* locations = instruction->GetLocations();
3454 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3455
3456 Register eax = locations->InAt(0).AsRegister<Register>();
3457 Register out = locations->Out().AsRegister<Register>();
3458 Register num;
3459 Register edx;
3460
3461 if (instruction->IsDiv()) {
3462 edx = locations->GetTemp(0).AsRegister<Register>();
3463 num = locations->GetTemp(1).AsRegister<Register>();
3464 } else {
3465 edx = locations->Out().AsRegister<Register>();
3466 num = locations->GetTemp(0).AsRegister<Register>();
3467 }
3468
3469 DCHECK_EQ(EAX, eax);
3470 DCHECK_EQ(EDX, edx);
3471 if (instruction->IsDiv()) {
3472 DCHECK_EQ(EAX, out);
3473 } else {
3474 DCHECK_EQ(EDX, out);
3475 }
3476
3477 int64_t magic;
3478 int shift;
3479 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3480
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003481 // Save the numerator.
3482 __ movl(num, eax);
3483
3484 // EAX = magic
3485 __ movl(eax, Immediate(magic));
3486
3487 // EDX:EAX = magic * numerator
3488 __ imull(num);
3489
3490 if (imm > 0 && magic < 0) {
3491 // EDX += num
3492 __ addl(edx, num);
3493 } else if (imm < 0 && magic > 0) {
3494 __ subl(edx, num);
3495 }
3496
3497 // Shift if needed.
3498 if (shift != 0) {
3499 __ sarl(edx, Immediate(shift));
3500 }
3501
3502 // EDX += 1 if EDX < 0
3503 __ movl(eax, edx);
3504 __ shrl(edx, Immediate(31));
3505 __ addl(edx, eax);
3506
3507 if (instruction->IsRem()) {
3508 __ movl(eax, num);
3509 __ imull(edx, Immediate(imm));
3510 __ subl(eax, edx);
3511 __ movl(edx, eax);
3512 } else {
3513 __ movl(eax, edx);
3514 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003515}
3516
Calin Juravlebacfec32014-11-14 15:54:36 +00003517void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3518 DCHECK(instruction->IsDiv() || instruction->IsRem());
3519
3520 LocationSummary* locations = instruction->GetLocations();
3521 Location out = locations->Out();
3522 Location first = locations->InAt(0);
3523 Location second = locations->InAt(1);
3524 bool is_div = instruction->IsDiv();
3525
3526 switch (instruction->GetResultType()) {
3527 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003528 DCHECK_EQ(EAX, first.AsRegister<Register>());
3529 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003530
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003531 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003532 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003533
3534 if (imm == 0) {
3535 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3536 } else if (imm == 1 || imm == -1) {
3537 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003538 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003539 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003540 } else {
3541 DCHECK(imm <= -2 || imm >= 2);
3542 GenerateDivRemWithAnyConstant(instruction);
3543 }
3544 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003545 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3546 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003547 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003548
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003549 Register second_reg = second.AsRegister<Register>();
3550 // 0x80000000/-1 triggers an arithmetic exception!
3551 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3552 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003553
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003554 __ cmpl(second_reg, Immediate(-1));
3555 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003556
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003557 // edx:eax <- sign-extended of eax
3558 __ cdq();
3559 // eax = quotient, edx = remainder
3560 __ idivl(second_reg);
3561 __ Bind(slow_path->GetExitLabel());
3562 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003563 break;
3564 }
3565
3566 case Primitive::kPrimLong: {
3567 InvokeRuntimeCallingConvention calling_convention;
3568 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3569 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3570 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3571 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3572 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3573 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3574
3575 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003576 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003577 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003578 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003579 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003580 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003581 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003582 break;
3583 }
3584
3585 default:
3586 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3587 }
3588}
3589
Calin Juravle7c4954d2014-10-28 16:57:40 +00003590void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003591 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003592 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003593 : LocationSummary::kNoCall;
3594 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3595
Calin Juravle7c4954d2014-10-28 16:57:40 +00003596 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003597 case Primitive::kPrimInt: {
3598 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003599 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003600 locations->SetOut(Location::SameAsFirstInput());
3601 // Intel uses edx:eax as the dividend.
3602 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003603 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3604 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3605 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003606 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003607 locations->AddTemp(Location::RequiresRegister());
3608 }
Calin Juravled0d48522014-11-04 16:40:20 +00003609 break;
3610 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003611 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003612 InvokeRuntimeCallingConvention calling_convention;
3613 locations->SetInAt(0, Location::RegisterPairLocation(
3614 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3615 locations->SetInAt(1, Location::RegisterPairLocation(
3616 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3617 // Runtime helper puts the result in EAX, EDX.
3618 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003619 break;
3620 }
3621 case Primitive::kPrimFloat:
3622 case Primitive::kPrimDouble: {
3623 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003624 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3625 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003626 } else if (div->InputAt(1)->IsConstant()) {
3627 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003628 } else {
3629 locations->SetInAt(1, Location::Any());
3630 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003631 locations->SetOut(Location::SameAsFirstInput());
3632 break;
3633 }
3634
3635 default:
3636 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3637 }
3638}
3639
3640void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3641 LocationSummary* locations = div->GetLocations();
3642 Location first = locations->InAt(0);
3643 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003644
3645 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003646 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003647 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003648 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003649 break;
3650 }
3651
3652 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003653 if (second.IsFpuRegister()) {
3654 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3655 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3656 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003657 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003658 __ divss(first.AsFpuRegister<XmmRegister>(),
3659 codegen_->LiteralFloatAddress(
3660 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3661 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3662 } else {
3663 DCHECK(second.IsStackSlot());
3664 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3665 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003666 break;
3667 }
3668
3669 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003670 if (second.IsFpuRegister()) {
3671 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3672 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3673 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003674 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003675 __ divsd(first.AsFpuRegister<XmmRegister>(),
3676 codegen_->LiteralDoubleAddress(
3677 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3678 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3679 } else {
3680 DCHECK(second.IsDoubleStackSlot());
3681 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3682 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003683 break;
3684 }
3685
3686 default:
3687 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3688 }
3689}
3690
Calin Juravlebacfec32014-11-14 15:54:36 +00003691void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003692 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003693
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003694 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003695 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003696 : LocationSummary::kNoCall;
3697 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003698
Calin Juravled2ec87d2014-12-08 14:24:46 +00003699 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003700 case Primitive::kPrimInt: {
3701 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003702 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003703 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003704 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3705 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3706 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003707 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003708 locations->AddTemp(Location::RequiresRegister());
3709 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003710 break;
3711 }
3712 case Primitive::kPrimLong: {
3713 InvokeRuntimeCallingConvention calling_convention;
3714 locations->SetInAt(0, Location::RegisterPairLocation(
3715 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3716 locations->SetInAt(1, Location::RegisterPairLocation(
3717 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3718 // Runtime helper puts the result in EAX, EDX.
3719 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3720 break;
3721 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003722 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003723 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003724 locations->SetInAt(0, Location::Any());
3725 locations->SetInAt(1, Location::Any());
3726 locations->SetOut(Location::RequiresFpuRegister());
3727 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003728 break;
3729 }
3730
3731 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003732 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003733 }
3734}
3735
3736void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3737 Primitive::Type type = rem->GetResultType();
3738 switch (type) {
3739 case Primitive::kPrimInt:
3740 case Primitive::kPrimLong: {
3741 GenerateDivRemIntegral(rem);
3742 break;
3743 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003744 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003745 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003746 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003747 break;
3748 }
3749 default:
3750 LOG(FATAL) << "Unexpected rem type " << type;
3751 }
3752}
3753
Calin Juravled0d48522014-11-04 16:40:20 +00003754void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003755 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003756 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003757 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003758 case Primitive::kPrimByte:
3759 case Primitive::kPrimChar:
3760 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003761 case Primitive::kPrimInt: {
3762 locations->SetInAt(0, Location::Any());
3763 break;
3764 }
3765 case Primitive::kPrimLong: {
3766 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3767 if (!instruction->IsConstant()) {
3768 locations->AddTemp(Location::RequiresRegister());
3769 }
3770 break;
3771 }
3772 default:
3773 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3774 }
Calin Juravled0d48522014-11-04 16:40:20 +00003775}
3776
3777void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003778 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003779 codegen_->AddSlowPath(slow_path);
3780
3781 LocationSummary* locations = instruction->GetLocations();
3782 Location value = locations->InAt(0);
3783
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003784 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003785 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003786 case Primitive::kPrimByte:
3787 case Primitive::kPrimChar:
3788 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003789 case Primitive::kPrimInt: {
3790 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003791 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003792 __ j(kEqual, slow_path->GetEntryLabel());
3793 } else if (value.IsStackSlot()) {
3794 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3795 __ j(kEqual, slow_path->GetEntryLabel());
3796 } else {
3797 DCHECK(value.IsConstant()) << value;
3798 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003799 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003800 }
3801 }
3802 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003803 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003804 case Primitive::kPrimLong: {
3805 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003806 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003807 __ movl(temp, value.AsRegisterPairLow<Register>());
3808 __ orl(temp, value.AsRegisterPairHigh<Register>());
3809 __ j(kEqual, slow_path->GetEntryLabel());
3810 } else {
3811 DCHECK(value.IsConstant()) << value;
3812 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3813 __ jmp(slow_path->GetEntryLabel());
3814 }
3815 }
3816 break;
3817 }
3818 default:
3819 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003820 }
Calin Juravled0d48522014-11-04 16:40:20 +00003821}
3822
Calin Juravle9aec02f2014-11-18 23:06:35 +00003823void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3824 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3825
3826 LocationSummary* locations =
3827 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3828
3829 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003830 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003831 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003832 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003833 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003834 // The shift count needs to be in CL or a constant.
3835 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003836 locations->SetOut(Location::SameAsFirstInput());
3837 break;
3838 }
3839 default:
3840 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3841 }
3842}
3843
3844void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3845 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3846
3847 LocationSummary* locations = op->GetLocations();
3848 Location first = locations->InAt(0);
3849 Location second = locations->InAt(1);
3850 DCHECK(first.Equals(locations->Out()));
3851
3852 switch (op->GetResultType()) {
3853 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003854 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003855 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003856 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003857 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003858 DCHECK_EQ(ECX, second_reg);
3859 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003860 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003861 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003862 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003863 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003864 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003865 }
3866 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003867 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003868 if (shift == 0) {
3869 return;
3870 }
3871 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003872 if (op->IsShl()) {
3873 __ shll(first_reg, imm);
3874 } else if (op->IsShr()) {
3875 __ sarl(first_reg, imm);
3876 } else {
3877 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003878 }
3879 }
3880 break;
3881 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003882 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003883 if (second.IsRegister()) {
3884 Register second_reg = second.AsRegister<Register>();
3885 DCHECK_EQ(ECX, second_reg);
3886 if (op->IsShl()) {
3887 GenerateShlLong(first, second_reg);
3888 } else if (op->IsShr()) {
3889 GenerateShrLong(first, second_reg);
3890 } else {
3891 GenerateUShrLong(first, second_reg);
3892 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003893 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003894 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003895 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003896 // Nothing to do if the shift is 0, as the input is already the output.
3897 if (shift != 0) {
3898 if (op->IsShl()) {
3899 GenerateShlLong(first, shift);
3900 } else if (op->IsShr()) {
3901 GenerateShrLong(first, shift);
3902 } else {
3903 GenerateUShrLong(first, shift);
3904 }
3905 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003906 }
3907 break;
3908 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003909 default:
3910 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3911 }
3912}
3913
Mark P Mendell73945692015-04-29 14:56:17 +00003914void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3915 Register low = loc.AsRegisterPairLow<Register>();
3916 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003917 if (shift == 1) {
3918 // This is just an addition.
3919 __ addl(low, low);
3920 __ adcl(high, high);
3921 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003922 // Shift by 32 is easy. High gets low, and low gets 0.
3923 codegen_->EmitParallelMoves(
3924 loc.ToLow(),
3925 loc.ToHigh(),
3926 Primitive::kPrimInt,
3927 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3928 loc.ToLow(),
3929 Primitive::kPrimInt);
3930 } else if (shift > 32) {
3931 // Low part becomes 0. High part is low part << (shift-32).
3932 __ movl(high, low);
3933 __ shll(high, Immediate(shift - 32));
3934 __ xorl(low, low);
3935 } else {
3936 // Between 1 and 31.
3937 __ shld(high, low, Immediate(shift));
3938 __ shll(low, Immediate(shift));
3939 }
3940}
3941
Calin Juravle9aec02f2014-11-18 23:06:35 +00003942void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003943 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003944 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3945 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3946 __ testl(shifter, Immediate(32));
3947 __ j(kEqual, &done);
3948 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3949 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3950 __ Bind(&done);
3951}
3952
Mark P Mendell73945692015-04-29 14:56:17 +00003953void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3954 Register low = loc.AsRegisterPairLow<Register>();
3955 Register high = loc.AsRegisterPairHigh<Register>();
3956 if (shift == 32) {
3957 // Need to copy the sign.
3958 DCHECK_NE(low, high);
3959 __ movl(low, high);
3960 __ sarl(high, Immediate(31));
3961 } else if (shift > 32) {
3962 DCHECK_NE(low, high);
3963 // High part becomes sign. Low part is shifted by shift - 32.
3964 __ movl(low, high);
3965 __ sarl(high, Immediate(31));
3966 __ sarl(low, Immediate(shift - 32));
3967 } else {
3968 // Between 1 and 31.
3969 __ shrd(low, high, Immediate(shift));
3970 __ sarl(high, Immediate(shift));
3971 }
3972}
3973
Calin Juravle9aec02f2014-11-18 23:06:35 +00003974void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003975 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003976 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3977 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3978 __ testl(shifter, Immediate(32));
3979 __ j(kEqual, &done);
3980 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3981 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3982 __ Bind(&done);
3983}
3984
Mark P Mendell73945692015-04-29 14:56:17 +00003985void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3986 Register low = loc.AsRegisterPairLow<Register>();
3987 Register high = loc.AsRegisterPairHigh<Register>();
3988 if (shift == 32) {
3989 // Shift by 32 is easy. Low gets high, and high gets 0.
3990 codegen_->EmitParallelMoves(
3991 loc.ToHigh(),
3992 loc.ToLow(),
3993 Primitive::kPrimInt,
3994 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3995 loc.ToHigh(),
3996 Primitive::kPrimInt);
3997 } else if (shift > 32) {
3998 // Low part is high >> (shift - 32). High part becomes 0.
3999 __ movl(low, high);
4000 __ shrl(low, Immediate(shift - 32));
4001 __ xorl(high, high);
4002 } else {
4003 // Between 1 and 31.
4004 __ shrd(low, high, Immediate(shift));
4005 __ shrl(high, Immediate(shift));
4006 }
4007}
4008
Calin Juravle9aec02f2014-11-18 23:06:35 +00004009void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004010 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004011 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4012 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4013 __ testl(shifter, Immediate(32));
4014 __ j(kEqual, &done);
4015 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4016 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4017 __ Bind(&done);
4018}
4019
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004020void LocationsBuilderX86::VisitRor(HRor* ror) {
4021 LocationSummary* locations =
4022 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4023
4024 switch (ror->GetResultType()) {
4025 case Primitive::kPrimLong:
4026 // Add the temporary needed.
4027 locations->AddTemp(Location::RequiresRegister());
4028 FALLTHROUGH_INTENDED;
4029 case Primitive::kPrimInt:
4030 locations->SetInAt(0, Location::RequiresRegister());
4031 // The shift count needs to be in CL (unless it is a constant).
4032 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4033 locations->SetOut(Location::SameAsFirstInput());
4034 break;
4035 default:
4036 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4037 UNREACHABLE();
4038 }
4039}
4040
4041void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4042 LocationSummary* locations = ror->GetLocations();
4043 Location first = locations->InAt(0);
4044 Location second = locations->InAt(1);
4045
4046 if (ror->GetResultType() == Primitive::kPrimInt) {
4047 Register first_reg = first.AsRegister<Register>();
4048 if (second.IsRegister()) {
4049 Register second_reg = second.AsRegister<Register>();
4050 __ rorl(first_reg, second_reg);
4051 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004052 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004053 __ rorl(first_reg, imm);
4054 }
4055 return;
4056 }
4057
4058 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4059 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4060 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4061 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4062 if (second.IsRegister()) {
4063 Register second_reg = second.AsRegister<Register>();
4064 DCHECK_EQ(second_reg, ECX);
4065 __ movl(temp_reg, first_reg_hi);
4066 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4067 __ shrd(first_reg_lo, temp_reg, second_reg);
4068 __ movl(temp_reg, first_reg_hi);
4069 __ testl(second_reg, Immediate(32));
4070 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4071 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4072 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004073 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004074 if (shift_amt == 0) {
4075 // Already fine.
4076 return;
4077 }
4078 if (shift_amt == 32) {
4079 // Just swap.
4080 __ movl(temp_reg, first_reg_lo);
4081 __ movl(first_reg_lo, first_reg_hi);
4082 __ movl(first_reg_hi, temp_reg);
4083 return;
4084 }
4085
4086 Immediate imm(shift_amt);
4087 // Save the constents of the low value.
4088 __ movl(temp_reg, first_reg_lo);
4089
4090 // Shift right into low, feeding bits from high.
4091 __ shrd(first_reg_lo, first_reg_hi, imm);
4092
4093 // Shift right into high, feeding bits from the original low.
4094 __ shrd(first_reg_hi, temp_reg, imm);
4095
4096 // Swap if needed.
4097 if (shift_amt > 32) {
4098 __ movl(temp_reg, first_reg_lo);
4099 __ movl(first_reg_lo, first_reg_hi);
4100 __ movl(first_reg_hi, temp_reg);
4101 }
4102 }
4103}
4104
Calin Juravle9aec02f2014-11-18 23:06:35 +00004105void LocationsBuilderX86::VisitShl(HShl* shl) {
4106 HandleShift(shl);
4107}
4108
4109void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4110 HandleShift(shl);
4111}
4112
4113void LocationsBuilderX86::VisitShr(HShr* shr) {
4114 HandleShift(shr);
4115}
4116
4117void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4118 HandleShift(shr);
4119}
4120
4121void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4122 HandleShift(ushr);
4123}
4124
4125void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4126 HandleShift(ushr);
4127}
4128
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004129void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004130 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004131 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004132 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004133 if (instruction->IsStringAlloc()) {
4134 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4135 } else {
4136 InvokeRuntimeCallingConvention calling_convention;
4137 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4138 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4139 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004140}
4141
4142void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004143 // Note: if heap poisoning is enabled, the entry point takes cares
4144 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004145 if (instruction->IsStringAlloc()) {
4146 // String is allocated through StringFactory. Call NewEmptyString entry point.
4147 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004148 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004149 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4150 __ call(Address(temp, code_offset.Int32Value()));
4151 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4152 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004153 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004154 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4155 DCHECK(!codegen_->IsLeafMethod());
4156 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004157}
4158
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004159void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4160 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004161 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004162 locations->SetOut(Location::RegisterLocation(EAX));
4163 InvokeRuntimeCallingConvention calling_convention;
4164 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004165 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004166 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004167}
4168
4169void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4170 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004171 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex().index_));
Roland Levillain4d027112015-07-01 15:41:14 +01004172 // Note: if heap poisoning is enabled, the entry point takes cares
4173 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004174 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004175 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004176 DCHECK(!codegen_->IsLeafMethod());
4177}
4178
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004179void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004180 LocationSummary* locations =
4181 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004182 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4183 if (location.IsStackSlot()) {
4184 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4185 } else if (location.IsDoubleStackSlot()) {
4186 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004187 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004188 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004189}
4190
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004191void InstructionCodeGeneratorX86::VisitParameterValue(
4192 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4193}
4194
4195void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4196 LocationSummary* locations =
4197 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4198 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4199}
4200
4201void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004202}
4203
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004204void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4205 LocationSummary* locations =
4206 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4207 locations->SetInAt(0, Location::RequiresRegister());
4208 locations->SetOut(Location::RequiresRegister());
4209}
4210
4211void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4212 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004213 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004214 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004215 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004216 __ movl(locations->Out().AsRegister<Register>(),
4217 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004218 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004219 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004220 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004221 __ movl(locations->Out().AsRegister<Register>(),
4222 Address(locations->InAt(0).AsRegister<Register>(),
4223 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4224 // temp = temp->GetImtEntryAt(method_offset);
4225 __ movl(locations->Out().AsRegister<Register>(),
4226 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004227 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004228}
4229
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004230void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004231 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004232 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004233 locations->SetInAt(0, Location::RequiresRegister());
4234 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004235}
4236
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004237void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4238 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004239 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004240 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004241 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004242 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004243 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004244 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004245 break;
4246
4247 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004248 __ notl(out.AsRegisterPairLow<Register>());
4249 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004250 break;
4251
4252 default:
4253 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4254 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004255}
4256
David Brazdil66d126e2015-04-03 16:02:44 +01004257void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4258 LocationSummary* locations =
4259 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4260 locations->SetInAt(0, Location::RequiresRegister());
4261 locations->SetOut(Location::SameAsFirstInput());
4262}
4263
4264void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004265 LocationSummary* locations = bool_not->GetLocations();
4266 Location in = locations->InAt(0);
4267 Location out = locations->Out();
4268 DCHECK(in.Equals(out));
4269 __ xorl(out.AsRegister<Register>(), Immediate(1));
4270}
4271
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004272void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004273 LocationSummary* locations =
4274 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004275 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004276 case Primitive::kPrimBoolean:
4277 case Primitive::kPrimByte:
4278 case Primitive::kPrimShort:
4279 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004280 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004281 case Primitive::kPrimLong: {
4282 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004283 locations->SetInAt(1, Location::Any());
4284 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4285 break;
4286 }
4287 case Primitive::kPrimFloat:
4288 case Primitive::kPrimDouble: {
4289 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004290 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4291 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4292 } else if (compare->InputAt(1)->IsConstant()) {
4293 locations->SetInAt(1, Location::RequiresFpuRegister());
4294 } else {
4295 locations->SetInAt(1, Location::Any());
4296 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004297 locations->SetOut(Location::RequiresRegister());
4298 break;
4299 }
4300 default:
4301 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4302 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004303}
4304
4305void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004306 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004307 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004308 Location left = locations->InAt(0);
4309 Location right = locations->InAt(1);
4310
Mark Mendell0c9497d2015-08-21 09:30:05 -04004311 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004312 Condition less_cond = kLess;
4313
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004314 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004315 case Primitive::kPrimBoolean:
4316 case Primitive::kPrimByte:
4317 case Primitive::kPrimShort:
4318 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004319 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004320 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004321 break;
4322 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004323 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004324 Register left_low = left.AsRegisterPairLow<Register>();
4325 Register left_high = left.AsRegisterPairHigh<Register>();
4326 int32_t val_low = 0;
4327 int32_t val_high = 0;
4328 bool right_is_const = false;
4329
4330 if (right.IsConstant()) {
4331 DCHECK(right.GetConstant()->IsLongConstant());
4332 right_is_const = true;
4333 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4334 val_low = Low32Bits(val);
4335 val_high = High32Bits(val);
4336 }
4337
Calin Juravleddb7df22014-11-25 20:56:51 +00004338 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004339 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004340 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004341 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004342 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004343 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004344 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004345 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004346 __ j(kLess, &less); // Signed compare.
4347 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004348 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004349 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004350 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004351 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004352 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004353 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004354 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004355 }
Aart Bika19616e2016-02-01 18:57:58 -08004356 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004357 break;
4358 }
4359 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004360 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004361 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004362 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004363 break;
4364 }
4365 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004366 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004367 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004368 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004369 break;
4370 }
4371 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004372 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004373 }
Aart Bika19616e2016-02-01 18:57:58 -08004374
Calin Juravleddb7df22014-11-25 20:56:51 +00004375 __ movl(out, Immediate(0));
4376 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004377 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004378
4379 __ Bind(&greater);
4380 __ movl(out, Immediate(1));
4381 __ jmp(&done);
4382
4383 __ Bind(&less);
4384 __ movl(out, Immediate(-1));
4385
4386 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004387}
4388
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004389void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004390 LocationSummary* locations =
4391 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004392 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004393 locations->SetInAt(i, Location::Any());
4394 }
4395 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004396}
4397
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004398void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004399 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004400}
4401
Roland Levillain7c1559a2015-12-15 10:55:36 +00004402void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004403 /*
4404 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4405 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4406 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4407 */
4408 switch (kind) {
4409 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004410 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004411 break;
4412 }
4413 case MemBarrierKind::kAnyStore:
4414 case MemBarrierKind::kLoadAny:
4415 case MemBarrierKind::kStoreStore: {
4416 // nop
4417 break;
4418 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004419 case MemBarrierKind::kNTStoreStore:
4420 // Non-Temporal Store/Store needs an explicit fence.
4421 MemoryFence(/* non-temporal */ true);
4422 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004423 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004424}
4425
Vladimir Markodc151b22015-10-15 18:02:30 +01004426HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4427 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004428 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004429 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4430
4431 // We disable pc-relative load when there is an irreducible loop, as the optimization
4432 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004433 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4434 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004435 if (GetGraph()->HasIrreducibleLoops() &&
4436 (dispatch_info.method_load_kind ==
4437 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4438 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4439 }
4440 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004441 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4442 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4443 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4444 // (Though the direct CALL ptr16:32 is available for consideration).
4445 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004446 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004447 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004448 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004449 0u
4450 };
4451 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004452 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004453 }
4454}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004455
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004456Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4457 Register temp) {
4458 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004459 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004460 if (!invoke->GetLocations()->Intrinsified()) {
4461 return location.AsRegister<Register>();
4462 }
4463 // For intrinsics we allow any location, so it may be on the stack.
4464 if (!location.IsRegister()) {
4465 __ movl(temp, Address(ESP, location.GetStackIndex()));
4466 return temp;
4467 }
4468 // For register locations, check if the register was saved. If so, get it from the stack.
4469 // Note: There is a chance that the register was saved but not overwritten, so we could
4470 // save one load. However, since this is just an intrinsic slow path we prefer this
4471 // simple and more robust approach rather that trying to determine if that's the case.
4472 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004473 if (slow_path != nullptr) {
4474 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4475 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4476 __ movl(temp, Address(ESP, stack_offset));
4477 return temp;
4478 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004479 }
4480 return location.AsRegister<Register>();
4481}
4482
Serguei Katkov288c7a82016-05-16 11:53:15 +06004483Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4484 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004485 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4486 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004487 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004488 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004489 uint32_t offset =
4490 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4491 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004492 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004493 }
Vladimir Marko58155012015-08-19 12:49:41 +00004494 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004495 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004496 break;
4497 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4498 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4499 break;
4500 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004501 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004502 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4503 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004504 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4505 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004506 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4507 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4508 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004509 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004510 // Bind a new fixup label at the end of the "movl" insn.
4511 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004512 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004513 break;
4514 }
Vladimir Marko58155012015-08-19 12:49:41 +00004515 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004516 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004517 Register method_reg;
4518 Register reg = temp.AsRegister<Register>();
4519 if (current_method.IsRegister()) {
4520 method_reg = current_method.AsRegister<Register>();
4521 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004522 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004523 DCHECK(!current_method.IsValid());
4524 method_reg = reg;
4525 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4526 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004527 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004528 __ movl(reg, Address(method_reg,
4529 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004530 // temp = temp[index_in_cache];
4531 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4532 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004533 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4534 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004535 }
Vladimir Marko58155012015-08-19 12:49:41 +00004536 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004537 return callee_method;
4538}
4539
4540void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4541 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004542
4543 switch (invoke->GetCodePtrLocation()) {
4544 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4545 __ call(GetFrameEntryLabel());
4546 break;
4547 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004548 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4549 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004550 Label* label = &relative_call_patches_.back().label;
4551 __ call(label); // Bind to the patch label, override at link time.
4552 __ Bind(label); // Bind the label at the end of the "call" insn.
4553 break;
4554 }
4555 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4556 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004557 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4558 LOG(FATAL) << "Unsupported";
4559 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004560 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4561 // (callee_method + offset_of_quick_compiled_code)()
4562 __ call(Address(callee_method.AsRegister<Register>(),
4563 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004564 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004565 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004566 }
4567
4568 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004569}
4570
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004571void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4572 Register temp = temp_in.AsRegister<Register>();
4573 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4574 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004575
4576 // Use the calling convention instead of the location of the receiver, as
4577 // intrinsics may have put the receiver in a different register. In the intrinsics
4578 // slow path, the arguments have been moved to the right place, so here we are
4579 // guaranteed that the receiver is the first register of the calling convention.
4580 InvokeDexCallingConvention calling_convention;
4581 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004582 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004583 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004584 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004585 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004586 // Instead of simply (possibly) unpoisoning `temp` here, we should
4587 // emit a read barrier for the previous class reference load.
4588 // However this is not required in practice, as this is an
4589 // intermediate/temporary reference and because the current
4590 // concurrent copying collector keeps the from-space memory
4591 // intact/accessible until the end of the marking phase (the
4592 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004593 __ MaybeUnpoisonHeapReference(temp);
4594 // temp = temp->GetMethodAt(method_offset);
4595 __ movl(temp, Address(temp, method_offset));
4596 // call temp->GetEntryPoint();
4597 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004598 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004599}
4600
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004601void CodeGeneratorX86::RecordSimplePatch() {
4602 if (GetCompilerOptions().GetIncludePatchInformation()) {
4603 simple_patches_.emplace_back();
4604 __ Bind(&simple_patches_.back());
4605 }
4606}
4607
Vladimir Markoaad75c62016-10-03 08:46:48 +00004608void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4609 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004610 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4611 __ Bind(&string_patches_.back().label);
4612}
4613
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004614void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004615 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004616 __ Bind(&type_patches_.back().label);
4617}
4618
Vladimir Markoaad75c62016-10-03 08:46:48 +00004619Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4620 DCHECK(!GetCompilerOptions().IsBootImage());
4621 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4622 return &string_patches_.back().label;
4623}
4624
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004625Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4626 uint32_t element_offset) {
4627 // Add the patch entry and bind its label at the end of the instruction.
4628 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4629 return &pc_relative_dex_cache_patches_.back().label;
4630}
4631
Vladimir Markoaad75c62016-10-03 08:46:48 +00004632// The label points to the end of the "movl" or another instruction but the literal offset
4633// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4634constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4635
4636template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4637inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
4638 const ArenaDeque<PatchInfo<Label>>& infos,
4639 ArenaVector<LinkerPatch>* linker_patches) {
4640 for (const PatchInfo<Label>& info : infos) {
4641 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4642 linker_patches->push_back(
4643 Factory(literal_offset, &info.dex_file, GetMethodAddressOffset(), info.index));
4644 }
4645}
4646
Vladimir Marko58155012015-08-19 12:49:41 +00004647void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4648 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004649 size_t size =
4650 method_patches_.size() +
4651 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004652 pc_relative_dex_cache_patches_.size() +
4653 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004654 string_patches_.size() +
4655 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004656 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004657 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004658 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004659 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004660 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004661 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004662 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004663 linker_patches->push_back(
4664 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004665 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004666 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4667 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004668 for (const Label& label : simple_patches_) {
4669 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4670 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4671 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004672 if (!GetCompilerOptions().IsBootImage()) {
4673 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4674 } else if (GetCompilerOptions().GetCompilePic()) {
4675 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004676 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004677 for (const PatchInfo<Label>& info : string_patches_) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004678 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004679 linker_patches->push_back(
4680 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004681 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004682 }
4683 if (GetCompilerOptions().GetCompilePic()) {
4684 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
4685 } else {
4686 for (const PatchInfo<Label>& info : type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004687 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004688 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004689 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004690 }
Vladimir Marko58155012015-08-19 12:49:41 +00004691}
4692
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004693void CodeGeneratorX86::MarkGCCard(Register temp,
4694 Register card,
4695 Register object,
4696 Register value,
4697 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004698 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004699 if (value_can_be_null) {
4700 __ testl(value, value);
4701 __ j(kEqual, &is_null);
4702 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004703 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004704 __ movl(temp, object);
4705 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004706 __ movb(Address(temp, card, TIMES_1, 0),
4707 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004708 if (value_can_be_null) {
4709 __ Bind(&is_null);
4710 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004711}
4712
Calin Juravle52c48962014-12-16 17:02:57 +00004713void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4714 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004715
4716 bool object_field_get_with_read_barrier =
4717 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004718 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004719 new (GetGraph()->GetArena()) LocationSummary(instruction,
4720 kEmitCompilerReadBarrier ?
4721 LocationSummary::kCallOnSlowPath :
4722 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004723 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004724 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004725 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004726 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004727
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004728 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4729 locations->SetOut(Location::RequiresFpuRegister());
4730 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004731 // The output overlaps in case of long: we don't want the low move
4732 // to overwrite the object's location. Likewise, in the case of
4733 // an object field get with read barriers enabled, we do not want
4734 // the move to overwrite the object's location, as we need it to emit
4735 // the read barrier.
4736 locations->SetOut(
4737 Location::RequiresRegister(),
4738 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4739 Location::kOutputOverlap :
4740 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004741 }
Calin Juravle52c48962014-12-16 17:02:57 +00004742
4743 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4744 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004745 // So we use an XMM register as a temp to achieve atomicity (first
4746 // load the temp into the XMM and then copy the XMM into the
4747 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004748 locations->AddTemp(Location::RequiresFpuRegister());
4749 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004750}
4751
Calin Juravle52c48962014-12-16 17:02:57 +00004752void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4753 const FieldInfo& field_info) {
4754 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004755
Calin Juravle52c48962014-12-16 17:02:57 +00004756 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004757 Location base_loc = locations->InAt(0);
4758 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004759 Location out = locations->Out();
4760 bool is_volatile = field_info.IsVolatile();
4761 Primitive::Type field_type = field_info.GetFieldType();
4762 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4763
4764 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004765 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004766 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004767 break;
4768 }
4769
4770 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004771 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004772 break;
4773 }
4774
4775 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004776 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004777 break;
4778 }
4779
4780 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004781 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004782 break;
4783 }
4784
4785 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004786 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004787 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004788
4789 case Primitive::kPrimNot: {
4790 // /* HeapReference<Object> */ out = *(base + offset)
4791 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004792 // Note that a potential implicit null check is handled in this
4793 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4794 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004795 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004796 if (is_volatile) {
4797 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4798 }
4799 } else {
4800 __ movl(out.AsRegister<Register>(), Address(base, offset));
4801 codegen_->MaybeRecordImplicitNullCheck(instruction);
4802 if (is_volatile) {
4803 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4804 }
4805 // If read barriers are enabled, emit read barriers other than
4806 // Baker's using a slow path (and also unpoison the loaded
4807 // reference, if heap poisoning is enabled).
4808 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4809 }
4810 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004811 }
4812
4813 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004814 if (is_volatile) {
4815 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4816 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004817 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004818 __ movd(out.AsRegisterPairLow<Register>(), temp);
4819 __ psrlq(temp, Immediate(32));
4820 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4821 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004822 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004823 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004824 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004825 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4826 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004827 break;
4828 }
4829
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004830 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004831 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004832 break;
4833 }
4834
4835 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004836 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004837 break;
4838 }
4839
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004840 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004841 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004842 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004843 }
Calin Juravle52c48962014-12-16 17:02:57 +00004844
Roland Levillain7c1559a2015-12-15 10:55:36 +00004845 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4846 // Potential implicit null checks, in the case of reference or
4847 // long fields, are handled in the previous switch statement.
4848 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004849 codegen_->MaybeRecordImplicitNullCheck(instruction);
4850 }
4851
Calin Juravle52c48962014-12-16 17:02:57 +00004852 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004853 if (field_type == Primitive::kPrimNot) {
4854 // Memory barriers, in the case of references, are also handled
4855 // in the previous switch statement.
4856 } else {
4857 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4858 }
Roland Levillain4d027112015-07-01 15:41:14 +01004859 }
Calin Juravle52c48962014-12-16 17:02:57 +00004860}
4861
4862void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4863 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4864
4865 LocationSummary* locations =
4866 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4867 locations->SetInAt(0, Location::RequiresRegister());
4868 bool is_volatile = field_info.IsVolatile();
4869 Primitive::Type field_type = field_info.GetFieldType();
4870 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4871 || (field_type == Primitive::kPrimByte);
4872
4873 // The register allocator does not support multiple
4874 // inputs that die at entry with one in a specific register.
4875 if (is_byte_type) {
4876 // Ensure the value is in a byte register.
4877 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004878 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004879 if (is_volatile && field_type == Primitive::kPrimDouble) {
4880 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4881 locations->SetInAt(1, Location::RequiresFpuRegister());
4882 } else {
4883 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4884 }
4885 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4886 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004887 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004888
Calin Juravle52c48962014-12-16 17:02:57 +00004889 // 64bits value can be atomically written to an address with movsd and an XMM register.
4890 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4891 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4892 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4893 // isolated cases when we need this it isn't worth adding the extra complexity.
4894 locations->AddTemp(Location::RequiresFpuRegister());
4895 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004896 } else {
4897 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4898
4899 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4900 // Temporary registers for the write barrier.
4901 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4902 // Ensure the card is in a byte register.
4903 locations->AddTemp(Location::RegisterLocation(ECX));
4904 }
Calin Juravle52c48962014-12-16 17:02:57 +00004905 }
4906}
4907
4908void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004909 const FieldInfo& field_info,
4910 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004911 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4912
4913 LocationSummary* locations = instruction->GetLocations();
4914 Register base = locations->InAt(0).AsRegister<Register>();
4915 Location value = locations->InAt(1);
4916 bool is_volatile = field_info.IsVolatile();
4917 Primitive::Type field_type = field_info.GetFieldType();
4918 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004919 bool needs_write_barrier =
4920 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004921
4922 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004923 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004924 }
4925
Mark Mendell81489372015-11-04 11:30:41 -05004926 bool maybe_record_implicit_null_check_done = false;
4927
Calin Juravle52c48962014-12-16 17:02:57 +00004928 switch (field_type) {
4929 case Primitive::kPrimBoolean:
4930 case Primitive::kPrimByte: {
4931 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4932 break;
4933 }
4934
4935 case Primitive::kPrimShort:
4936 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004937 if (value.IsConstant()) {
4938 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4939 __ movw(Address(base, offset), Immediate(v));
4940 } else {
4941 __ movw(Address(base, offset), value.AsRegister<Register>());
4942 }
Calin Juravle52c48962014-12-16 17:02:57 +00004943 break;
4944 }
4945
4946 case Primitive::kPrimInt:
4947 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004948 if (kPoisonHeapReferences && needs_write_barrier) {
4949 // Note that in the case where `value` is a null reference,
4950 // we do not enter this block, as the reference does not
4951 // need poisoning.
4952 DCHECK_EQ(field_type, Primitive::kPrimNot);
4953 Register temp = locations->GetTemp(0).AsRegister<Register>();
4954 __ movl(temp, value.AsRegister<Register>());
4955 __ PoisonHeapReference(temp);
4956 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004957 } else if (value.IsConstant()) {
4958 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4959 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004960 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004961 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004962 __ movl(Address(base, offset), value.AsRegister<Register>());
4963 }
Calin Juravle52c48962014-12-16 17:02:57 +00004964 break;
4965 }
4966
4967 case Primitive::kPrimLong: {
4968 if (is_volatile) {
4969 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4970 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4971 __ movd(temp1, value.AsRegisterPairLow<Register>());
4972 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4973 __ punpckldq(temp1, temp2);
4974 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004975 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004976 } else if (value.IsConstant()) {
4977 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4978 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4979 codegen_->MaybeRecordImplicitNullCheck(instruction);
4980 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004981 } else {
4982 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004983 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004984 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4985 }
Mark Mendell81489372015-11-04 11:30:41 -05004986 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004987 break;
4988 }
4989
4990 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004991 if (value.IsConstant()) {
4992 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4993 __ movl(Address(base, offset), Immediate(v));
4994 } else {
4995 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4996 }
Calin Juravle52c48962014-12-16 17:02:57 +00004997 break;
4998 }
4999
5000 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005001 if (value.IsConstant()) {
5002 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5003 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5004 codegen_->MaybeRecordImplicitNullCheck(instruction);
5005 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5006 maybe_record_implicit_null_check_done = true;
5007 } else {
5008 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5009 }
Calin Juravle52c48962014-12-16 17:02:57 +00005010 break;
5011 }
5012
5013 case Primitive::kPrimVoid:
5014 LOG(FATAL) << "Unreachable type " << field_type;
5015 UNREACHABLE();
5016 }
5017
Mark Mendell81489372015-11-04 11:30:41 -05005018 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005019 codegen_->MaybeRecordImplicitNullCheck(instruction);
5020 }
5021
Roland Levillain4d027112015-07-01 15:41:14 +01005022 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005023 Register temp = locations->GetTemp(0).AsRegister<Register>();
5024 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005025 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005026 }
5027
Calin Juravle52c48962014-12-16 17:02:57 +00005028 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005029 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005030 }
5031}
5032
5033void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5034 HandleFieldGet(instruction, instruction->GetFieldInfo());
5035}
5036
5037void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5038 HandleFieldGet(instruction, instruction->GetFieldInfo());
5039}
5040
5041void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5042 HandleFieldSet(instruction, instruction->GetFieldInfo());
5043}
5044
5045void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005046 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005047}
5048
5049void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5050 HandleFieldSet(instruction, instruction->GetFieldInfo());
5051}
5052
5053void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005054 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005055}
5056
5057void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5058 HandleFieldGet(instruction, instruction->GetFieldInfo());
5059}
5060
5061void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5062 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005063}
5064
Calin Juravlee460d1d2015-09-29 04:52:17 +01005065void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5066 HUnresolvedInstanceFieldGet* instruction) {
5067 FieldAccessCallingConventionX86 calling_convention;
5068 codegen_->CreateUnresolvedFieldLocationSummary(
5069 instruction, instruction->GetFieldType(), calling_convention);
5070}
5071
5072void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5073 HUnresolvedInstanceFieldGet* instruction) {
5074 FieldAccessCallingConventionX86 calling_convention;
5075 codegen_->GenerateUnresolvedFieldAccess(instruction,
5076 instruction->GetFieldType(),
5077 instruction->GetFieldIndex(),
5078 instruction->GetDexPc(),
5079 calling_convention);
5080}
5081
5082void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5083 HUnresolvedInstanceFieldSet* instruction) {
5084 FieldAccessCallingConventionX86 calling_convention;
5085 codegen_->CreateUnresolvedFieldLocationSummary(
5086 instruction, instruction->GetFieldType(), calling_convention);
5087}
5088
5089void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5090 HUnresolvedInstanceFieldSet* instruction) {
5091 FieldAccessCallingConventionX86 calling_convention;
5092 codegen_->GenerateUnresolvedFieldAccess(instruction,
5093 instruction->GetFieldType(),
5094 instruction->GetFieldIndex(),
5095 instruction->GetDexPc(),
5096 calling_convention);
5097}
5098
5099void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5100 HUnresolvedStaticFieldGet* instruction) {
5101 FieldAccessCallingConventionX86 calling_convention;
5102 codegen_->CreateUnresolvedFieldLocationSummary(
5103 instruction, instruction->GetFieldType(), calling_convention);
5104}
5105
5106void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5107 HUnresolvedStaticFieldGet* instruction) {
5108 FieldAccessCallingConventionX86 calling_convention;
5109 codegen_->GenerateUnresolvedFieldAccess(instruction,
5110 instruction->GetFieldType(),
5111 instruction->GetFieldIndex(),
5112 instruction->GetDexPc(),
5113 calling_convention);
5114}
5115
5116void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5117 HUnresolvedStaticFieldSet* instruction) {
5118 FieldAccessCallingConventionX86 calling_convention;
5119 codegen_->CreateUnresolvedFieldLocationSummary(
5120 instruction, instruction->GetFieldType(), calling_convention);
5121}
5122
5123void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5124 HUnresolvedStaticFieldSet* instruction) {
5125 FieldAccessCallingConventionX86 calling_convention;
5126 codegen_->GenerateUnresolvedFieldAccess(instruction,
5127 instruction->GetFieldType(),
5128 instruction->GetFieldIndex(),
5129 instruction->GetDexPc(),
5130 calling_convention);
5131}
5132
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005133void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005134 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5135 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5136 ? Location::RequiresRegister()
5137 : Location::Any();
5138 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005139}
5140
Calin Juravle2ae48182016-03-16 14:05:09 +00005141void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5142 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005143 return;
5144 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005145 LocationSummary* locations = instruction->GetLocations();
5146 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005147
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005148 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005149 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005150}
5151
Calin Juravle2ae48182016-03-16 14:05:09 +00005152void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005153 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005154 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005155
5156 LocationSummary* locations = instruction->GetLocations();
5157 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005158
5159 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005160 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005161 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005162 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005163 } else {
5164 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005165 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005166 __ jmp(slow_path->GetEntryLabel());
5167 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005168 }
5169 __ j(kEqual, slow_path->GetEntryLabel());
5170}
5171
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005172void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005173 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005174}
5175
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005176void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005177 bool object_array_get_with_read_barrier =
5178 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005179 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005180 new (GetGraph()->GetArena()) LocationSummary(instruction,
5181 object_array_get_with_read_barrier ?
5182 LocationSummary::kCallOnSlowPath :
5183 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005184 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005185 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005186 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005187 locations->SetInAt(0, Location::RequiresRegister());
5188 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005189 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5190 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5191 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005192 // The output overlaps in case of long: we don't want the low move
5193 // to overwrite the array's location. Likewise, in the case of an
5194 // object array get with read barriers enabled, we do not want the
5195 // move to overwrite the array's location, as we need it to emit
5196 // the read barrier.
5197 locations->SetOut(
5198 Location::RequiresRegister(),
5199 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5200 Location::kOutputOverlap :
5201 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005202 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005203}
5204
5205void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5206 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005207 Location obj_loc = locations->InAt(0);
5208 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005209 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005210 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005211 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005212
Calin Juravle77520bc2015-01-12 18:45:46 +00005213 Primitive::Type type = instruction->GetType();
5214 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005215 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005216 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005217 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005218 break;
5219 }
5220
5221 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005222 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005223 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005224 break;
5225 }
5226
5227 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005228 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005229 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005230 break;
5231 }
5232
5233 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005234 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005235 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5236 // Branch cases into compressed and uncompressed for each index's type.
5237 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5238 NearLabel done, not_compressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005239 __ testl(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005240 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005241 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5242 "Expecting 0=compressed, 1=uncompressed");
5243 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005244 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5245 __ jmp(&done);
5246 __ Bind(&not_compressed);
5247 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5248 __ Bind(&done);
5249 } else {
5250 // Common case for charAt of array of char or when string compression's
5251 // feature is turned off.
5252 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5253 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 break;
5255 }
5256
Roland Levillain7c1559a2015-12-15 10:55:36 +00005257 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005258 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005259 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 break;
5261 }
5262
Roland Levillain7c1559a2015-12-15 10:55:36 +00005263 case Primitive::kPrimNot: {
5264 static_assert(
5265 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5266 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005267 // /* HeapReference<Object> */ out =
5268 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5269 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005270 // Note that a potential implicit null check is handled in this
5271 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5272 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005273 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005274 } else {
5275 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005276 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5277 codegen_->MaybeRecordImplicitNullCheck(instruction);
5278 // If read barriers are enabled, emit read barriers other than
5279 // Baker's using a slow path (and also unpoison the loaded
5280 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005281 if (index.IsConstant()) {
5282 uint32_t offset =
5283 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005284 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5285 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005286 codegen_->MaybeGenerateReadBarrierSlow(
5287 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5288 }
5289 }
5290 break;
5291 }
5292
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005294 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005295 __ movl(out_loc.AsRegisterPairLow<Register>(),
5296 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5297 codegen_->MaybeRecordImplicitNullCheck(instruction);
5298 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5299 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300 break;
5301 }
5302
Mark Mendell7c8d0092015-01-26 11:21:33 -05005303 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005304 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005305 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005306 break;
5307 }
5308
5309 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005310 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005311 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005312 break;
5313 }
5314
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005315 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005316 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005317 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005318 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005319
Roland Levillain7c1559a2015-12-15 10:55:36 +00005320 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5321 // Potential implicit null checks, in the case of reference or
5322 // long arrays, are handled in the previous switch statement.
5323 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005324 codegen_->MaybeRecordImplicitNullCheck(instruction);
5325 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326}
5327
5328void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005329 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005330
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005331 bool needs_write_barrier =
5332 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005333 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005334
Nicolas Geoffray39468442014-09-02 15:17:15 +01005335 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5336 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005337 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005338 LocationSummary::kCallOnSlowPath :
5339 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005340
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005341 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5342 || (value_type == Primitive::kPrimByte);
5343 // We need the inputs to be different than the output in case of long operation.
5344 // In case of a byte operation, the register allocator does not support multiple
5345 // inputs that die at entry with one in a specific register.
5346 locations->SetInAt(0, Location::RequiresRegister());
5347 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5348 if (is_byte_type) {
5349 // Ensure the value is in a byte register.
5350 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5351 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005352 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005353 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005354 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5355 }
5356 if (needs_write_barrier) {
5357 // Temporary registers for the write barrier.
5358 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5359 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005360 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005362}
5363
5364void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5365 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005366 Location array_loc = locations->InAt(0);
5367 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005368 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005369 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005370 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005371 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5372 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5373 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005374 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005375 bool needs_write_barrier =
5376 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005377
5378 switch (value_type) {
5379 case Primitive::kPrimBoolean:
5380 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005381 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005382 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005383 if (value.IsRegister()) {
5384 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005385 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005386 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005387 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005388 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005389 break;
5390 }
5391
5392 case Primitive::kPrimShort:
5393 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005394 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005395 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005396 if (value.IsRegister()) {
5397 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005398 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005399 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005400 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005401 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005402 break;
5403 }
5404
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005405 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005406 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005407 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005408
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005409 if (!value.IsRegister()) {
5410 // Just setting null.
5411 DCHECK(instruction->InputAt(2)->IsNullConstant());
5412 DCHECK(value.IsConstant()) << value;
5413 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005414 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005415 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005416 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005417 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005418 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005419
5420 DCHECK(needs_write_barrier);
5421 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005422 // We cannot use a NearLabel for `done`, as its range may be too
5423 // short when Baker read barriers are enabled.
5424 Label done;
5425 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005426 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005427 Location temp_loc = locations->GetTemp(0);
5428 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005429 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005430 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5431 codegen_->AddSlowPath(slow_path);
5432 if (instruction->GetValueCanBeNull()) {
5433 __ testl(register_value, register_value);
5434 __ j(kNotEqual, &not_null);
5435 __ movl(address, Immediate(0));
5436 codegen_->MaybeRecordImplicitNullCheck(instruction);
5437 __ jmp(&done);
5438 __ Bind(&not_null);
5439 }
5440
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005441 // Note that when Baker read barriers are enabled, the type
5442 // checks are performed without read barriers. This is fine,
5443 // even in the case where a class object is in the from-space
5444 // after the flip, as a comparison involving such a type would
5445 // not produce a false positive; it may of course produce a
5446 // false negative, in which case we would take the ArraySet
5447 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005448
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005449 // /* HeapReference<Class> */ temp = array->klass_
5450 __ movl(temp, Address(array, class_offset));
5451 codegen_->MaybeRecordImplicitNullCheck(instruction);
5452 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005453
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005454 // /* HeapReference<Class> */ temp = temp->component_type_
5455 __ movl(temp, Address(temp, component_offset));
5456 // If heap poisoning is enabled, no need to unpoison `temp`
5457 // nor the object reference in `register_value->klass`, as
5458 // we are comparing two poisoned references.
5459 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005460
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005461 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5462 __ j(kEqual, &do_put);
5463 // If heap poisoning is enabled, the `temp` reference has
5464 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005465 __ MaybeUnpoisonHeapReference(temp);
5466
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005467 // If heap poisoning is enabled, no need to unpoison the
5468 // heap reference loaded below, as it is only used for a
5469 // comparison with null.
5470 __ cmpl(Address(temp, super_offset), Immediate(0));
5471 __ j(kNotEqual, slow_path->GetEntryLabel());
5472 __ Bind(&do_put);
5473 } else {
5474 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005475 }
5476 }
5477
5478 if (kPoisonHeapReferences) {
5479 __ movl(temp, register_value);
5480 __ PoisonHeapReference(temp);
5481 __ movl(address, temp);
5482 } else {
5483 __ movl(address, register_value);
5484 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005485 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005486 codegen_->MaybeRecordImplicitNullCheck(instruction);
5487 }
5488
5489 Register card = locations->GetTemp(1).AsRegister<Register>();
5490 codegen_->MarkGCCard(
5491 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5492 __ Bind(&done);
5493
5494 if (slow_path != nullptr) {
5495 __ Bind(slow_path->GetExitLabel());
5496 }
5497
5498 break;
5499 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005500
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005501 case Primitive::kPrimInt: {
5502 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005503 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005504 if (value.IsRegister()) {
5505 __ movl(address, value.AsRegister<Register>());
5506 } else {
5507 DCHECK(value.IsConstant()) << value;
5508 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5509 __ movl(address, Immediate(v));
5510 }
5511 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005512 break;
5513 }
5514
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005515 case Primitive::kPrimLong: {
5516 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005517 if (value.IsRegisterPair()) {
5518 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5519 value.AsRegisterPairLow<Register>());
5520 codegen_->MaybeRecordImplicitNullCheck(instruction);
5521 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5522 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005523 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005524 DCHECK(value.IsConstant());
5525 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5526 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5527 Immediate(Low32Bits(val)));
5528 codegen_->MaybeRecordImplicitNullCheck(instruction);
5529 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5530 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005531 }
5532 break;
5533 }
5534
Mark Mendell7c8d0092015-01-26 11:21:33 -05005535 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005536 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005537 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005538 if (value.IsFpuRegister()) {
5539 __ movss(address, value.AsFpuRegister<XmmRegister>());
5540 } else {
5541 DCHECK(value.IsConstant());
5542 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5543 __ movl(address, Immediate(v));
5544 }
5545 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005546 break;
5547 }
5548
5549 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005550 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005551 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005552 if (value.IsFpuRegister()) {
5553 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5554 } else {
5555 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005556 Address address_hi =
5557 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005558 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5559 __ movl(address, Immediate(Low32Bits(v)));
5560 codegen_->MaybeRecordImplicitNullCheck(instruction);
5561 __ movl(address_hi, Immediate(High32Bits(v)));
5562 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005563 break;
5564 }
5565
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005566 case Primitive::kPrimVoid:
5567 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005568 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005569 }
5570}
5571
5572void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5573 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005574 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005575 if (!instruction->IsEmittedAtUseSite()) {
5576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5577 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005578}
5579
5580void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005581 if (instruction->IsEmittedAtUseSite()) {
5582 return;
5583 }
5584
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005585 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005586 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005587 Register obj = locations->InAt(0).AsRegister<Register>();
5588 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005589 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005590 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005591 // Mask out most significant bit in case the array is String's array of char.
5592 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005593 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005594 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005595}
5596
5597void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005598 RegisterSet caller_saves = RegisterSet::Empty();
5599 InvokeRuntimeCallingConvention calling_convention;
5600 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5601 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5602 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005603 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005604 HInstruction* length = instruction->InputAt(1);
5605 if (!length->IsEmittedAtUseSite()) {
5606 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5607 }
jessicahandojo4877b792016-09-08 19:49:13 -07005608 // Need register to see array's length.
5609 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5610 locations->AddTemp(Location::RequiresRegister());
5611 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005612}
5613
5614void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005615 const bool is_string_compressed_char_at =
5616 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005617 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005618 Location index_loc = locations->InAt(0);
5619 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005620 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005621 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005622
Mark Mendell99dbd682015-04-22 16:18:52 -04005623 if (length_loc.IsConstant()) {
5624 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5625 if (index_loc.IsConstant()) {
5626 // BCE will remove the bounds check if we are guarenteed to pass.
5627 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5628 if (index < 0 || index >= length) {
5629 codegen_->AddSlowPath(slow_path);
5630 __ jmp(slow_path->GetEntryLabel());
5631 } else {
5632 // Some optimization after BCE may have generated this, and we should not
5633 // generate a bounds check if it is a valid range.
5634 }
5635 return;
5636 }
5637
5638 // We have to reverse the jump condition because the length is the constant.
5639 Register index_reg = index_loc.AsRegister<Register>();
5640 __ cmpl(index_reg, Immediate(length));
5641 codegen_->AddSlowPath(slow_path);
5642 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005643 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005644 HInstruction* array_length = instruction->InputAt(1);
5645 if (array_length->IsEmittedAtUseSite()) {
5646 // Address the length field in the array.
5647 DCHECK(array_length->IsArrayLength());
5648 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5649 Location array_loc = array_length->GetLocations()->InAt(0);
5650 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005651 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005652 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5653 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005654 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5655 __ movl(length_reg, array_len);
5656 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005657 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005658 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005659 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005660 // Checking bounds for general case:
5661 // Array of char or string's array with feature compression off.
5662 if (index_loc.IsConstant()) {
5663 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5664 __ cmpl(array_len, Immediate(value));
5665 } else {
5666 __ cmpl(array_len, index_loc.AsRegister<Register>());
5667 }
5668 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005669 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005670 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005671 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005672 }
5673 codegen_->AddSlowPath(slow_path);
5674 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005675 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005676}
5677
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005678void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005679 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005680}
5681
5682void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005683 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5684}
5685
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005686void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005687 LocationSummary* locations =
5688 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005689 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005690}
5691
5692void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005693 HBasicBlock* block = instruction->GetBlock();
5694 if (block->GetLoopInformation() != nullptr) {
5695 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5696 // The back edge will generate the suspend check.
5697 return;
5698 }
5699 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5700 // The goto will generate the suspend check.
5701 return;
5702 }
5703 GenerateSuspendCheck(instruction, nullptr);
5704}
5705
5706void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5707 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005708 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005709 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5710 if (slow_path == nullptr) {
5711 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5712 instruction->SetSlowPath(slow_path);
5713 codegen_->AddSlowPath(slow_path);
5714 if (successor != nullptr) {
5715 DCHECK(successor->IsLoopHeader());
5716 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5717 }
5718 } else {
5719 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5720 }
5721
Andreas Gampe542451c2016-07-26 09:02:02 -07005722 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005723 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005724 if (successor == nullptr) {
5725 __ j(kNotEqual, slow_path->GetEntryLabel());
5726 __ Bind(slow_path->GetReturnLabel());
5727 } else {
5728 __ j(kEqual, codegen_->GetLabelOf(successor));
5729 __ jmp(slow_path->GetEntryLabel());
5730 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005731}
5732
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005733X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5734 return codegen_->GetAssembler();
5735}
5736
Mark Mendell7c8d0092015-01-26 11:21:33 -05005737void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005738 ScratchRegisterScope ensure_scratch(
5739 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5740 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5741 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5742 __ movl(temp_reg, Address(ESP, src + stack_offset));
5743 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005744}
5745
5746void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005747 ScratchRegisterScope ensure_scratch(
5748 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5749 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5750 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5751 __ movl(temp_reg, Address(ESP, src + stack_offset));
5752 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5753 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5754 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005755}
5756
5757void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005758 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005759 Location source = move->GetSource();
5760 Location destination = move->GetDestination();
5761
5762 if (source.IsRegister()) {
5763 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005764 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005765 } else if (destination.IsFpuRegister()) {
5766 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005767 } else {
5768 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005769 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005770 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005771 } else if (source.IsRegisterPair()) {
5772 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5773 // Create stack space for 2 elements.
5774 __ subl(ESP, Immediate(2 * elem_size));
5775 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5776 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5777 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5778 // And remove the temporary stack space we allocated.
5779 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005780 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005781 if (destination.IsRegister()) {
5782 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5783 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005784 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005785 } else if (destination.IsRegisterPair()) {
5786 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5787 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5788 __ psrlq(src_reg, Immediate(32));
5789 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005790 } else if (destination.IsStackSlot()) {
5791 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5792 } else {
5793 DCHECK(destination.IsDoubleStackSlot());
5794 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5795 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005796 } else if (source.IsStackSlot()) {
5797 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005798 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005799 } else if (destination.IsFpuRegister()) {
5800 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005801 } else {
5802 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005803 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5804 }
5805 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005806 if (destination.IsRegisterPair()) {
5807 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5808 __ movl(destination.AsRegisterPairHigh<Register>(),
5809 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5810 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005811 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5812 } else {
5813 DCHECK(destination.IsDoubleStackSlot()) << destination;
5814 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005815 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005816 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005817 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005818 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005819 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005820 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005821 if (value == 0) {
5822 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5823 } else {
5824 __ movl(destination.AsRegister<Register>(), Immediate(value));
5825 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005826 } else {
5827 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005828 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005829 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005830 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005831 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005832 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005833 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005834 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005835 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5836 if (value == 0) {
5837 // Easy handling of 0.0.
5838 __ xorps(dest, dest);
5839 } else {
5840 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005841 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5842 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5843 __ movl(temp, Immediate(value));
5844 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005845 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005846 } else {
5847 DCHECK(destination.IsStackSlot()) << destination;
5848 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5849 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005850 } else if (constant->IsLongConstant()) {
5851 int64_t value = constant->AsLongConstant()->GetValue();
5852 int32_t low_value = Low32Bits(value);
5853 int32_t high_value = High32Bits(value);
5854 Immediate low(low_value);
5855 Immediate high(high_value);
5856 if (destination.IsDoubleStackSlot()) {
5857 __ movl(Address(ESP, destination.GetStackIndex()), low);
5858 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5859 } else {
5860 __ movl(destination.AsRegisterPairLow<Register>(), low);
5861 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5862 }
5863 } else {
5864 DCHECK(constant->IsDoubleConstant());
5865 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005866 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005867 int32_t low_value = Low32Bits(value);
5868 int32_t high_value = High32Bits(value);
5869 Immediate low(low_value);
5870 Immediate high(high_value);
5871 if (destination.IsFpuRegister()) {
5872 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5873 if (value == 0) {
5874 // Easy handling of 0.0.
5875 __ xorpd(dest, dest);
5876 } else {
5877 __ pushl(high);
5878 __ pushl(low);
5879 __ movsd(dest, Address(ESP, 0));
5880 __ addl(ESP, Immediate(8));
5881 }
5882 } else {
5883 DCHECK(destination.IsDoubleStackSlot()) << destination;
5884 __ movl(Address(ESP, destination.GetStackIndex()), low);
5885 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5886 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005887 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005888 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005889 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005890 }
5891}
5892
Mark Mendella5c19ce2015-04-01 12:51:05 -04005893void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005894 Register suggested_scratch = reg == EAX ? EBX : EAX;
5895 ScratchRegisterScope ensure_scratch(
5896 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5897
5898 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5899 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5900 __ movl(Address(ESP, mem + stack_offset), reg);
5901 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005902}
5903
Mark Mendell7c8d0092015-01-26 11:21:33 -05005904void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005905 ScratchRegisterScope ensure_scratch(
5906 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5907
5908 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5909 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5910 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5911 __ movss(Address(ESP, mem + stack_offset), reg);
5912 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005913}
5914
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005915void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005916 ScratchRegisterScope ensure_scratch1(
5917 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005918
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005919 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5920 ScratchRegisterScope ensure_scratch2(
5921 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005922
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005923 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5924 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5925 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5926 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5927 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5928 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005929}
5930
5931void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005932 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005933 Location source = move->GetSource();
5934 Location destination = move->GetDestination();
5935
5936 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005937 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5938 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5939 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5940 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5941 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005942 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005943 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005944 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005945 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005946 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5947 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005948 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5949 // Use XOR Swap algorithm to avoid a temporary.
5950 DCHECK_NE(source.reg(), destination.reg());
5951 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5952 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5953 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5954 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5955 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5956 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5957 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005958 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5959 // Take advantage of the 16 bytes in the XMM register.
5960 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5961 Address stack(ESP, destination.GetStackIndex());
5962 // Load the double into the high doubleword.
5963 __ movhpd(reg, stack);
5964
5965 // Store the low double into the destination.
5966 __ movsd(stack, reg);
5967
5968 // Move the high double to the low double.
5969 __ psrldq(reg, Immediate(8));
5970 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5971 // Take advantage of the 16 bytes in the XMM register.
5972 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5973 Address stack(ESP, source.GetStackIndex());
5974 // Load the double into the high doubleword.
5975 __ movhpd(reg, stack);
5976
5977 // Store the low double into the destination.
5978 __ movsd(stack, reg);
5979
5980 // Move the high double to the low double.
5981 __ psrldq(reg, Immediate(8));
5982 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5983 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5984 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005985 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005986 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005987 }
5988}
5989
5990void ParallelMoveResolverX86::SpillScratch(int reg) {
5991 __ pushl(static_cast<Register>(reg));
5992}
5993
5994void ParallelMoveResolverX86::RestoreScratch(int reg) {
5995 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005996}
5997
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005998HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5999 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006000 switch (desired_class_load_kind) {
6001 case HLoadClass::LoadKind::kReferrersClass:
6002 break;
6003 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6004 DCHECK(!GetCompilerOptions().GetCompilePic());
6005 break;
6006 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6007 DCHECK(GetCompilerOptions().GetCompilePic());
6008 FALLTHROUGH_INTENDED;
6009 case HLoadClass::LoadKind::kDexCachePcRelative:
6010 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
6011 // We disable pc-relative load when there is an irreducible loop, as the optimization
6012 // is incompatible with it.
6013 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6014 // with irreducible loops.
6015 if (GetGraph()->HasIrreducibleLoops()) {
6016 return HLoadClass::LoadKind::kDexCacheViaMethod;
6017 }
6018 break;
6019 case HLoadClass::LoadKind::kBootImageAddress:
6020 break;
6021 case HLoadClass::LoadKind::kDexCacheAddress:
6022 DCHECK(Runtime::Current()->UseJitCompilation());
6023 break;
6024 case HLoadClass::LoadKind::kDexCacheViaMethod:
6025 break;
6026 }
6027 return desired_class_load_kind;
6028}
6029
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006030void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006031 if (cls->NeedsAccessCheck()) {
6032 InvokeRuntimeCallingConvention calling_convention;
6033 CodeGenerator::CreateLoadClassLocationSummary(
6034 cls,
6035 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6036 Location::RegisterLocation(EAX),
6037 /* code_generator_supports_read_barrier */ true);
6038 return;
6039 }
6040
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006041 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6042 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006043 ? LocationSummary::kCallOnSlowPath
6044 : LocationSummary::kNoCall;
6045 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006046 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006047 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006048 }
6049
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006050 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6051 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6052 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6053 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6054 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6055 locations->SetInAt(0, Location::RequiresRegister());
6056 }
6057 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006058}
6059
6060void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006061 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006062 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08006063 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescuba45db02016-07-12 22:53:02 +01006064 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006065 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006066 return;
6067 }
6068
Roland Levillain0d5a2812015-11-13 10:07:31 +00006069 Location out_loc = locations->Out();
6070 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006071
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006072 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006073 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6074 ? kWithoutReadBarrier
6075 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006076 switch (cls->GetLoadKind()) {
6077 case HLoadClass::LoadKind::kReferrersClass: {
6078 DCHECK(!cls->CanCallRuntime());
6079 DCHECK(!cls->MustGenerateClinitCheck());
6080 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6081 Register current_method = locations->InAt(0).AsRegister<Register>();
6082 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006083 cls,
6084 out_loc,
6085 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006086 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006087 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006088 break;
6089 }
6090 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006091 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006092 __ movl(out, Immediate(/* placeholder */ 0));
6093 codegen_->RecordTypePatch(cls);
6094 break;
6095 }
6096 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006097 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006098 Register method_address = locations->InAt(0).AsRegister<Register>();
6099 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6100 codegen_->RecordTypePatch(cls);
6101 break;
6102 }
6103 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006104 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006105 DCHECK_NE(cls->GetAddress(), 0u);
6106 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6107 __ movl(out, Immediate(address));
6108 codegen_->RecordSimplePatch();
6109 break;
6110 }
6111 case HLoadClass::LoadKind::kDexCacheAddress: {
6112 DCHECK_NE(cls->GetAddress(), 0u);
6113 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6114 // /* GcRoot<mirror::Class> */ out = *address
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006115 GenerateGcRootFieldLoad(cls,
6116 out_loc,
6117 Address::Absolute(address),
Roland Levillain00468f32016-10-27 18:02:48 +01006118 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006119 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006120 generate_null_check = !cls->IsInDexCache();
6121 break;
6122 }
6123 case HLoadClass::LoadKind::kDexCachePcRelative: {
6124 Register base_reg = locations->InAt(0).AsRegister<Register>();
6125 uint32_t offset = cls->GetDexCacheElementOffset();
6126 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6127 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006128 GenerateGcRootFieldLoad(cls,
6129 out_loc,
6130 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
6131 fixup_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006132 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006133 generate_null_check = !cls->IsInDexCache();
6134 break;
6135 }
6136 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6137 // /* GcRoot<mirror::Class>[] */ out =
6138 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6139 Register current_method = locations->InAt(0).AsRegister<Register>();
6140 __ movl(out, Address(current_method,
6141 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6142 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006143 GenerateGcRootFieldLoad(cls,
6144 out_loc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006145 Address(out,
6146 CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_)),
Roland Levillain00468f32016-10-27 18:02:48 +01006147 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006148 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006149 generate_null_check = !cls->IsInDexCache();
6150 break;
6151 }
6152 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006153
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006154 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6155 DCHECK(cls->CanCallRuntime());
6156 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6157 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6158 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006159
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006160 if (generate_null_check) {
6161 __ testl(out, out);
6162 __ j(kEqual, slow_path->GetEntryLabel());
6163 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006164
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006165 if (cls->MustGenerateClinitCheck()) {
6166 GenerateClassInitializationCheck(slow_path, out);
6167 } else {
6168 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006169 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006170 }
6171}
6172
6173void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6174 LocationSummary* locations =
6175 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6176 locations->SetInAt(0, Location::RequiresRegister());
6177 if (check->HasUses()) {
6178 locations->SetOut(Location::SameAsFirstInput());
6179 }
6180}
6181
6182void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006183 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006184 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006185 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006186 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006187 GenerateClassInitializationCheck(slow_path,
6188 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006189}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006190
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006191void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006192 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006193 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6194 Immediate(mirror::Class::kStatusInitialized));
6195 __ j(kLess, slow_path->GetEntryLabel());
6196 __ Bind(slow_path->GetExitLabel());
6197 // No need for memory fence, thanks to the X86 memory model.
6198}
6199
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006200HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6201 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006202 switch (desired_string_load_kind) {
6203 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6204 DCHECK(!GetCompilerOptions().GetCompilePic());
6205 break;
6206 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6207 DCHECK(GetCompilerOptions().GetCompilePic());
6208 FALLTHROUGH_INTENDED;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006209 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006210 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006211 // We disable pc-relative load when there is an irreducible loop, as the optimization
6212 // is incompatible with it.
6213 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6214 // with irreducible loops.
6215 if (GetGraph()->HasIrreducibleLoops()) {
6216 return HLoadString::LoadKind::kDexCacheViaMethod;
6217 }
6218 break;
6219 case HLoadString::LoadKind::kBootImageAddress:
6220 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006221 case HLoadString::LoadKind::kDexCacheViaMethod:
6222 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006223 case HLoadString::LoadKind::kJitTableAddress:
6224 DCHECK(Runtime::Current()->UseJitCompilation());
6225 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006226 }
6227 return desired_string_load_kind;
6228}
6229
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006230void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006231 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006232 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006233 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006234 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006235 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006236 locations->SetInAt(0, Location::RequiresRegister());
6237 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006238 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6239 locations->SetOut(Location::RegisterLocation(EAX));
6240 } else {
6241 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006242 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6243 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6244 // Rely on the pResolveString and/or marking to save everything.
6245 RegisterSet caller_saves = RegisterSet::Empty();
6246 InvokeRuntimeCallingConvention calling_convention;
6247 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6248 locations->SetCustomSlowPathCallerSaves(caller_saves);
6249 } else {
6250 // For non-Baker read barrier we have a temp-clobbering call.
6251 }
6252 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006253 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006254}
6255
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006256Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file, uint32_t dex_index) {
6257 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index), /* placeholder */ 0u);
6258 // Add a patch entry and return the label.
6259 jit_string_patches_.emplace_back(dex_file, dex_index);
6260 PatchInfo<Label>* info = &jit_string_patches_.back();
6261 return &info->label;
6262}
6263
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006264void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006265 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006266 Location out_loc = locations->Out();
6267 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006268
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006269 switch (load->GetLoadKind()) {
6270 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006271 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006272 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006273 return; // No dex cache slow path.
6274 }
6275 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006276 Register method_address = locations->InAt(0).AsRegister<Register>();
6277 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006278 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006279 return; // No dex cache slow path.
6280 }
6281 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006282 DCHECK_NE(load->GetAddress(), 0u);
6283 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6284 __ movl(out, Immediate(address));
6285 codegen_->RecordSimplePatch();
6286 return; // No dex cache slow path.
6287 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006288 case HLoadString::LoadKind::kBssEntry: {
6289 Register method_address = locations->InAt(0).AsRegister<Register>();
6290 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6291 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006292 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006293 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006294 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6295 codegen_->AddSlowPath(slow_path);
6296 __ testl(out, out);
6297 __ j(kEqual, slow_path->GetEntryLabel());
6298 __ Bind(slow_path->GetExitLabel());
6299 return;
6300 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006301 case HLoadString::LoadKind::kJitTableAddress: {
6302 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6303 Label* fixup_label = codegen_->NewJitRootStringPatch(
6304 load->GetDexFile(), load->GetStringIndex());
6305 // /* GcRoot<mirror::String> */ out = *address
6306 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6307 return;
6308 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006309 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006310 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006311 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006312
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006313 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006314 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006315 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006316 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex()));
6317 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6318 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006319}
6320
David Brazdilcb1c0552015-08-04 16:22:25 +01006321static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006322 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006323}
6324
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006325void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6326 LocationSummary* locations =
6327 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6328 locations->SetOut(Location::RequiresRegister());
6329}
6330
6331void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006332 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6333}
6334
6335void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6336 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6337}
6338
6339void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6340 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006341}
6342
6343void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6344 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006345 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006346 InvokeRuntimeCallingConvention calling_convention;
6347 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6348}
6349
6350void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006351 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006352 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006353}
6354
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006355// Temp is used for read barrier.
6356static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6357 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006358 !kUseBakerReadBarrier &&
6359 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006360 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006361 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6362 return 1;
6363 }
6364 return 0;
6365}
6366
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006367// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006368// interface pointer, one for loading the current interface.
6369// The other checks have one temp for loading the object's class.
6370static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6371 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6372 return 2;
6373 }
6374 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006375}
6376
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006377void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006378 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006379 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006380 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006381 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006382 case TypeCheckKind::kExactCheck:
6383 case TypeCheckKind::kAbstractClassCheck:
6384 case TypeCheckKind::kClassHierarchyCheck:
6385 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006386 call_kind =
6387 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006388 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006389 break;
6390 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006391 case TypeCheckKind::kUnresolvedCheck:
6392 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006393 call_kind = LocationSummary::kCallOnSlowPath;
6394 break;
6395 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006396
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006397 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006398 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006399 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006400 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006401 locations->SetInAt(0, Location::RequiresRegister());
6402 locations->SetInAt(1, Location::Any());
6403 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6404 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006405 // When read barriers are enabled, we need a temporary register for some cases.
6406 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006407}
6408
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006409void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006410 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006411 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412 Location obj_loc = locations->InAt(0);
6413 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006414 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006415 Location out_loc = locations->Out();
6416 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006417 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6418 DCHECK_LE(num_temps, 1u);
6419 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006420 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006421 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6422 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6423 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006424 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006425 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006426
6427 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006428 // Avoid null check if we know obj is not null.
6429 if (instruction->MustDoNullCheck()) {
6430 __ testl(obj, obj);
6431 __ j(kEqual, &zero);
6432 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006433
Roland Levillain7c1559a2015-12-15 10:55:36 +00006434 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006435 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006436 // /* HeapReference<Class> */ out = obj->klass_
6437 GenerateReferenceLoadTwoRegisters(instruction,
6438 out_loc,
6439 obj_loc,
6440 class_offset,
6441 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006442 if (cls.IsRegister()) {
6443 __ cmpl(out, cls.AsRegister<Register>());
6444 } else {
6445 DCHECK(cls.IsStackSlot()) << cls;
6446 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6447 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006448
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006449 // Classes must be equal for the instanceof to succeed.
6450 __ j(kNotEqual, &zero);
6451 __ movl(out, Immediate(1));
6452 __ jmp(&done);
6453 break;
6454 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006455
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006456 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006457 // /* HeapReference<Class> */ out = obj->klass_
6458 GenerateReferenceLoadTwoRegisters(instruction,
6459 out_loc,
6460 obj_loc,
6461 class_offset,
6462 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006463 // If the class is abstract, we eagerly fetch the super class of the
6464 // object to avoid doing a comparison we know will fail.
6465 NearLabel loop;
6466 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006467 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006468 GenerateReferenceLoadOneRegister(instruction,
6469 out_loc,
6470 super_offset,
6471 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006472 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006473 __ testl(out, out);
6474 // If `out` is null, we use it for the result, and jump to `done`.
6475 __ j(kEqual, &done);
6476 if (cls.IsRegister()) {
6477 __ cmpl(out, cls.AsRegister<Register>());
6478 } else {
6479 DCHECK(cls.IsStackSlot()) << cls;
6480 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6481 }
6482 __ j(kNotEqual, &loop);
6483 __ movl(out, Immediate(1));
6484 if (zero.IsLinked()) {
6485 __ jmp(&done);
6486 }
6487 break;
6488 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006489
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006490 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006491 // /* HeapReference<Class> */ out = obj->klass_
6492 GenerateReferenceLoadTwoRegisters(instruction,
6493 out_loc,
6494 obj_loc,
6495 class_offset,
6496 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006497 // Walk over the class hierarchy to find a match.
6498 NearLabel loop, success;
6499 __ Bind(&loop);
6500 if (cls.IsRegister()) {
6501 __ cmpl(out, cls.AsRegister<Register>());
6502 } else {
6503 DCHECK(cls.IsStackSlot()) << cls;
6504 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6505 }
6506 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006507 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006508 GenerateReferenceLoadOneRegister(instruction,
6509 out_loc,
6510 super_offset,
6511 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006512 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006513 __ testl(out, out);
6514 __ j(kNotEqual, &loop);
6515 // If `out` is null, we use it for the result, and jump to `done`.
6516 __ jmp(&done);
6517 __ Bind(&success);
6518 __ movl(out, Immediate(1));
6519 if (zero.IsLinked()) {
6520 __ jmp(&done);
6521 }
6522 break;
6523 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006524
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006525 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006526 // /* HeapReference<Class> */ out = obj->klass_
6527 GenerateReferenceLoadTwoRegisters(instruction,
6528 out_loc,
6529 obj_loc,
6530 class_offset,
6531 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006532 // Do an exact check.
6533 NearLabel exact_check;
6534 if (cls.IsRegister()) {
6535 __ cmpl(out, cls.AsRegister<Register>());
6536 } else {
6537 DCHECK(cls.IsStackSlot()) << cls;
6538 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6539 }
6540 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006541 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006542 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006543 GenerateReferenceLoadOneRegister(instruction,
6544 out_loc,
6545 component_offset,
6546 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006547 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006548 __ testl(out, out);
6549 // If `out` is null, we use it for the result, and jump to `done`.
6550 __ j(kEqual, &done);
6551 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6552 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006553 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006554 __ movl(out, Immediate(1));
6555 __ jmp(&done);
6556 break;
6557 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006558
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006559 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006560 // No read barrier since the slow path will retry upon failure.
6561 // /* HeapReference<Class> */ out = obj->klass_
6562 GenerateReferenceLoadTwoRegisters(instruction,
6563 out_loc,
6564 obj_loc,
6565 class_offset,
6566 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006567 if (cls.IsRegister()) {
6568 __ cmpl(out, cls.AsRegister<Register>());
6569 } else {
6570 DCHECK(cls.IsStackSlot()) << cls;
6571 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6572 }
6573 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006574 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6575 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006576 codegen_->AddSlowPath(slow_path);
6577 __ j(kNotEqual, slow_path->GetEntryLabel());
6578 __ movl(out, Immediate(1));
6579 if (zero.IsLinked()) {
6580 __ jmp(&done);
6581 }
6582 break;
6583 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006584
Calin Juravle98893e12015-10-02 21:05:03 +01006585 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006586 case TypeCheckKind::kInterfaceCheck: {
6587 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006588 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006589 // cases.
6590 //
6591 // We cannot directly call the InstanceofNonTrivial runtime
6592 // entry point without resorting to a type checking slow path
6593 // here (i.e. by calling InvokeRuntime directly), as it would
6594 // require to assign fixed registers for the inputs of this
6595 // HInstanceOf instruction (following the runtime calling
6596 // convention), which might be cluttered by the potential first
6597 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006598 //
6599 // TODO: Introduce a new runtime entry point taking the object
6600 // to test (instead of its class) as argument, and let it deal
6601 // with the read barrier issues. This will let us refactor this
6602 // case of the `switch` code as it was previously (with a direct
6603 // call to the runtime not using a type checking slow path).
6604 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006605 DCHECK(locations->OnlyCallsOnSlowPath());
6606 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6607 /* is_fatal */ false);
6608 codegen_->AddSlowPath(slow_path);
6609 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006610 if (zero.IsLinked()) {
6611 __ jmp(&done);
6612 }
6613 break;
6614 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006615 }
6616
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006617 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006618 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006619 __ xorl(out, out);
6620 }
6621
6622 if (done.IsLinked()) {
6623 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006624 }
6625
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006626 if (slow_path != nullptr) {
6627 __ Bind(slow_path->GetExitLabel());
6628 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006629}
6630
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006631static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6632 switch (type_check_kind) {
6633 case TypeCheckKind::kExactCheck:
6634 case TypeCheckKind::kAbstractClassCheck:
6635 case TypeCheckKind::kClassHierarchyCheck:
6636 case TypeCheckKind::kArrayObjectCheck:
6637 return !throws_into_catch && !kEmitCompilerReadBarrier;
6638 case TypeCheckKind::kInterfaceCheck:
6639 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6640 case TypeCheckKind::kArrayCheck:
6641 case TypeCheckKind::kUnresolvedCheck:
6642 return false;
6643 }
6644 LOG(FATAL) << "Unreachable";
6645 UNREACHABLE();
6646}
6647
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006648void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006649 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006650 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006651 LocationSummary::CallKind call_kind =
6652 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6653 ? LocationSummary::kNoCall
6654 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006655 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6656 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006657 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6658 // Require a register for the interface check since there is a loop that compares the class to
6659 // a memory address.
6660 locations->SetInAt(1, Location::RequiresRegister());
6661 } else {
6662 locations->SetInAt(1, Location::Any());
6663 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006664 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6665 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006666 // When read barriers are enabled, we need an additional temporary register for some cases.
6667 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6668}
6669
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006670void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006671 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006672 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006673 Location obj_loc = locations->InAt(0);
6674 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006675 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006676 Location temp_loc = locations->GetTemp(0);
6677 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006678 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6679 DCHECK_GE(num_temps, 1u);
6680 DCHECK_LE(num_temps, 2u);
6681 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6682 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6683 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6684 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6685 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6686 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6687 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6688 const uint32_t object_array_data_offset =
6689 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006690
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006691 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6692 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6693 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006694 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006695 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6696
Roland Levillain0d5a2812015-11-13 10:07:31 +00006697 SlowPathCode* type_check_slow_path =
6698 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6699 is_type_check_slow_path_fatal);
6700 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006701
Roland Levillain0d5a2812015-11-13 10:07:31 +00006702 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006703 // Avoid null check if we know obj is not null.
6704 if (instruction->MustDoNullCheck()) {
6705 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006706 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006707 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006708
Roland Levillain0d5a2812015-11-13 10:07:31 +00006709 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006710 case TypeCheckKind::kExactCheck:
6711 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006712 // /* HeapReference<Class> */ temp = obj->klass_
6713 GenerateReferenceLoadTwoRegisters(instruction,
6714 temp_loc,
6715 obj_loc,
6716 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006717 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006718
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006719 if (cls.IsRegister()) {
6720 __ cmpl(temp, cls.AsRegister<Register>());
6721 } else {
6722 DCHECK(cls.IsStackSlot()) << cls;
6723 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6724 }
6725 // Jump to slow path for throwing the exception or doing a
6726 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006727 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006728 break;
6729 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006730
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006731 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006732 // /* HeapReference<Class> */ temp = obj->klass_
6733 GenerateReferenceLoadTwoRegisters(instruction,
6734 temp_loc,
6735 obj_loc,
6736 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006737 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006738
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006739 // If the class is abstract, we eagerly fetch the super class of the
6740 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006741 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006742 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006743 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006744 GenerateReferenceLoadOneRegister(instruction,
6745 temp_loc,
6746 super_offset,
6747 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006748 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006749
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006750 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6751 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006752 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006753 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006754
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006755 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006756 if (cls.IsRegister()) {
6757 __ cmpl(temp, cls.AsRegister<Register>());
6758 } else {
6759 DCHECK(cls.IsStackSlot()) << cls;
6760 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6761 }
6762 __ j(kNotEqual, &loop);
6763 break;
6764 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006765
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006766 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006767 // /* HeapReference<Class> */ temp = obj->klass_
6768 GenerateReferenceLoadTwoRegisters(instruction,
6769 temp_loc,
6770 obj_loc,
6771 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006772 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006773
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006774 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006775 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006776 __ Bind(&loop);
6777 if (cls.IsRegister()) {
6778 __ cmpl(temp, cls.AsRegister<Register>());
6779 } else {
6780 DCHECK(cls.IsStackSlot()) << cls;
6781 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6782 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006783 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006784
Roland Levillain0d5a2812015-11-13 10:07:31 +00006785 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006786 GenerateReferenceLoadOneRegister(instruction,
6787 temp_loc,
6788 super_offset,
6789 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006790 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006791
6792 // If the class reference currently in `temp` is not null, jump
6793 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006794 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006795 __ j(kNotZero, &loop);
6796 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006797 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006798 break;
6799 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006800
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006801 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006802 // /* HeapReference<Class> */ temp = obj->klass_
6803 GenerateReferenceLoadTwoRegisters(instruction,
6804 temp_loc,
6805 obj_loc,
6806 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006807 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006808
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006809 // Do an exact check.
6810 if (cls.IsRegister()) {
6811 __ cmpl(temp, cls.AsRegister<Register>());
6812 } else {
6813 DCHECK(cls.IsStackSlot()) << cls;
6814 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6815 }
6816 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006817
6818 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006819 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006820 GenerateReferenceLoadOneRegister(instruction,
6821 temp_loc,
6822 component_offset,
6823 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006824 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006825
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006826 // If the component type is null (i.e. the object not an array), jump to the slow path to
6827 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006828 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006829 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006830
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006831 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006832 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006833 break;
6834 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006835
Calin Juravle98893e12015-10-02 21:05:03 +01006836 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006837 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006838 // We cannot directly call the CheckCast runtime entry point
6839 // without resorting to a type checking slow path here (i.e. by
6840 // calling InvokeRuntime directly), as it would require to
6841 // assign fixed registers for the inputs of this HInstanceOf
6842 // instruction (following the runtime calling convention), which
6843 // might be cluttered by the potential first read barrier
6844 // emission at the beginning of this method.
6845 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006846 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006847
6848 case TypeCheckKind::kInterfaceCheck: {
6849 // Fast path for the interface check. Since we compare with a memory location in the inner
6850 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6851 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006852 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006853 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006854 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6855 // doing this.
6856 // /* HeapReference<Class> */ temp = obj->klass_
6857 GenerateReferenceLoadTwoRegisters(instruction,
6858 temp_loc,
6859 obj_loc,
6860 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006861 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006862
6863 // /* HeapReference<Class> */ temp = temp->iftable_
6864 GenerateReferenceLoadTwoRegisters(instruction,
6865 temp_loc,
6866 temp_loc,
6867 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006868 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006869 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006870 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006871 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006872 NearLabel start_loop;
6873 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006874 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006875 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006876 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6877 // Go to next interface if the classes do not match.
6878 __ cmpl(cls.AsRegister<Register>(),
6879 CodeGeneratorX86::ArrayAddress(temp,
6880 maybe_temp2_loc,
6881 TIMES_4,
6882 object_array_data_offset));
6883 __ j(kNotEqual, &start_loop);
6884 } else {
6885 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006886 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006887 break;
6888 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006889 }
6890 __ Bind(&done);
6891
Roland Levillain0d5a2812015-11-13 10:07:31 +00006892 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006893}
6894
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006895void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6896 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006897 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006898 InvokeRuntimeCallingConvention calling_convention;
6899 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6900}
6901
6902void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006903 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6904 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006905 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006906 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006907 if (instruction->IsEnter()) {
6908 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6909 } else {
6910 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6911 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006912}
6913
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006914void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6915void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6916void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6917
6918void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6919 LocationSummary* locations =
6920 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6921 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6922 || instruction->GetResultType() == Primitive::kPrimLong);
6923 locations->SetInAt(0, Location::RequiresRegister());
6924 locations->SetInAt(1, Location::Any());
6925 locations->SetOut(Location::SameAsFirstInput());
6926}
6927
6928void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6929 HandleBitwiseOperation(instruction);
6930}
6931
6932void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6933 HandleBitwiseOperation(instruction);
6934}
6935
6936void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6937 HandleBitwiseOperation(instruction);
6938}
6939
6940void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6941 LocationSummary* locations = instruction->GetLocations();
6942 Location first = locations->InAt(0);
6943 Location second = locations->InAt(1);
6944 DCHECK(first.Equals(locations->Out()));
6945
6946 if (instruction->GetResultType() == Primitive::kPrimInt) {
6947 if (second.IsRegister()) {
6948 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006949 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006950 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006951 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006952 } else {
6953 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006954 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006955 }
6956 } else if (second.IsConstant()) {
6957 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006958 __ andl(first.AsRegister<Register>(),
6959 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006960 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006961 __ orl(first.AsRegister<Register>(),
6962 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006963 } else {
6964 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006965 __ xorl(first.AsRegister<Register>(),
6966 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006967 }
6968 } else {
6969 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006970 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006971 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006972 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006973 } else {
6974 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006975 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006976 }
6977 }
6978 } else {
6979 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6980 if (second.IsRegisterPair()) {
6981 if (instruction->IsAnd()) {
6982 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6983 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6984 } else if (instruction->IsOr()) {
6985 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6986 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6987 } else {
6988 DCHECK(instruction->IsXor());
6989 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6990 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6991 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006992 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006993 if (instruction->IsAnd()) {
6994 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6995 __ andl(first.AsRegisterPairHigh<Register>(),
6996 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6997 } else if (instruction->IsOr()) {
6998 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6999 __ orl(first.AsRegisterPairHigh<Register>(),
7000 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7001 } else {
7002 DCHECK(instruction->IsXor());
7003 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7004 __ xorl(first.AsRegisterPairHigh<Register>(),
7005 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7006 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007007 } else {
7008 DCHECK(second.IsConstant()) << second;
7009 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007010 int32_t low_value = Low32Bits(value);
7011 int32_t high_value = High32Bits(value);
7012 Immediate low(low_value);
7013 Immediate high(high_value);
7014 Register first_low = first.AsRegisterPairLow<Register>();
7015 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007016 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007017 if (low_value == 0) {
7018 __ xorl(first_low, first_low);
7019 } else if (low_value != -1) {
7020 __ andl(first_low, low);
7021 }
7022 if (high_value == 0) {
7023 __ xorl(first_high, first_high);
7024 } else if (high_value != -1) {
7025 __ andl(first_high, high);
7026 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007027 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007028 if (low_value != 0) {
7029 __ orl(first_low, low);
7030 }
7031 if (high_value != 0) {
7032 __ orl(first_high, high);
7033 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007034 } else {
7035 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007036 if (low_value != 0) {
7037 __ xorl(first_low, low);
7038 }
7039 if (high_value != 0) {
7040 __ xorl(first_high, high);
7041 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007042 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007043 }
7044 }
7045}
7046
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007047void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7048 HInstruction* instruction,
7049 Location out,
7050 uint32_t offset,
7051 Location maybe_temp,
7052 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007053 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007054 if (read_barrier_option == kWithReadBarrier) {
7055 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007056 if (kUseBakerReadBarrier) {
7057 // Load with fast path based Baker's read barrier.
7058 // /* HeapReference<Object> */ out = *(out + offset)
7059 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007060 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007061 } else {
7062 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007063 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007064 // in the following move operation, as we will need it for the
7065 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007066 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007067 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007068 // /* HeapReference<Object> */ out = *(out + offset)
7069 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007070 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007071 }
7072 } else {
7073 // Plain load with no read barrier.
7074 // /* HeapReference<Object> */ out = *(out + offset)
7075 __ movl(out_reg, Address(out_reg, offset));
7076 __ MaybeUnpoisonHeapReference(out_reg);
7077 }
7078}
7079
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007080void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7081 HInstruction* instruction,
7082 Location out,
7083 Location obj,
7084 uint32_t offset,
7085 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007086 Register out_reg = out.AsRegister<Register>();
7087 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007088 if (read_barrier_option == kWithReadBarrier) {
7089 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007090 if (kUseBakerReadBarrier) {
7091 // Load with fast path based Baker's read barrier.
7092 // /* HeapReference<Object> */ out = *(obj + offset)
7093 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007094 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007095 } else {
7096 // Load with slow path based read barrier.
7097 // /* HeapReference<Object> */ out = *(obj + offset)
7098 __ movl(out_reg, Address(obj_reg, offset));
7099 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7100 }
7101 } else {
7102 // Plain load with no read barrier.
7103 // /* HeapReference<Object> */ out = *(obj + offset)
7104 __ movl(out_reg, Address(obj_reg, offset));
7105 __ MaybeUnpoisonHeapReference(out_reg);
7106 }
7107}
7108
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007109void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7110 HInstruction* instruction,
7111 Location root,
7112 const Address& address,
7113 Label* fixup_label,
7114 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007115 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007116 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007117 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007118 if (kUseBakerReadBarrier) {
7119 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7120 // Baker's read barrier are used:
7121 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007122 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00007123 // if (Thread::Current()->GetIsGcMarking()) {
7124 // root = ReadBarrier::Mark(root)
7125 // }
7126
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007127 // /* GcRoot<mirror::Object> */ root = *address
7128 __ movl(root_reg, address);
7129 if (fixup_label != nullptr) {
7130 __ Bind(fixup_label);
7131 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007132 static_assert(
7133 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7134 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7135 "have different sizes.");
7136 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7137 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7138 "have different sizes.");
7139
Vladimir Marko953437b2016-08-24 08:30:46 +00007140 // Slow path marking the GC root `root`.
7141 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007142 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007143 codegen_->AddSlowPath(slow_path);
7144
Andreas Gampe542451c2016-07-26 09:02:02 -07007145 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00007146 Immediate(0));
7147 __ j(kNotEqual, slow_path->GetEntryLabel());
7148 __ Bind(slow_path->GetExitLabel());
7149 } else {
7150 // GC root loaded through a slow path for read barriers other
7151 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007152 // /* GcRoot<mirror::Object>* */ root = address
7153 __ leal(root_reg, address);
7154 if (fixup_label != nullptr) {
7155 __ Bind(fixup_label);
7156 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007157 // /* mirror::Object* */ root = root->Read()
7158 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7159 }
7160 } else {
7161 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007162 // /* GcRoot<mirror::Object> */ root = *address
7163 __ movl(root_reg, address);
7164 if (fixup_label != nullptr) {
7165 __ Bind(fixup_label);
7166 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007167 // Note that GC roots are not affected by heap poisoning, thus we
7168 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007169 }
7170}
7171
7172void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7173 Location ref,
7174 Register obj,
7175 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007176 bool needs_null_check) {
7177 DCHECK(kEmitCompilerReadBarrier);
7178 DCHECK(kUseBakerReadBarrier);
7179
7180 // /* HeapReference<Object> */ ref = *(obj + offset)
7181 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007182 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007183}
7184
7185void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7186 Location ref,
7187 Register obj,
7188 uint32_t data_offset,
7189 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007190 bool needs_null_check) {
7191 DCHECK(kEmitCompilerReadBarrier);
7192 DCHECK(kUseBakerReadBarrier);
7193
Roland Levillain3d312422016-06-23 13:53:42 +01007194 static_assert(
7195 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7196 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007197 // /* HeapReference<Object> */ ref =
7198 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007199 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007200 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007201}
7202
7203void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7204 Location ref,
7205 Register obj,
7206 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007207 bool needs_null_check,
7208 bool always_update_field,
7209 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007210 DCHECK(kEmitCompilerReadBarrier);
7211 DCHECK(kUseBakerReadBarrier);
7212
7213 // In slow path based read barriers, the read barrier call is
7214 // inserted after the original load. However, in fast path based
7215 // Baker's read barriers, we need to perform the load of
7216 // mirror::Object::monitor_ *before* the original reference load.
7217 // This load-load ordering is required by the read barrier.
7218 // The fast path/slow path (for Baker's algorithm) should look like:
7219 //
7220 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7221 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7222 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007223 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007224 // if (is_gray) {
7225 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7226 // }
7227 //
7228 // Note: the original implementation in ReadBarrier::Barrier is
7229 // slightly more complex as:
7230 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007231 // the high-bits of rb_state, which are expected to be all zeroes
7232 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7233 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007234 // - it performs additional checks that we do not do here for
7235 // performance reasons.
7236
7237 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007238 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7239
Vladimir Marko953437b2016-08-24 08:30:46 +00007240 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007241 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7242 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007243 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7244 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7245 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7246
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007247 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007248 // ref = ReadBarrier::Mark(ref);
7249 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7250 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007251 if (needs_null_check) {
7252 MaybeRecordImplicitNullCheck(instruction);
7253 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007254
7255 // Load fence to prevent load-load reordering.
7256 // Note that this is a no-op, thanks to the x86 memory model.
7257 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7258
7259 // The actual reference load.
7260 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007261 __ movl(ref_reg, src); // Flags are unaffected.
7262
7263 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7264 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007265 SlowPathCode* slow_path;
7266 if (always_update_field) {
7267 DCHECK(temp != nullptr);
7268 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7269 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7270 } else {
7271 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7272 instruction, ref, /* unpoison_ref_before_marking */ true);
7273 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007274 AddSlowPath(slow_path);
7275
7276 // We have done the "if" of the gray bit check above, now branch based on the flags.
7277 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007278
7279 // Object* ref = ref_addr->AsMirrorPtr()
7280 __ MaybeUnpoisonHeapReference(ref_reg);
7281
Roland Levillain7c1559a2015-12-15 10:55:36 +00007282 __ Bind(slow_path->GetExitLabel());
7283}
7284
7285void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7286 Location out,
7287 Location ref,
7288 Location obj,
7289 uint32_t offset,
7290 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007291 DCHECK(kEmitCompilerReadBarrier);
7292
Roland Levillain7c1559a2015-12-15 10:55:36 +00007293 // Insert a slow path based read barrier *after* the reference load.
7294 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007295 // If heap poisoning is enabled, the unpoisoning of the loaded
7296 // reference will be carried out by the runtime within the slow
7297 // path.
7298 //
7299 // Note that `ref` currently does not get unpoisoned (when heap
7300 // poisoning is enabled), which is alright as the `ref` argument is
7301 // not used by the artReadBarrierSlow entry point.
7302 //
7303 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7304 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7305 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7306 AddSlowPath(slow_path);
7307
Roland Levillain0d5a2812015-11-13 10:07:31 +00007308 __ jmp(slow_path->GetEntryLabel());
7309 __ Bind(slow_path->GetExitLabel());
7310}
7311
Roland Levillain7c1559a2015-12-15 10:55:36 +00007312void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7313 Location out,
7314 Location ref,
7315 Location obj,
7316 uint32_t offset,
7317 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007318 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007319 // Baker's read barriers shall be handled by the fast path
7320 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7321 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007322 // If heap poisoning is enabled, unpoisoning will be taken care of
7323 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007324 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007325 } else if (kPoisonHeapReferences) {
7326 __ UnpoisonHeapReference(out.AsRegister<Register>());
7327 }
7328}
7329
Roland Levillain7c1559a2015-12-15 10:55:36 +00007330void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7331 Location out,
7332 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007333 DCHECK(kEmitCompilerReadBarrier);
7334
Roland Levillain7c1559a2015-12-15 10:55:36 +00007335 // Insert a slow path based read barrier *after* the GC root load.
7336 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007337 // Note that GC roots are not affected by heap poisoning, so we do
7338 // not need to do anything special for this here.
7339 SlowPathCode* slow_path =
7340 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7341 AddSlowPath(slow_path);
7342
Roland Levillain0d5a2812015-11-13 10:07:31 +00007343 __ jmp(slow_path->GetEntryLabel());
7344 __ Bind(slow_path->GetExitLabel());
7345}
7346
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007347void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007348 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007349 LOG(FATAL) << "Unreachable";
7350}
7351
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007352void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007353 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007354 LOG(FATAL) << "Unreachable";
7355}
7356
Mark Mendellfe57faa2015-09-18 09:26:15 -04007357// Simple implementation of packed switch - generate cascaded compare/jumps.
7358void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7359 LocationSummary* locations =
7360 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7361 locations->SetInAt(0, Location::RequiresRegister());
7362}
7363
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007364void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7365 int32_t lower_bound,
7366 uint32_t num_entries,
7367 HBasicBlock* switch_block,
7368 HBasicBlock* default_block) {
7369 // Figure out the correct compare values and jump conditions.
7370 // Handle the first compare/branch as a special case because it might
7371 // jump to the default case.
7372 DCHECK_GT(num_entries, 2u);
7373 Condition first_condition;
7374 uint32_t index;
7375 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7376 if (lower_bound != 0) {
7377 first_condition = kLess;
7378 __ cmpl(value_reg, Immediate(lower_bound));
7379 __ j(first_condition, codegen_->GetLabelOf(default_block));
7380 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007381
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007382 index = 1;
7383 } else {
7384 // Handle all the compare/jumps below.
7385 first_condition = kBelow;
7386 index = 0;
7387 }
7388
7389 // Handle the rest of the compare/jumps.
7390 for (; index + 1 < num_entries; index += 2) {
7391 int32_t compare_to_value = lower_bound + index + 1;
7392 __ cmpl(value_reg, Immediate(compare_to_value));
7393 // Jump to successors[index] if value < case_value[index].
7394 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7395 // Jump to successors[index + 1] if value == case_value[index + 1].
7396 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7397 }
7398
7399 if (index != num_entries) {
7400 // There are an odd number of entries. Handle the last one.
7401 DCHECK_EQ(index + 1, num_entries);
7402 __ cmpl(value_reg, Immediate(lower_bound + index));
7403 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007404 }
7405
7406 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007407 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7408 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007409 }
7410}
7411
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007412void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7413 int32_t lower_bound = switch_instr->GetStartValue();
7414 uint32_t num_entries = switch_instr->GetNumEntries();
7415 LocationSummary* locations = switch_instr->GetLocations();
7416 Register value_reg = locations->InAt(0).AsRegister<Register>();
7417
7418 GenPackedSwitchWithCompares(value_reg,
7419 lower_bound,
7420 num_entries,
7421 switch_instr->GetBlock(),
7422 switch_instr->GetDefaultBlock());
7423}
7424
Mark Mendell805b3b52015-09-18 14:10:29 -04007425void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7426 LocationSummary* locations =
7427 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7428 locations->SetInAt(0, Location::RequiresRegister());
7429
7430 // Constant area pointer.
7431 locations->SetInAt(1, Location::RequiresRegister());
7432
7433 // And the temporary we need.
7434 locations->AddTemp(Location::RequiresRegister());
7435}
7436
7437void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7438 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007439 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007440 LocationSummary* locations = switch_instr->GetLocations();
7441 Register value_reg = locations->InAt(0).AsRegister<Register>();
7442 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7443
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007444 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7445 GenPackedSwitchWithCompares(value_reg,
7446 lower_bound,
7447 num_entries,
7448 switch_instr->GetBlock(),
7449 default_block);
7450 return;
7451 }
7452
Mark Mendell805b3b52015-09-18 14:10:29 -04007453 // Optimizing has a jump area.
7454 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7455 Register constant_area = locations->InAt(1).AsRegister<Register>();
7456
7457 // Remove the bias, if needed.
7458 if (lower_bound != 0) {
7459 __ leal(temp_reg, Address(value_reg, -lower_bound));
7460 value_reg = temp_reg;
7461 }
7462
7463 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007464 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007465 __ cmpl(value_reg, Immediate(num_entries - 1));
7466 __ j(kAbove, codegen_->GetLabelOf(default_block));
7467
7468 // We are in the range of the table.
7469 // Load (target-constant_area) from the jump table, indexing by the value.
7470 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7471
7472 // Compute the actual target address by adding in constant_area.
7473 __ addl(temp_reg, constant_area);
7474
7475 // And jump.
7476 __ jmp(temp_reg);
7477}
7478
Mark Mendell0616ae02015-04-17 12:49:27 -04007479void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7480 HX86ComputeBaseMethodAddress* insn) {
7481 LocationSummary* locations =
7482 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7483 locations->SetOut(Location::RequiresRegister());
7484}
7485
7486void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7487 HX86ComputeBaseMethodAddress* insn) {
7488 LocationSummary* locations = insn->GetLocations();
7489 Register reg = locations->Out().AsRegister<Register>();
7490
7491 // Generate call to next instruction.
7492 Label next_instruction;
7493 __ call(&next_instruction);
7494 __ Bind(&next_instruction);
7495
7496 // Remember this offset for later use with constant area.
7497 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7498
7499 // Grab the return address off the stack.
7500 __ popl(reg);
7501}
7502
7503void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7504 HX86LoadFromConstantTable* insn) {
7505 LocationSummary* locations =
7506 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7507
7508 locations->SetInAt(0, Location::RequiresRegister());
7509 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7510
7511 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007512 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007513 return;
7514 }
7515
7516 switch (insn->GetType()) {
7517 case Primitive::kPrimFloat:
7518 case Primitive::kPrimDouble:
7519 locations->SetOut(Location::RequiresFpuRegister());
7520 break;
7521
7522 case Primitive::kPrimInt:
7523 locations->SetOut(Location::RequiresRegister());
7524 break;
7525
7526 default:
7527 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7528 }
7529}
7530
7531void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007532 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007533 return;
7534 }
7535
7536 LocationSummary* locations = insn->GetLocations();
7537 Location out = locations->Out();
7538 Register const_area = locations->InAt(0).AsRegister<Register>();
7539 HConstant *value = insn->GetConstant();
7540
7541 switch (insn->GetType()) {
7542 case Primitive::kPrimFloat:
7543 __ movss(out.AsFpuRegister<XmmRegister>(),
7544 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7545 break;
7546
7547 case Primitive::kPrimDouble:
7548 __ movsd(out.AsFpuRegister<XmmRegister>(),
7549 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7550 break;
7551
7552 case Primitive::kPrimInt:
7553 __ movl(out.AsRegister<Register>(),
7554 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7555 break;
7556
7557 default:
7558 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7559 }
7560}
7561
Mark Mendell0616ae02015-04-17 12:49:27 -04007562/**
7563 * Class to handle late fixup of offsets into constant area.
7564 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007565class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007566 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007567 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7568 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7569
7570 protected:
7571 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7572
7573 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007574
7575 private:
7576 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7577 // Patch the correct offset for the instruction. The place to patch is the
7578 // last 4 bytes of the instruction.
7579 // The value to patch is the distance from the offset in the constant area
7580 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007581 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Mathieu Chartier6beced42016-11-15 15:51:31 -08007582 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();
Mark Mendell0616ae02015-04-17 12:49:27 -04007583
7584 // Patch in the right value.
7585 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7586 }
7587
Mark Mendell0616ae02015-04-17 12:49:27 -04007588 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007589 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007590};
7591
Mark Mendell805b3b52015-09-18 14:10:29 -04007592/**
7593 * Class to handle late fixup of offsets to a jump table that will be created in the
7594 * constant area.
7595 */
7596class JumpTableRIPFixup : public RIPFixup {
7597 public:
7598 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7599 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7600
7601 void CreateJumpTable() {
7602 X86Assembler* assembler = codegen_->GetAssembler();
7603
7604 // Ensure that the reference to the jump table has the correct offset.
7605 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7606 SetOffset(offset_in_constant_table);
7607
7608 // The label values in the jump table are computed relative to the
7609 // instruction addressing the constant area.
7610 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7611
7612 // Populate the jump table with the correct values for the jump table.
7613 int32_t num_entries = switch_instr_->GetNumEntries();
7614 HBasicBlock* block = switch_instr_->GetBlock();
7615 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7616 // The value that we want is the target offset - the position of the table.
7617 for (int32_t i = 0; i < num_entries; i++) {
7618 HBasicBlock* b = successors[i];
7619 Label* l = codegen_->GetLabelOf(b);
7620 DCHECK(l->IsBound());
7621 int32_t offset_to_block = l->Position() - relative_offset;
7622 assembler->AppendInt32(offset_to_block);
7623 }
7624 }
7625
7626 private:
7627 const HX86PackedSwitch* switch_instr_;
7628};
7629
7630void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7631 // Generate the constant area if needed.
7632 X86Assembler* assembler = GetAssembler();
7633 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7634 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7635 // byte values.
7636 assembler->Align(4, 0);
7637 constant_area_start_ = assembler->CodeSize();
7638
7639 // Populate any jump tables.
7640 for (auto jump_table : fixups_to_jump_tables_) {
7641 jump_table->CreateJumpTable();
7642 }
7643
7644 // And now add the constant area to the generated code.
7645 assembler->AddConstantArea();
7646 }
7647
7648 // And finish up.
7649 CodeGenerator::Finalize(allocator);
7650}
7651
Mark Mendell0616ae02015-04-17 12:49:27 -04007652Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7653 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7654 return Address(reg, kDummy32BitOffset, fixup);
7655}
7656
7657Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7658 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7659 return Address(reg, kDummy32BitOffset, fixup);
7660}
7661
7662Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7663 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7664 return Address(reg, kDummy32BitOffset, fixup);
7665}
7666
7667Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7668 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7669 return Address(reg, kDummy32BitOffset, fixup);
7670}
7671
Aart Bika19616e2016-02-01 18:57:58 -08007672void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7673 if (value == 0) {
7674 __ xorl(dest, dest);
7675 } else {
7676 __ movl(dest, Immediate(value));
7677 }
7678}
7679
7680void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7681 if (value == 0) {
7682 __ testl(dest, dest);
7683 } else {
7684 __ cmpl(dest, Immediate(value));
7685 }
7686}
7687
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007688void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7689 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007690 GenerateIntCompare(lhs_reg, rhs);
7691}
7692
7693void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007694 if (rhs.IsConstant()) {
7695 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007696 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007697 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007698 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007699 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007700 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007701 }
7702}
7703
7704Address CodeGeneratorX86::ArrayAddress(Register obj,
7705 Location index,
7706 ScaleFactor scale,
7707 uint32_t data_offset) {
7708 return index.IsConstant() ?
7709 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7710 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7711}
7712
Mark Mendell805b3b52015-09-18 14:10:29 -04007713Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7714 Register reg,
7715 Register value) {
7716 // Create a fixup to be used to create and address the jump table.
7717 JumpTableRIPFixup* table_fixup =
7718 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7719
7720 // We have to populate the jump tables.
7721 fixups_to_jump_tables_.push_back(table_fixup);
7722
7723 // We want a scaled address, as we are extracting the correct offset from the table.
7724 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7725}
7726
Andreas Gampe85b62f22015-09-09 13:15:38 -07007727// TODO: target as memory.
7728void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7729 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007730 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007731 return;
7732 }
7733
7734 DCHECK_NE(type, Primitive::kPrimVoid);
7735
7736 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7737 if (target.Equals(return_loc)) {
7738 return;
7739 }
7740
7741 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7742 // with the else branch.
7743 if (type == Primitive::kPrimLong) {
7744 HParallelMove parallel_move(GetGraph()->GetArena());
7745 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7746 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7747 GetMoveResolver()->EmitNativeCode(&parallel_move);
7748 } else {
7749 // Let the parallel move resolver take care of all of this.
7750 HParallelMove parallel_move(GetGraph()->GetArena());
7751 parallel_move.AddMove(return_loc, target, type, nullptr);
7752 GetMoveResolver()->EmitNativeCode(&parallel_move);
7753 }
7754}
7755
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007756void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7757 for (const PatchInfo<Label>& info : jit_string_patches_) {
7758 const auto& it = jit_string_roots_.find(StringReference(&info.dex_file, info.index));
7759 DCHECK(it != jit_string_roots_.end());
7760 size_t index_in_table = it->second;
7761 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7762 uintptr_t address =
7763 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7764 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7765 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7766 dchecked_integral_cast<uint32_t>(address);
7767 }
7768}
7769
Roland Levillain4d027112015-07-01 15:41:14 +01007770#undef __
7771
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007772} // namespace x86
7773} // namespace art