blob: 2451b8d24707c88c18d67627ee36307edb6fa264 [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;
268 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
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)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001015 constant_area_start_(-1),
1016 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1017 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001018 // Use a fake return address register to mimic Quick.
1019 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001020}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001021
David Brazdil58282f42016-01-14 12:45:10 +00001022void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001023 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001024 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001025}
1026
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001027InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001028 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001029 assembler_(codegen->GetAssembler()),
1030 codegen_(codegen) {}
1031
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001032static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001033 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001034}
1035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001036void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001037 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001038 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001039 bool skip_overflow_check =
1040 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001041 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001042
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001043 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001044 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001045 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001046 }
1047
Mark Mendell5f874182015-03-04 15:42:45 -05001048 if (HasEmptyFrame()) {
1049 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001050 }
Mark Mendell5f874182015-03-04 15:42:45 -05001051
1052 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1053 Register reg = kCoreCalleeSaves[i];
1054 if (allocated_registers_.ContainsCoreRegister(reg)) {
1055 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001056 __ cfi().AdjustCFAOffset(kX86WordSize);
1057 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001058 }
1059 }
1060
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001061 int adjust = GetFrameSize() - FrameEntrySpillSize();
1062 __ subl(ESP, Immediate(adjust));
1063 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001064 // Save the current method if we need it. Note that we do not
1065 // do this in HCurrentMethod, as the instruction might have been removed
1066 // in the SSA graph.
1067 if (RequiresCurrentMethod()) {
1068 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1069 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001070}
1071
1072void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001073 __ cfi().RememberState();
1074 if (!HasEmptyFrame()) {
1075 int adjust = GetFrameSize() - FrameEntrySpillSize();
1076 __ addl(ESP, Immediate(adjust));
1077 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001078
David Srbeckyc34dc932015-04-12 09:27:43 +01001079 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1080 Register reg = kCoreCalleeSaves[i];
1081 if (allocated_registers_.ContainsCoreRegister(reg)) {
1082 __ popl(reg);
1083 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1084 __ cfi().Restore(DWARFReg(reg));
1085 }
Mark Mendell5f874182015-03-04 15:42:45 -05001086 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001087 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001088 __ ret();
1089 __ cfi().RestoreState();
1090 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001091}
1092
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001093void CodeGeneratorX86::Bind(HBasicBlock* block) {
1094 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001095}
1096
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001097Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1098 switch (type) {
1099 case Primitive::kPrimBoolean:
1100 case Primitive::kPrimByte:
1101 case Primitive::kPrimChar:
1102 case Primitive::kPrimShort:
1103 case Primitive::kPrimInt:
1104 case Primitive::kPrimNot:
1105 return Location::RegisterLocation(EAX);
1106
1107 case Primitive::kPrimLong:
1108 return Location::RegisterPairLocation(EAX, EDX);
1109
1110 case Primitive::kPrimVoid:
1111 return Location::NoLocation();
1112
1113 case Primitive::kPrimDouble:
1114 case Primitive::kPrimFloat:
1115 return Location::FpuRegisterLocation(XMM0);
1116 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001117
1118 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001119}
1120
1121Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1122 return Location::RegisterLocation(kMethodRegisterArgument);
1123}
1124
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001125Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001126 switch (type) {
1127 case Primitive::kPrimBoolean:
1128 case Primitive::kPrimByte:
1129 case Primitive::kPrimChar:
1130 case Primitive::kPrimShort:
1131 case Primitive::kPrimInt:
1132 case Primitive::kPrimNot: {
1133 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001134 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001135 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001136 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001137 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001138 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001139 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001140 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001141
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001142 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001143 uint32_t index = gp_index_;
1144 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001145 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001146 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001147 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1148 calling_convention.GetRegisterPairAt(index));
1149 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001150 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001151 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1152 }
1153 }
1154
1155 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001156 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001157 stack_index_++;
1158 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1159 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1160 } else {
1161 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1162 }
1163 }
1164
1165 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001166 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001167 stack_index_ += 2;
1168 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1169 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1170 } else {
1171 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001172 }
1173 }
1174
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001175 case Primitive::kPrimVoid:
1176 LOG(FATAL) << "Unexpected parameter type " << type;
1177 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001178 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001179 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001180}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001181
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001182void CodeGeneratorX86::Move32(Location destination, Location source) {
1183 if (source.Equals(destination)) {
1184 return;
1185 }
1186 if (destination.IsRegister()) {
1187 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001188 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001189 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001190 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001191 } else {
1192 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001193 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001194 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001195 } else if (destination.IsFpuRegister()) {
1196 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001197 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001198 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001199 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001200 } else {
1201 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001202 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001203 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001204 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001205 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001206 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001207 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001208 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001209 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001210 } else if (source.IsConstant()) {
1211 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001212 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001213 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001214 } else {
1215 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001216 __ pushl(Address(ESP, source.GetStackIndex()));
1217 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001218 }
1219 }
1220}
1221
1222void CodeGeneratorX86::Move64(Location destination, Location source) {
1223 if (source.Equals(destination)) {
1224 return;
1225 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001226 if (destination.IsRegisterPair()) {
1227 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001228 EmitParallelMoves(
1229 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1230 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001231 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001232 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001233 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1234 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001235 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001236 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1237 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1238 __ psrlq(src_reg, Immediate(32));
1239 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001240 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001241 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001242 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001243 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1244 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001245 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1246 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001247 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001248 if (source.IsFpuRegister()) {
1249 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1250 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001251 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001252 } else if (source.IsRegisterPair()) {
1253 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1254 // Create stack space for 2 elements.
1255 __ subl(ESP, Immediate(2 * elem_size));
1256 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1257 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1258 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1259 // And remove the temporary stack space we allocated.
1260 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001261 } else {
1262 LOG(FATAL) << "Unimplemented";
1263 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001264 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001265 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001266 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001267 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001268 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001269 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001270 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001271 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001272 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001273 } else if (source.IsConstant()) {
1274 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001275 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1276 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001277 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001278 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1279 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001280 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001281 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001282 EmitParallelMoves(
1283 Location::StackSlot(source.GetStackIndex()),
1284 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001285 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001286 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001287 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1288 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001289 }
1290 }
1291}
1292
Calin Juravle175dc732015-08-25 15:42:32 +01001293void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1294 DCHECK(location.IsRegister());
1295 __ movl(location.AsRegister<Register>(), Immediate(value));
1296}
1297
Calin Juravlee460d1d2015-09-29 04:52:17 +01001298void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001299 HParallelMove move(GetGraph()->GetArena());
1300 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1301 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1302 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001303 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001304 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001305 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001306 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001307}
1308
1309void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1310 if (location.IsRegister()) {
1311 locations->AddTemp(location);
1312 } else if (location.IsRegisterPair()) {
1313 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1314 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1315 } else {
1316 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1317 }
1318}
1319
David Brazdilfc6a86a2015-06-26 10:33:45 +00001320void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001321 DCHECK(!successor->IsExitBlock());
1322
1323 HBasicBlock* block = got->GetBlock();
1324 HInstruction* previous = got->GetPrevious();
1325
1326 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001327 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001328 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1329 return;
1330 }
1331
1332 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1333 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1334 }
1335 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001336 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001337 }
1338}
1339
David Brazdilfc6a86a2015-06-26 10:33:45 +00001340void LocationsBuilderX86::VisitGoto(HGoto* got) {
1341 got->SetLocations(nullptr);
1342}
1343
1344void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1345 HandleGoto(got, got->GetSuccessor());
1346}
1347
1348void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1349 try_boundary->SetLocations(nullptr);
1350}
1351
1352void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1353 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1354 if (!successor->IsExitBlock()) {
1355 HandleGoto(try_boundary, successor);
1356 }
1357}
1358
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001359void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001360 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001361}
1362
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001363void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001364}
1365
Mark Mendell152408f2015-12-31 12:28:50 -05001366template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001367void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001368 LabelType* true_label,
1369 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001370 if (cond->IsFPConditionTrueIfNaN()) {
1371 __ j(kUnordered, true_label);
1372 } else if (cond->IsFPConditionFalseIfNaN()) {
1373 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001374 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001375 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001376}
1377
Mark Mendell152408f2015-12-31 12:28:50 -05001378template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001379void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001380 LabelType* true_label,
1381 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001382 LocationSummary* locations = cond->GetLocations();
1383 Location left = locations->InAt(0);
1384 Location right = locations->InAt(1);
1385 IfCondition if_cond = cond->GetCondition();
1386
Mark Mendellc4701932015-04-10 13:18:51 -04001387 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001388 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001389 IfCondition true_high_cond = if_cond;
1390 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001391 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001392
1393 // Set the conditions for the test, remembering that == needs to be
1394 // decided using the low words.
1395 switch (if_cond) {
1396 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001397 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001398 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001399 break;
1400 case kCondLT:
1401 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001402 break;
1403 case kCondLE:
1404 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001405 break;
1406 case kCondGT:
1407 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001408 break;
1409 case kCondGE:
1410 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001411 break;
Aart Bike9f37602015-10-09 11:15:55 -07001412 case kCondB:
1413 false_high_cond = kCondA;
1414 break;
1415 case kCondBE:
1416 true_high_cond = kCondB;
1417 break;
1418 case kCondA:
1419 false_high_cond = kCondB;
1420 break;
1421 case kCondAE:
1422 true_high_cond = kCondA;
1423 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001424 }
1425
1426 if (right.IsConstant()) {
1427 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001428 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001429 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001430
Aart Bika19616e2016-02-01 18:57:58 -08001431 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001432 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001433 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001434 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001435 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001436 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001437 __ j(X86Condition(true_high_cond), true_label);
1438 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001439 }
1440 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001441 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001442 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001443 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001444 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001445
1446 __ cmpl(left_high, right_high);
1447 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001448 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001449 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001450 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001451 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001452 __ j(X86Condition(true_high_cond), true_label);
1453 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001454 }
1455 // Must be equal high, so compare the lows.
1456 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001457 } else {
1458 DCHECK(right.IsDoubleStackSlot());
1459 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1460 if (if_cond == kCondNE) {
1461 __ j(X86Condition(true_high_cond), true_label);
1462 } else if (if_cond == kCondEQ) {
1463 __ j(X86Condition(false_high_cond), false_label);
1464 } else {
1465 __ j(X86Condition(true_high_cond), true_label);
1466 __ j(X86Condition(false_high_cond), false_label);
1467 }
1468 // Must be equal high, so compare the lows.
1469 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001470 }
1471 // The last comparison might be unsigned.
1472 __ j(final_condition, true_label);
1473}
1474
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001475void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1476 Location rhs,
1477 HInstruction* insn,
1478 bool is_double) {
1479 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1480 if (is_double) {
1481 if (rhs.IsFpuRegister()) {
1482 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1483 } else if (const_area != nullptr) {
1484 DCHECK(const_area->IsEmittedAtUseSite());
1485 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1486 codegen_->LiteralDoubleAddress(
1487 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1488 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1489 } else {
1490 DCHECK(rhs.IsDoubleStackSlot());
1491 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1492 }
1493 } else {
1494 if (rhs.IsFpuRegister()) {
1495 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1496 } else if (const_area != nullptr) {
1497 DCHECK(const_area->IsEmittedAtUseSite());
1498 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1499 codegen_->LiteralFloatAddress(
1500 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1501 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1502 } else {
1503 DCHECK(rhs.IsStackSlot());
1504 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1505 }
1506 }
1507}
1508
Mark Mendell152408f2015-12-31 12:28:50 -05001509template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001510void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001511 LabelType* true_target_in,
1512 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001513 // Generated branching requires both targets to be explicit. If either of the
1514 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001515 LabelType fallthrough_target;
1516 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1517 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001518
Mark Mendellc4701932015-04-10 13:18:51 -04001519 LocationSummary* locations = condition->GetLocations();
1520 Location left = locations->InAt(0);
1521 Location right = locations->InAt(1);
1522
Mark Mendellc4701932015-04-10 13:18:51 -04001523 Primitive::Type type = condition->InputAt(0)->GetType();
1524 switch (type) {
1525 case Primitive::kPrimLong:
1526 GenerateLongComparesAndJumps(condition, true_target, false_target);
1527 break;
1528 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001529 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001530 GenerateFPJumps(condition, true_target, false_target);
1531 break;
1532 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001533 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001534 GenerateFPJumps(condition, true_target, false_target);
1535 break;
1536 default:
1537 LOG(FATAL) << "Unexpected compare type " << type;
1538 }
1539
David Brazdil0debae72015-11-12 18:37:00 +00001540 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001541 __ jmp(false_target);
1542 }
David Brazdil0debae72015-11-12 18:37:00 +00001543
1544 if (fallthrough_target.IsLinked()) {
1545 __ Bind(&fallthrough_target);
1546 }
Mark Mendellc4701932015-04-10 13:18:51 -04001547}
1548
David Brazdil0debae72015-11-12 18:37:00 +00001549static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1550 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1551 // are set only strictly before `branch`. We can't use the eflags on long/FP
1552 // conditions if they are materialized due to the complex branching.
1553 return cond->IsCondition() &&
1554 cond->GetNext() == branch &&
1555 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1556 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1557}
1558
Mark Mendell152408f2015-12-31 12:28:50 -05001559template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001560void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001561 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001562 LabelType* true_target,
1563 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001564 HInstruction* cond = instruction->InputAt(condition_input_index);
1565
1566 if (true_target == nullptr && false_target == nullptr) {
1567 // Nothing to do. The code always falls through.
1568 return;
1569 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001570 // Constant condition, statically compared against "true" (integer value 1).
1571 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001572 if (true_target != nullptr) {
1573 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001574 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001575 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001576 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001577 if (false_target != nullptr) {
1578 __ jmp(false_target);
1579 }
1580 }
1581 return;
1582 }
1583
1584 // The following code generates these patterns:
1585 // (1) true_target == nullptr && false_target != nullptr
1586 // - opposite condition true => branch to false_target
1587 // (2) true_target != nullptr && false_target == nullptr
1588 // - condition true => branch to true_target
1589 // (3) true_target != nullptr && false_target != nullptr
1590 // - condition true => branch to true_target
1591 // - branch to false_target
1592 if (IsBooleanValueOrMaterializedCondition(cond)) {
1593 if (AreEflagsSetFrom(cond, instruction)) {
1594 if (true_target == nullptr) {
1595 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1596 } else {
1597 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1598 }
1599 } else {
1600 // Materialized condition, compare against 0.
1601 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1602 if (lhs.IsRegister()) {
1603 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1604 } else {
1605 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1606 }
1607 if (true_target == nullptr) {
1608 __ j(kEqual, false_target);
1609 } else {
1610 __ j(kNotEqual, true_target);
1611 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001612 }
1613 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001614 // Condition has not been materialized, use its inputs as the comparison and
1615 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001616 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001617
1618 // If this is a long or FP comparison that has been folded into
1619 // the HCondition, generate the comparison directly.
1620 Primitive::Type type = condition->InputAt(0)->GetType();
1621 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1622 GenerateCompareTestAndBranch(condition, true_target, false_target);
1623 return;
1624 }
1625
1626 Location lhs = condition->GetLocations()->InAt(0);
1627 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001628 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001629 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001630 if (true_target == nullptr) {
1631 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1632 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001633 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001634 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001635 }
David Brazdil0debae72015-11-12 18:37:00 +00001636
1637 // If neither branch falls through (case 3), the conditional branch to `true_target`
1638 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1639 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001640 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001641 }
1642}
1643
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001644void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001645 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1646 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001647 locations->SetInAt(0, Location::Any());
1648 }
1649}
1650
1651void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001652 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1653 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1654 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1655 nullptr : codegen_->GetLabelOf(true_successor);
1656 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1657 nullptr : codegen_->GetLabelOf(false_successor);
1658 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001659}
1660
1661void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1662 LocationSummary* locations = new (GetGraph()->GetArena())
1663 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001664 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001665 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001666 locations->SetInAt(0, Location::Any());
1667 }
1668}
1669
1670void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001671 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001672 GenerateTestAndBranch<Label>(deoptimize,
1673 /* condition_input_index */ 0,
1674 slow_path->GetEntryLabel(),
1675 /* false_target */ nullptr);
1676}
1677
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001678static bool SelectCanUseCMOV(HSelect* select) {
1679 // There are no conditional move instructions for XMMs.
1680 if (Primitive::IsFloatingPointType(select->GetType())) {
1681 return false;
1682 }
1683
1684 // A FP condition doesn't generate the single CC that we need.
1685 // In 32 bit mode, a long condition doesn't generate a single CC either.
1686 HInstruction* condition = select->GetCondition();
1687 if (condition->IsCondition()) {
1688 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1689 if (compare_type == Primitive::kPrimLong ||
1690 Primitive::IsFloatingPointType(compare_type)) {
1691 return false;
1692 }
1693 }
1694
1695 // We can generate a CMOV for this Select.
1696 return true;
1697}
1698
David Brazdil74eb1b22015-12-14 11:44:01 +00001699void LocationsBuilderX86::VisitSelect(HSelect* select) {
1700 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001701 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001702 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001703 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001704 } else {
1705 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001706 if (SelectCanUseCMOV(select)) {
1707 if (select->InputAt(1)->IsConstant()) {
1708 // Cmov can't handle a constant value.
1709 locations->SetInAt(1, Location::RequiresRegister());
1710 } else {
1711 locations->SetInAt(1, Location::Any());
1712 }
1713 } else {
1714 locations->SetInAt(1, Location::Any());
1715 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001716 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001717 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1718 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001719 }
1720 locations->SetOut(Location::SameAsFirstInput());
1721}
1722
1723void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1724 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001725 DCHECK(locations->InAt(0).Equals(locations->Out()));
1726 if (SelectCanUseCMOV(select)) {
1727 // If both the condition and the source types are integer, we can generate
1728 // a CMOV to implement Select.
1729
1730 HInstruction* select_condition = select->GetCondition();
1731 Condition cond = kNotEqual;
1732
1733 // Figure out how to test the 'condition'.
1734 if (select_condition->IsCondition()) {
1735 HCondition* condition = select_condition->AsCondition();
1736 if (!condition->IsEmittedAtUseSite()) {
1737 // This was a previously materialized condition.
1738 // Can we use the existing condition code?
1739 if (AreEflagsSetFrom(condition, select)) {
1740 // Materialization was the previous instruction. Condition codes are right.
1741 cond = X86Condition(condition->GetCondition());
1742 } else {
1743 // No, we have to recreate the condition code.
1744 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1745 __ testl(cond_reg, cond_reg);
1746 }
1747 } else {
1748 // We can't handle FP or long here.
1749 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1750 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1751 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001752 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001753 cond = X86Condition(condition->GetCondition());
1754 }
1755 } else {
1756 // Must be a boolean condition, which needs to be compared to 0.
1757 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1758 __ testl(cond_reg, cond_reg);
1759 }
1760
1761 // If the condition is true, overwrite the output, which already contains false.
1762 Location false_loc = locations->InAt(0);
1763 Location true_loc = locations->InAt(1);
1764 if (select->GetType() == Primitive::kPrimLong) {
1765 // 64 bit conditional move.
1766 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1767 Register false_low = false_loc.AsRegisterPairLow<Register>();
1768 if (true_loc.IsRegisterPair()) {
1769 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1770 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1771 } else {
1772 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1773 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1774 }
1775 } else {
1776 // 32 bit conditional move.
1777 Register false_reg = false_loc.AsRegister<Register>();
1778 if (true_loc.IsRegister()) {
1779 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1780 } else {
1781 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1782 }
1783 }
1784 } else {
1785 NearLabel false_target;
1786 GenerateTestAndBranch<NearLabel>(
1787 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1788 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1789 __ Bind(&false_target);
1790 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001791}
1792
David Srbecky0cf44932015-12-09 14:09:59 +00001793void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1794 new (GetGraph()->GetArena()) LocationSummary(info);
1795}
1796
David Srbeckyd28f4a02016-03-14 17:14:24 +00001797void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1798 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001799}
1800
1801void CodeGeneratorX86::GenerateNop() {
1802 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001803}
1804
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001805void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001806 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001807 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001808 // Handle the long/FP comparisons made in instruction simplification.
1809 switch (cond->InputAt(0)->GetType()) {
1810 case Primitive::kPrimLong: {
1811 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001812 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001813 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001814 locations->SetOut(Location::RequiresRegister());
1815 }
1816 break;
1817 }
1818 case Primitive::kPrimFloat:
1819 case Primitive::kPrimDouble: {
1820 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001821 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1822 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1823 } else if (cond->InputAt(1)->IsConstant()) {
1824 locations->SetInAt(1, Location::RequiresFpuRegister());
1825 } else {
1826 locations->SetInAt(1, Location::Any());
1827 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001828 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001829 locations->SetOut(Location::RequiresRegister());
1830 }
1831 break;
1832 }
1833 default:
1834 locations->SetInAt(0, Location::RequiresRegister());
1835 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001836 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001837 // We need a byte register.
1838 locations->SetOut(Location::RegisterLocation(ECX));
1839 }
1840 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001841 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001842}
1843
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001844void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001845 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001846 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001847 }
Mark Mendellc4701932015-04-10 13:18:51 -04001848
1849 LocationSummary* locations = cond->GetLocations();
1850 Location lhs = locations->InAt(0);
1851 Location rhs = locations->InAt(1);
1852 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001853 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001854
1855 switch (cond->InputAt(0)->GetType()) {
1856 default: {
1857 // Integer case.
1858
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001859 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001860 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001861 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001862 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001863 return;
1864 }
1865 case Primitive::kPrimLong:
1866 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1867 break;
1868 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001869 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001870 GenerateFPJumps(cond, &true_label, &false_label);
1871 break;
1872 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001873 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001874 GenerateFPJumps(cond, &true_label, &false_label);
1875 break;
1876 }
1877
1878 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001879 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001880
Roland Levillain4fa13f62015-07-06 18:11:54 +01001881 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001882 __ Bind(&false_label);
1883 __ xorl(reg, reg);
1884 __ jmp(&done_label);
1885
Roland Levillain4fa13f62015-07-06 18:11:54 +01001886 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001887 __ Bind(&true_label);
1888 __ movl(reg, Immediate(1));
1889 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001890}
1891
1892void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001893 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001894}
1895
1896void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001897 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001898}
1899
1900void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001901 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001902}
1903
1904void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001905 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001906}
1907
1908void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001909 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001910}
1911
1912void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001913 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001914}
1915
1916void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001917 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001918}
1919
1920void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001921 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001922}
1923
1924void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001925 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001926}
1927
1928void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001929 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001930}
1931
1932void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001933 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001934}
1935
1936void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001937 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001938}
1939
Aart Bike9f37602015-10-09 11:15:55 -07001940void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001941 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001942}
1943
1944void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001945 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001946}
1947
1948void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001949 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001950}
1951
1952void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001953 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001954}
1955
1956void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001957 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001958}
1959
1960void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001961 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001962}
1963
1964void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001965 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001966}
1967
1968void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001969 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001970}
1971
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001972void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001973 LocationSummary* locations =
1974 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001975 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001976}
1977
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001978void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001979 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001980}
1981
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001982void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1983 LocationSummary* locations =
1984 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1985 locations->SetOut(Location::ConstantLocation(constant));
1986}
1987
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001988void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001989 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001990}
1991
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001992void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001993 LocationSummary* locations =
1994 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001995 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001996}
1997
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001998void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001999 // Will be generated at use site.
2000}
2001
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002002void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2003 LocationSummary* locations =
2004 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2005 locations->SetOut(Location::ConstantLocation(constant));
2006}
2007
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002008void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002009 // Will be generated at use site.
2010}
2011
2012void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2013 LocationSummary* locations =
2014 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2015 locations->SetOut(Location::ConstantLocation(constant));
2016}
2017
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002018void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002019 // Will be generated at use site.
2020}
2021
Calin Juravle27df7582015-04-17 19:12:31 +01002022void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2023 memory_barrier->SetLocations(nullptr);
2024}
2025
2026void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002027 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002028}
2029
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002030void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002031 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002032}
2033
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002034void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002035 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002036}
2037
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002038void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002039 LocationSummary* locations =
2040 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002041 switch (ret->InputAt(0)->GetType()) {
2042 case Primitive::kPrimBoolean:
2043 case Primitive::kPrimByte:
2044 case Primitive::kPrimChar:
2045 case Primitive::kPrimShort:
2046 case Primitive::kPrimInt:
2047 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002048 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002049 break;
2050
2051 case Primitive::kPrimLong:
2052 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002053 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002054 break;
2055
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002056 case Primitive::kPrimFloat:
2057 case Primitive::kPrimDouble:
2058 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002059 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002060 break;
2061
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002062 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002063 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002064 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002065}
2066
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002067void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002068 if (kIsDebugBuild) {
2069 switch (ret->InputAt(0)->GetType()) {
2070 case Primitive::kPrimBoolean:
2071 case Primitive::kPrimByte:
2072 case Primitive::kPrimChar:
2073 case Primitive::kPrimShort:
2074 case Primitive::kPrimInt:
2075 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002076 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002077 break;
2078
2079 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002080 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2081 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002082 break;
2083
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002084 case Primitive::kPrimFloat:
2085 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002086 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002087 break;
2088
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002089 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002090 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002091 }
2092 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002093 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002094}
2095
Calin Juravle175dc732015-08-25 15:42:32 +01002096void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2097 // The trampoline uses the same calling convention as dex calling conventions,
2098 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2099 // the method_idx.
2100 HandleInvoke(invoke);
2101}
2102
2103void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2104 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2105}
2106
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002107void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002108 // Explicit clinit checks triggered by static invokes must have been pruned by
2109 // art::PrepareForRegisterAllocation.
2110 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002111
Mark Mendellfb8d2792015-03-31 22:16:59 -04002112 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002113 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002114 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002115 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002116 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002117 return;
2118 }
2119
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002120 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002121
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002122 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2123 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002124 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002125 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002126}
2127
Mark Mendell09ed1a32015-03-25 08:30:06 -04002128static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2129 if (invoke->GetLocations()->Intrinsified()) {
2130 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2131 intrinsic.Dispatch(invoke);
2132 return true;
2133 }
2134 return false;
2135}
2136
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002137void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002138 // Explicit clinit checks triggered by static invokes must have been pruned by
2139 // art::PrepareForRegisterAllocation.
2140 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002141
Mark Mendell09ed1a32015-03-25 08:30:06 -04002142 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2143 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002144 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002145
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002146 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002147 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002148 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002149 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002150}
2151
2152void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002153 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2154 if (intrinsic.TryDispatch(invoke)) {
2155 return;
2156 }
2157
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002158 HandleInvoke(invoke);
2159}
2160
2161void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002162 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002163 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002164}
2165
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002166void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002167 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2168 return;
2169 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002170
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002171 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002172 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002173 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002174}
2175
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002176void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002177 // This call to HandleInvoke allocates a temporary (core) register
2178 // which is also used to transfer the hidden argument from FP to
2179 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002180 HandleInvoke(invoke);
2181 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002182 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002183}
2184
2185void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2186 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002187 LocationSummary* locations = invoke->GetLocations();
2188 Register temp = locations->GetTemp(0).AsRegister<Register>();
2189 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002190 Location receiver = locations->InAt(0);
2191 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2192
Roland Levillain0d5a2812015-11-13 10:07:31 +00002193 // Set the hidden argument. This is safe to do this here, as XMM7
2194 // won't be modified thereafter, before the `call` instruction.
2195 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002196 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002197 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002198
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002199 if (receiver.IsStackSlot()) {
2200 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002201 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002202 __ movl(temp, Address(temp, class_offset));
2203 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002204 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002205 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002206 }
Roland Levillain4d027112015-07-01 15:41:14 +01002207 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002208 // Instead of simply (possibly) unpoisoning `temp` here, we should
2209 // emit a read barrier for the previous class reference load.
2210 // However this is not required in practice, as this is an
2211 // intermediate/temporary reference and because the current
2212 // concurrent copying collector keeps the from-space memory
2213 // intact/accessible until the end of the marking phase (the
2214 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002215 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002216 // temp = temp->GetAddressOfIMT()
2217 __ movl(temp,
2218 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002219 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002220 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002221 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002222 __ movl(temp, Address(temp, method_offset));
2223 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002224 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002225 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002226
2227 DCHECK(!codegen_->IsLeafMethod());
2228 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2229}
2230
Roland Levillain88cb1752014-10-20 16:36:47 +01002231void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2232 LocationSummary* locations =
2233 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2234 switch (neg->GetResultType()) {
2235 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002236 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002237 locations->SetInAt(0, Location::RequiresRegister());
2238 locations->SetOut(Location::SameAsFirstInput());
2239 break;
2240
Roland Levillain88cb1752014-10-20 16:36:47 +01002241 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002242 locations->SetInAt(0, Location::RequiresFpuRegister());
2243 locations->SetOut(Location::SameAsFirstInput());
2244 locations->AddTemp(Location::RequiresRegister());
2245 locations->AddTemp(Location::RequiresFpuRegister());
2246 break;
2247
Roland Levillain88cb1752014-10-20 16:36:47 +01002248 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002249 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002250 locations->SetOut(Location::SameAsFirstInput());
2251 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002252 break;
2253
2254 default:
2255 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2256 }
2257}
2258
2259void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2260 LocationSummary* locations = neg->GetLocations();
2261 Location out = locations->Out();
2262 Location in = locations->InAt(0);
2263 switch (neg->GetResultType()) {
2264 case Primitive::kPrimInt:
2265 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002266 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002267 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002268 break;
2269
2270 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002271 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002272 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002273 __ negl(out.AsRegisterPairLow<Register>());
2274 // Negation is similar to subtraction from zero. The least
2275 // significant byte triggers a borrow when it is different from
2276 // zero; to take it into account, add 1 to the most significant
2277 // byte if the carry flag (CF) is set to 1 after the first NEGL
2278 // operation.
2279 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2280 __ negl(out.AsRegisterPairHigh<Register>());
2281 break;
2282
Roland Levillain5368c212014-11-27 15:03:41 +00002283 case Primitive::kPrimFloat: {
2284 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002285 Register constant = locations->GetTemp(0).AsRegister<Register>();
2286 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002287 // Implement float negation with an exclusive or with value
2288 // 0x80000000 (mask for bit 31, representing the sign of a
2289 // single-precision floating-point number).
2290 __ movl(constant, Immediate(INT32_C(0x80000000)));
2291 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002292 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002293 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002294 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002295
Roland Levillain5368c212014-11-27 15:03:41 +00002296 case Primitive::kPrimDouble: {
2297 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002298 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002299 // Implement double negation with an exclusive or with value
2300 // 0x8000000000000000 (mask for bit 63, representing the sign of
2301 // a double-precision floating-point number).
2302 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002303 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002304 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002305 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002306
2307 default:
2308 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2309 }
2310}
2311
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002312void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2313 LocationSummary* locations =
2314 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2315 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2316 locations->SetInAt(0, Location::RequiresFpuRegister());
2317 locations->SetInAt(1, Location::RequiresRegister());
2318 locations->SetOut(Location::SameAsFirstInput());
2319 locations->AddTemp(Location::RequiresFpuRegister());
2320}
2321
2322void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2323 LocationSummary* locations = neg->GetLocations();
2324 Location out = locations->Out();
2325 DCHECK(locations->InAt(0).Equals(out));
2326
2327 Register constant_area = locations->InAt(1).AsRegister<Register>();
2328 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2329 if (neg->GetType() == Primitive::kPrimFloat) {
2330 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2331 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2332 } else {
2333 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2334 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2335 }
2336}
2337
Roland Levillaindff1f282014-11-05 14:15:05 +00002338void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002339 Primitive::Type result_type = conversion->GetResultType();
2340 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002341 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002342
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002343 // The float-to-long and double-to-long type conversions rely on a
2344 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002345 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002346 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2347 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002348 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002349 : LocationSummary::kNoCall;
2350 LocationSummary* locations =
2351 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2352
David Brazdilb2bd1c52015-03-25 11:17:37 +00002353 // The Java language does not allow treating boolean as an integral type but
2354 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002355
Roland Levillaindff1f282014-11-05 14:15:05 +00002356 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002357 case Primitive::kPrimByte:
2358 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002359 case Primitive::kPrimLong: {
2360 // Type conversion from long to byte is a result of code transformations.
2361 HInstruction* input = conversion->InputAt(0);
2362 Location input_location = input->IsConstant()
2363 ? Location::ConstantLocation(input->AsConstant())
2364 : Location::RegisterPairLocation(EAX, EDX);
2365 locations->SetInAt(0, input_location);
2366 // Make the output overlap to please the register allocator. This greatly simplifies
2367 // the validation of the linear scan implementation
2368 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2369 break;
2370 }
David Brazdil46e2a392015-03-16 17:31:52 +00002371 case Primitive::kPrimBoolean:
2372 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002373 case Primitive::kPrimShort:
2374 case Primitive::kPrimInt:
2375 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002376 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002377 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2378 // Make the output overlap to please the register allocator. This greatly simplifies
2379 // the validation of the linear scan implementation
2380 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002381 break;
2382
2383 default:
2384 LOG(FATAL) << "Unexpected type conversion from " << input_type
2385 << " to " << result_type;
2386 }
2387 break;
2388
Roland Levillain01a8d712014-11-14 16:27:39 +00002389 case Primitive::kPrimShort:
2390 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002391 case Primitive::kPrimLong:
2392 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002393 case Primitive::kPrimBoolean:
2394 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002395 case Primitive::kPrimByte:
2396 case Primitive::kPrimInt:
2397 case Primitive::kPrimChar:
2398 // Processing a Dex `int-to-short' instruction.
2399 locations->SetInAt(0, Location::Any());
2400 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2401 break;
2402
2403 default:
2404 LOG(FATAL) << "Unexpected type conversion from " << input_type
2405 << " to " << result_type;
2406 }
2407 break;
2408
Roland Levillain946e1432014-11-11 17:35:19 +00002409 case Primitive::kPrimInt:
2410 switch (input_type) {
2411 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002412 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002413 locations->SetInAt(0, Location::Any());
2414 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2415 break;
2416
2417 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002418 // Processing a Dex `float-to-int' instruction.
2419 locations->SetInAt(0, Location::RequiresFpuRegister());
2420 locations->SetOut(Location::RequiresRegister());
2421 locations->AddTemp(Location::RequiresFpuRegister());
2422 break;
2423
Roland Levillain946e1432014-11-11 17:35:19 +00002424 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002425 // Processing a Dex `double-to-int' instruction.
2426 locations->SetInAt(0, Location::RequiresFpuRegister());
2427 locations->SetOut(Location::RequiresRegister());
2428 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002429 break;
2430
2431 default:
2432 LOG(FATAL) << "Unexpected type conversion from " << input_type
2433 << " to " << result_type;
2434 }
2435 break;
2436
Roland Levillaindff1f282014-11-05 14:15:05 +00002437 case Primitive::kPrimLong:
2438 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002439 case Primitive::kPrimBoolean:
2440 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002441 case Primitive::kPrimByte:
2442 case Primitive::kPrimShort:
2443 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002444 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002445 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002446 locations->SetInAt(0, Location::RegisterLocation(EAX));
2447 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2448 break;
2449
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002450 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002451 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002452 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002453 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002454 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2455 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2456
Vladimir Marko949c91f2015-01-27 10:48:44 +00002457 // The runtime helper puts the result in EAX, EDX.
2458 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002459 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002460 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002461
2462 default:
2463 LOG(FATAL) << "Unexpected type conversion from " << input_type
2464 << " to " << result_type;
2465 }
2466 break;
2467
Roland Levillain981e4542014-11-14 11:47:14 +00002468 case Primitive::kPrimChar:
2469 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002470 case Primitive::kPrimLong:
2471 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002472 case Primitive::kPrimBoolean:
2473 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002474 case Primitive::kPrimByte:
2475 case Primitive::kPrimShort:
2476 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002477 // Processing a Dex `int-to-char' instruction.
2478 locations->SetInAt(0, Location::Any());
2479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2480 break;
2481
2482 default:
2483 LOG(FATAL) << "Unexpected type conversion from " << input_type
2484 << " to " << result_type;
2485 }
2486 break;
2487
Roland Levillaindff1f282014-11-05 14:15:05 +00002488 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002489 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002490 case Primitive::kPrimBoolean:
2491 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002492 case Primitive::kPrimByte:
2493 case Primitive::kPrimShort:
2494 case Primitive::kPrimInt:
2495 case Primitive::kPrimChar:
2496 // Processing a Dex `int-to-float' instruction.
2497 locations->SetInAt(0, Location::RequiresRegister());
2498 locations->SetOut(Location::RequiresFpuRegister());
2499 break;
2500
2501 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002502 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002503 locations->SetInAt(0, Location::Any());
2504 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002505 break;
2506
Roland Levillaincff13742014-11-17 14:32:17 +00002507 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002508 // Processing a Dex `double-to-float' instruction.
2509 locations->SetInAt(0, Location::RequiresFpuRegister());
2510 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002511 break;
2512
2513 default:
2514 LOG(FATAL) << "Unexpected type conversion from " << input_type
2515 << " to " << result_type;
2516 };
2517 break;
2518
Roland Levillaindff1f282014-11-05 14:15:05 +00002519 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002520 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002521 case Primitive::kPrimBoolean:
2522 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002523 case Primitive::kPrimByte:
2524 case Primitive::kPrimShort:
2525 case Primitive::kPrimInt:
2526 case Primitive::kPrimChar:
2527 // Processing a Dex `int-to-double' instruction.
2528 locations->SetInAt(0, Location::RequiresRegister());
2529 locations->SetOut(Location::RequiresFpuRegister());
2530 break;
2531
2532 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002533 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002534 locations->SetInAt(0, Location::Any());
2535 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002536 break;
2537
Roland Levillaincff13742014-11-17 14:32:17 +00002538 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002539 // Processing a Dex `float-to-double' instruction.
2540 locations->SetInAt(0, Location::RequiresFpuRegister());
2541 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002542 break;
2543
2544 default:
2545 LOG(FATAL) << "Unexpected type conversion from " << input_type
2546 << " to " << result_type;
2547 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002548 break;
2549
2550 default:
2551 LOG(FATAL) << "Unexpected type conversion from " << input_type
2552 << " to " << result_type;
2553 }
2554}
2555
2556void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2557 LocationSummary* locations = conversion->GetLocations();
2558 Location out = locations->Out();
2559 Location in = locations->InAt(0);
2560 Primitive::Type result_type = conversion->GetResultType();
2561 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002562 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002563 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002564 case Primitive::kPrimByte:
2565 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002566 case Primitive::kPrimLong:
2567 // Type conversion from long to byte is a result of code transformations.
2568 if (in.IsRegisterPair()) {
2569 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2570 } else {
2571 DCHECK(in.GetConstant()->IsLongConstant());
2572 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2573 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2574 }
2575 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002576 case Primitive::kPrimBoolean:
2577 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002578 case Primitive::kPrimShort:
2579 case Primitive::kPrimInt:
2580 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002581 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002582 if (in.IsRegister()) {
2583 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002584 } else {
2585 DCHECK(in.GetConstant()->IsIntConstant());
2586 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2587 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2588 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002589 break;
2590
2591 default:
2592 LOG(FATAL) << "Unexpected type conversion from " << input_type
2593 << " to " << result_type;
2594 }
2595 break;
2596
Roland Levillain01a8d712014-11-14 16:27:39 +00002597 case Primitive::kPrimShort:
2598 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002599 case Primitive::kPrimLong:
2600 // Type conversion from long to short is a result of code transformations.
2601 if (in.IsRegisterPair()) {
2602 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2603 } else if (in.IsDoubleStackSlot()) {
2604 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2605 } else {
2606 DCHECK(in.GetConstant()->IsLongConstant());
2607 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2608 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2609 }
2610 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002611 case Primitive::kPrimBoolean:
2612 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002613 case Primitive::kPrimByte:
2614 case Primitive::kPrimInt:
2615 case Primitive::kPrimChar:
2616 // Processing a Dex `int-to-short' instruction.
2617 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002618 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002619 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002620 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002621 } else {
2622 DCHECK(in.GetConstant()->IsIntConstant());
2623 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002624 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002625 }
2626 break;
2627
2628 default:
2629 LOG(FATAL) << "Unexpected type conversion from " << input_type
2630 << " to " << result_type;
2631 }
2632 break;
2633
Roland Levillain946e1432014-11-11 17:35:19 +00002634 case Primitive::kPrimInt:
2635 switch (input_type) {
2636 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002637 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002638 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002639 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002640 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002641 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002642 } else {
2643 DCHECK(in.IsConstant());
2644 DCHECK(in.GetConstant()->IsLongConstant());
2645 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002646 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002647 }
2648 break;
2649
Roland Levillain3f8f9362014-12-02 17:45:01 +00002650 case Primitive::kPrimFloat: {
2651 // Processing a Dex `float-to-int' instruction.
2652 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2653 Register output = out.AsRegister<Register>();
2654 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002655 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002656
2657 __ movl(output, Immediate(kPrimIntMax));
2658 // temp = int-to-float(output)
2659 __ cvtsi2ss(temp, output);
2660 // if input >= temp goto done
2661 __ comiss(input, temp);
2662 __ j(kAboveEqual, &done);
2663 // if input == NaN goto nan
2664 __ j(kUnordered, &nan);
2665 // output = float-to-int-truncate(input)
2666 __ cvttss2si(output, input);
2667 __ jmp(&done);
2668 __ Bind(&nan);
2669 // output = 0
2670 __ xorl(output, output);
2671 __ Bind(&done);
2672 break;
2673 }
2674
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002675 case Primitive::kPrimDouble: {
2676 // Processing a Dex `double-to-int' instruction.
2677 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2678 Register output = out.AsRegister<Register>();
2679 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002680 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002681
2682 __ movl(output, Immediate(kPrimIntMax));
2683 // temp = int-to-double(output)
2684 __ cvtsi2sd(temp, output);
2685 // if input >= temp goto done
2686 __ comisd(input, temp);
2687 __ j(kAboveEqual, &done);
2688 // if input == NaN goto nan
2689 __ j(kUnordered, &nan);
2690 // output = double-to-int-truncate(input)
2691 __ cvttsd2si(output, input);
2692 __ jmp(&done);
2693 __ Bind(&nan);
2694 // output = 0
2695 __ xorl(output, output);
2696 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002697 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002698 }
Roland Levillain946e1432014-11-11 17:35:19 +00002699
2700 default:
2701 LOG(FATAL) << "Unexpected type conversion from " << input_type
2702 << " to " << result_type;
2703 }
2704 break;
2705
Roland Levillaindff1f282014-11-05 14:15:05 +00002706 case Primitive::kPrimLong:
2707 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002708 case Primitive::kPrimBoolean:
2709 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002710 case Primitive::kPrimByte:
2711 case Primitive::kPrimShort:
2712 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002713 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002714 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002715 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2716 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002717 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002718 __ cdq();
2719 break;
2720
2721 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002722 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002723 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002724 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002725 break;
2726
Roland Levillaindff1f282014-11-05 14:15:05 +00002727 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002728 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002729 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002730 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002731 break;
2732
2733 default:
2734 LOG(FATAL) << "Unexpected type conversion from " << input_type
2735 << " to " << result_type;
2736 }
2737 break;
2738
Roland Levillain981e4542014-11-14 11:47:14 +00002739 case Primitive::kPrimChar:
2740 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002741 case Primitive::kPrimLong:
2742 // Type conversion from long to short is a result of code transformations.
2743 if (in.IsRegisterPair()) {
2744 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2745 } else if (in.IsDoubleStackSlot()) {
2746 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2747 } else {
2748 DCHECK(in.GetConstant()->IsLongConstant());
2749 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2750 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2751 }
2752 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002753 case Primitive::kPrimBoolean:
2754 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002755 case Primitive::kPrimByte:
2756 case Primitive::kPrimShort:
2757 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002758 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2759 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002760 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002761 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002762 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002763 } else {
2764 DCHECK(in.GetConstant()->IsIntConstant());
2765 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002766 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002767 }
2768 break;
2769
2770 default:
2771 LOG(FATAL) << "Unexpected type conversion from " << input_type
2772 << " to " << result_type;
2773 }
2774 break;
2775
Roland Levillaindff1f282014-11-05 14:15:05 +00002776 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002777 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002778 case Primitive::kPrimBoolean:
2779 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002780 case Primitive::kPrimByte:
2781 case Primitive::kPrimShort:
2782 case Primitive::kPrimInt:
2783 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002784 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002785 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002786 break;
2787
Roland Levillain6d0e4832014-11-27 18:31:21 +00002788 case Primitive::kPrimLong: {
2789 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002790 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002791
Roland Levillain232ade02015-04-20 15:14:36 +01002792 // Create stack space for the call to
2793 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2794 // TODO: enhance register allocator to ask for stack temporaries.
2795 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2796 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2797 __ subl(ESP, Immediate(adjustment));
2798 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002799
Roland Levillain232ade02015-04-20 15:14:36 +01002800 // Load the value to the FP stack, using temporaries if needed.
2801 PushOntoFPStack(in, 0, adjustment, false, true);
2802
2803 if (out.IsStackSlot()) {
2804 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2805 } else {
2806 __ fstps(Address(ESP, 0));
2807 Location stack_temp = Location::StackSlot(0);
2808 codegen_->Move32(out, stack_temp);
2809 }
2810
2811 // Remove the temporary stack space we allocated.
2812 if (adjustment != 0) {
2813 __ addl(ESP, Immediate(adjustment));
2814 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002815 break;
2816 }
2817
Roland Levillaincff13742014-11-17 14:32:17 +00002818 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002819 // Processing a Dex `double-to-float' instruction.
2820 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002821 break;
2822
2823 default:
2824 LOG(FATAL) << "Unexpected type conversion from " << input_type
2825 << " to " << result_type;
2826 };
2827 break;
2828
Roland Levillaindff1f282014-11-05 14:15:05 +00002829 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002830 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002831 case Primitive::kPrimBoolean:
2832 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002833 case Primitive::kPrimByte:
2834 case Primitive::kPrimShort:
2835 case Primitive::kPrimInt:
2836 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002837 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002838 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002839 break;
2840
Roland Levillain647b9ed2014-11-27 12:06:00 +00002841 case Primitive::kPrimLong: {
2842 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002843 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002844
Roland Levillain232ade02015-04-20 15:14:36 +01002845 // Create stack space for the call to
2846 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2847 // TODO: enhance register allocator to ask for stack temporaries.
2848 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2849 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2850 __ subl(ESP, Immediate(adjustment));
2851 }
2852
2853 // Load the value to the FP stack, using temporaries if needed.
2854 PushOntoFPStack(in, 0, adjustment, false, true);
2855
2856 if (out.IsDoubleStackSlot()) {
2857 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2858 } else {
2859 __ fstpl(Address(ESP, 0));
2860 Location stack_temp = Location::DoubleStackSlot(0);
2861 codegen_->Move64(out, stack_temp);
2862 }
2863
2864 // Remove the temporary stack space we allocated.
2865 if (adjustment != 0) {
2866 __ addl(ESP, Immediate(adjustment));
2867 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002868 break;
2869 }
2870
Roland Levillaincff13742014-11-17 14:32:17 +00002871 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002872 // Processing a Dex `float-to-double' instruction.
2873 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002874 break;
2875
2876 default:
2877 LOG(FATAL) << "Unexpected type conversion from " << input_type
2878 << " to " << result_type;
2879 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002880 break;
2881
2882 default:
2883 LOG(FATAL) << "Unexpected type conversion from " << input_type
2884 << " to " << result_type;
2885 }
2886}
2887
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002888void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002889 LocationSummary* locations =
2890 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002891 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002892 case Primitive::kPrimInt: {
2893 locations->SetInAt(0, Location::RequiresRegister());
2894 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2895 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2896 break;
2897 }
2898
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002899 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002900 locations->SetInAt(0, Location::RequiresRegister());
2901 locations->SetInAt(1, Location::Any());
2902 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002903 break;
2904 }
2905
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002906 case Primitive::kPrimFloat:
2907 case Primitive::kPrimDouble: {
2908 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002909 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2910 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002911 } else if (add->InputAt(1)->IsConstant()) {
2912 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002913 } else {
2914 locations->SetInAt(1, Location::Any());
2915 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002916 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002917 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002918 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002919
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002920 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002921 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2922 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002923 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002924}
2925
2926void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2927 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002928 Location first = locations->InAt(0);
2929 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002930 Location out = locations->Out();
2931
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002932 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002933 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002934 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002935 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2936 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002937 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2938 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002939 } else {
2940 __ leal(out.AsRegister<Register>(), Address(
2941 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2942 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002943 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002944 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2945 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2946 __ addl(out.AsRegister<Register>(), Immediate(value));
2947 } else {
2948 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2949 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002950 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002951 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002952 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002953 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002954 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002955 }
2956
2957 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002958 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002959 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2960 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002961 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002962 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2963 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002964 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002965 } else {
2966 DCHECK(second.IsConstant()) << second;
2967 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2968 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2969 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002970 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002971 break;
2972 }
2973
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002974 case Primitive::kPrimFloat: {
2975 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002976 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002977 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2978 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002979 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002980 __ addss(first.AsFpuRegister<XmmRegister>(),
2981 codegen_->LiteralFloatAddress(
2982 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2983 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2984 } else {
2985 DCHECK(second.IsStackSlot());
2986 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002987 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002988 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002989 }
2990
2991 case Primitive::kPrimDouble: {
2992 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002993 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002994 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2995 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002996 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002997 __ addsd(first.AsFpuRegister<XmmRegister>(),
2998 codegen_->LiteralDoubleAddress(
2999 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3000 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3001 } else {
3002 DCHECK(second.IsDoubleStackSlot());
3003 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003004 }
3005 break;
3006 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003007
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003008 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003009 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003010 }
3011}
3012
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003013void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003014 LocationSummary* locations =
3015 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003016 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003017 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003018 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003019 locations->SetInAt(0, Location::RequiresRegister());
3020 locations->SetInAt(1, Location::Any());
3021 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003022 break;
3023 }
Calin Juravle11351682014-10-23 15:38:15 +01003024 case Primitive::kPrimFloat:
3025 case Primitive::kPrimDouble: {
3026 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003027 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3028 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003029 } else if (sub->InputAt(1)->IsConstant()) {
3030 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003031 } else {
3032 locations->SetInAt(1, Location::Any());
3033 }
Calin Juravle11351682014-10-23 15:38:15 +01003034 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003035 break;
Calin Juravle11351682014-10-23 15:38:15 +01003036 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003037
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003038 default:
Calin Juravle11351682014-10-23 15:38:15 +01003039 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003040 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003041}
3042
3043void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3044 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003045 Location first = locations->InAt(0);
3046 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003047 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003048 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003049 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003050 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003051 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003052 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003053 __ subl(first.AsRegister<Register>(),
3054 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003055 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003056 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003057 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003058 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003059 }
3060
3061 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003062 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003063 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3064 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003065 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003066 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003067 __ sbbl(first.AsRegisterPairHigh<Register>(),
3068 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003069 } else {
3070 DCHECK(second.IsConstant()) << second;
3071 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3072 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3073 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003074 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003075 break;
3076 }
3077
Calin Juravle11351682014-10-23 15:38:15 +01003078 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003079 if (second.IsFpuRegister()) {
3080 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3081 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3082 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003083 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003084 __ subss(first.AsFpuRegister<XmmRegister>(),
3085 codegen_->LiteralFloatAddress(
3086 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3087 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3088 } else {
3089 DCHECK(second.IsStackSlot());
3090 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3091 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003092 break;
Calin Juravle11351682014-10-23 15:38:15 +01003093 }
3094
3095 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003096 if (second.IsFpuRegister()) {
3097 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3098 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3099 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003100 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003101 __ subsd(first.AsFpuRegister<XmmRegister>(),
3102 codegen_->LiteralDoubleAddress(
3103 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3104 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3105 } else {
3106 DCHECK(second.IsDoubleStackSlot());
3107 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3108 }
Calin Juravle11351682014-10-23 15:38:15 +01003109 break;
3110 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003111
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003112 default:
Calin Juravle11351682014-10-23 15:38:15 +01003113 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003114 }
3115}
3116
Calin Juravle34bacdf2014-10-07 20:23:36 +01003117void LocationsBuilderX86::VisitMul(HMul* mul) {
3118 LocationSummary* locations =
3119 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3120 switch (mul->GetResultType()) {
3121 case Primitive::kPrimInt:
3122 locations->SetInAt(0, Location::RequiresRegister());
3123 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003124 if (mul->InputAt(1)->IsIntConstant()) {
3125 // Can use 3 operand multiply.
3126 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3127 } else {
3128 locations->SetOut(Location::SameAsFirstInput());
3129 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003130 break;
3131 case Primitive::kPrimLong: {
3132 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003133 locations->SetInAt(1, Location::Any());
3134 locations->SetOut(Location::SameAsFirstInput());
3135 // Needed for imul on 32bits with 64bits output.
3136 locations->AddTemp(Location::RegisterLocation(EAX));
3137 locations->AddTemp(Location::RegisterLocation(EDX));
3138 break;
3139 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003140 case Primitive::kPrimFloat:
3141 case Primitive::kPrimDouble: {
3142 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003143 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3144 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003145 } else if (mul->InputAt(1)->IsConstant()) {
3146 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003147 } else {
3148 locations->SetInAt(1, Location::Any());
3149 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003150 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003151 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003152 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003153
3154 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003155 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003156 }
3157}
3158
3159void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3160 LocationSummary* locations = mul->GetLocations();
3161 Location first = locations->InAt(0);
3162 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003163 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003164
3165 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003166 case Primitive::kPrimInt:
3167 // The constant may have ended up in a register, so test explicitly to avoid
3168 // problems where the output may not be the same as the first operand.
3169 if (mul->InputAt(1)->IsIntConstant()) {
3170 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3171 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3172 } else if (second.IsRegister()) {
3173 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003174 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003175 } else {
3176 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003177 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003178 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003179 }
3180 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003181
3182 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003183 Register in1_hi = first.AsRegisterPairHigh<Register>();
3184 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003185 Register eax = locations->GetTemp(0).AsRegister<Register>();
3186 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003187
3188 DCHECK_EQ(EAX, eax);
3189 DCHECK_EQ(EDX, edx);
3190
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003191 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003192 // output: in1
3193 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3194 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3195 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003196 if (second.IsConstant()) {
3197 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003198
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003199 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3200 int32_t low_value = Low32Bits(value);
3201 int32_t high_value = High32Bits(value);
3202 Immediate low(low_value);
3203 Immediate high(high_value);
3204
3205 __ movl(eax, high);
3206 // eax <- in1.lo * in2.hi
3207 __ imull(eax, in1_lo);
3208 // in1.hi <- in1.hi * in2.lo
3209 __ imull(in1_hi, low);
3210 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3211 __ addl(in1_hi, eax);
3212 // move in2_lo to eax to prepare for double precision
3213 __ movl(eax, low);
3214 // edx:eax <- in1.lo * in2.lo
3215 __ mull(in1_lo);
3216 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3217 __ addl(in1_hi, edx);
3218 // in1.lo <- (in1.lo * in2.lo)[31:0];
3219 __ movl(in1_lo, eax);
3220 } else if (second.IsRegisterPair()) {
3221 Register in2_hi = second.AsRegisterPairHigh<Register>();
3222 Register in2_lo = second.AsRegisterPairLow<Register>();
3223
3224 __ movl(eax, in2_hi);
3225 // eax <- in1.lo * in2.hi
3226 __ imull(eax, in1_lo);
3227 // in1.hi <- in1.hi * in2.lo
3228 __ imull(in1_hi, in2_lo);
3229 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3230 __ addl(in1_hi, eax);
3231 // move in1_lo to eax to prepare for double precision
3232 __ movl(eax, in1_lo);
3233 // edx:eax <- in1.lo * in2.lo
3234 __ mull(in2_lo);
3235 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3236 __ addl(in1_hi, edx);
3237 // in1.lo <- (in1.lo * in2.lo)[31:0];
3238 __ movl(in1_lo, eax);
3239 } else {
3240 DCHECK(second.IsDoubleStackSlot()) << second;
3241 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3242 Address in2_lo(ESP, second.GetStackIndex());
3243
3244 __ movl(eax, in2_hi);
3245 // eax <- in1.lo * in2.hi
3246 __ imull(eax, in1_lo);
3247 // in1.hi <- in1.hi * in2.lo
3248 __ imull(in1_hi, in2_lo);
3249 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3250 __ addl(in1_hi, eax);
3251 // move in1_lo to eax to prepare for double precision
3252 __ movl(eax, in1_lo);
3253 // edx:eax <- in1.lo * in2.lo
3254 __ mull(in2_lo);
3255 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3256 __ addl(in1_hi, edx);
3257 // in1.lo <- (in1.lo * in2.lo)[31:0];
3258 __ movl(in1_lo, eax);
3259 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003260
3261 break;
3262 }
3263
Calin Juravleb5bfa962014-10-21 18:02:24 +01003264 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003265 DCHECK(first.Equals(locations->Out()));
3266 if (second.IsFpuRegister()) {
3267 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3268 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3269 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003270 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003271 __ mulss(first.AsFpuRegister<XmmRegister>(),
3272 codegen_->LiteralFloatAddress(
3273 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3274 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3275 } else {
3276 DCHECK(second.IsStackSlot());
3277 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3278 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003279 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003280 }
3281
3282 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003283 DCHECK(first.Equals(locations->Out()));
3284 if (second.IsFpuRegister()) {
3285 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3286 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3287 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003288 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003289 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3290 codegen_->LiteralDoubleAddress(
3291 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3292 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3293 } else {
3294 DCHECK(second.IsDoubleStackSlot());
3295 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3296 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003297 break;
3298 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003299
3300 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003301 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003302 }
3303}
3304
Roland Levillain232ade02015-04-20 15:14:36 +01003305void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3306 uint32_t temp_offset,
3307 uint32_t stack_adjustment,
3308 bool is_fp,
3309 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003310 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003311 DCHECK(!is_wide);
3312 if (is_fp) {
3313 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3314 } else {
3315 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3316 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003317 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003318 DCHECK(is_wide);
3319 if (is_fp) {
3320 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3321 } else {
3322 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3323 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003324 } else {
3325 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003326 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003327 Location stack_temp = Location::StackSlot(temp_offset);
3328 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003329 if (is_fp) {
3330 __ flds(Address(ESP, temp_offset));
3331 } else {
3332 __ filds(Address(ESP, temp_offset));
3333 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003334 } else {
3335 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3336 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003337 if (is_fp) {
3338 __ fldl(Address(ESP, temp_offset));
3339 } else {
3340 __ fildl(Address(ESP, temp_offset));
3341 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003342 }
3343 }
3344}
3345
3346void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3347 Primitive::Type type = rem->GetResultType();
3348 bool is_float = type == Primitive::kPrimFloat;
3349 size_t elem_size = Primitive::ComponentSize(type);
3350 LocationSummary* locations = rem->GetLocations();
3351 Location first = locations->InAt(0);
3352 Location second = locations->InAt(1);
3353 Location out = locations->Out();
3354
3355 // Create stack space for 2 elements.
3356 // TODO: enhance register allocator to ask for stack temporaries.
3357 __ subl(ESP, Immediate(2 * elem_size));
3358
3359 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003360 const bool is_wide = !is_float;
3361 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3362 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003363
3364 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003365 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003366 __ Bind(&retry);
3367 __ fprem();
3368
3369 // Move FP status to AX.
3370 __ fstsw();
3371
3372 // And see if the argument reduction is complete. This is signaled by the
3373 // C2 FPU flag bit set to 0.
3374 __ andl(EAX, Immediate(kC2ConditionMask));
3375 __ j(kNotEqual, &retry);
3376
3377 // We have settled on the final value. Retrieve it into an XMM register.
3378 // Store FP top of stack to real stack.
3379 if (is_float) {
3380 __ fsts(Address(ESP, 0));
3381 } else {
3382 __ fstl(Address(ESP, 0));
3383 }
3384
3385 // Pop the 2 items from the FP stack.
3386 __ fucompp();
3387
3388 // Load the value from the stack into an XMM register.
3389 DCHECK(out.IsFpuRegister()) << out;
3390 if (is_float) {
3391 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3392 } else {
3393 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3394 }
3395
3396 // And remove the temporary stack space we allocated.
3397 __ addl(ESP, Immediate(2 * elem_size));
3398}
3399
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003400
3401void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3402 DCHECK(instruction->IsDiv() || instruction->IsRem());
3403
3404 LocationSummary* locations = instruction->GetLocations();
3405 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003406 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003407
3408 Register out_register = locations->Out().AsRegister<Register>();
3409 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003410 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003411
3412 DCHECK(imm == 1 || imm == -1);
3413
3414 if (instruction->IsRem()) {
3415 __ xorl(out_register, out_register);
3416 } else {
3417 __ movl(out_register, input_register);
3418 if (imm == -1) {
3419 __ negl(out_register);
3420 }
3421 }
3422}
3423
3424
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003425void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003426 LocationSummary* locations = instruction->GetLocations();
3427
3428 Register out_register = locations->Out().AsRegister<Register>();
3429 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003430 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003431 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3432 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003433
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003434 Register num = locations->GetTemp(0).AsRegister<Register>();
3435
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003436 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003437 __ testl(input_register, input_register);
3438 __ cmovl(kGreaterEqual, num, input_register);
3439 int shift = CTZ(imm);
3440 __ sarl(num, Immediate(shift));
3441
3442 if (imm < 0) {
3443 __ negl(num);
3444 }
3445
3446 __ movl(out_register, num);
3447}
3448
3449void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3450 DCHECK(instruction->IsDiv() || instruction->IsRem());
3451
3452 LocationSummary* locations = instruction->GetLocations();
3453 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3454
3455 Register eax = locations->InAt(0).AsRegister<Register>();
3456 Register out = locations->Out().AsRegister<Register>();
3457 Register num;
3458 Register edx;
3459
3460 if (instruction->IsDiv()) {
3461 edx = locations->GetTemp(0).AsRegister<Register>();
3462 num = locations->GetTemp(1).AsRegister<Register>();
3463 } else {
3464 edx = locations->Out().AsRegister<Register>();
3465 num = locations->GetTemp(0).AsRegister<Register>();
3466 }
3467
3468 DCHECK_EQ(EAX, eax);
3469 DCHECK_EQ(EDX, edx);
3470 if (instruction->IsDiv()) {
3471 DCHECK_EQ(EAX, out);
3472 } else {
3473 DCHECK_EQ(EDX, out);
3474 }
3475
3476 int64_t magic;
3477 int shift;
3478 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3479
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003480 // Save the numerator.
3481 __ movl(num, eax);
3482
3483 // EAX = magic
3484 __ movl(eax, Immediate(magic));
3485
3486 // EDX:EAX = magic * numerator
3487 __ imull(num);
3488
3489 if (imm > 0 && magic < 0) {
3490 // EDX += num
3491 __ addl(edx, num);
3492 } else if (imm < 0 && magic > 0) {
3493 __ subl(edx, num);
3494 }
3495
3496 // Shift if needed.
3497 if (shift != 0) {
3498 __ sarl(edx, Immediate(shift));
3499 }
3500
3501 // EDX += 1 if EDX < 0
3502 __ movl(eax, edx);
3503 __ shrl(edx, Immediate(31));
3504 __ addl(edx, eax);
3505
3506 if (instruction->IsRem()) {
3507 __ movl(eax, num);
3508 __ imull(edx, Immediate(imm));
3509 __ subl(eax, edx);
3510 __ movl(edx, eax);
3511 } else {
3512 __ movl(eax, edx);
3513 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003514}
3515
Calin Juravlebacfec32014-11-14 15:54:36 +00003516void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3517 DCHECK(instruction->IsDiv() || instruction->IsRem());
3518
3519 LocationSummary* locations = instruction->GetLocations();
3520 Location out = locations->Out();
3521 Location first = locations->InAt(0);
3522 Location second = locations->InAt(1);
3523 bool is_div = instruction->IsDiv();
3524
3525 switch (instruction->GetResultType()) {
3526 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003527 DCHECK_EQ(EAX, first.AsRegister<Register>());
3528 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003529
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003530 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003531 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003532
3533 if (imm == 0) {
3534 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3535 } else if (imm == 1 || imm == -1) {
3536 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003537 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003538 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003539 } else {
3540 DCHECK(imm <= -2 || imm >= 2);
3541 GenerateDivRemWithAnyConstant(instruction);
3542 }
3543 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003544 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3545 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003546 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003547
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003548 Register second_reg = second.AsRegister<Register>();
3549 // 0x80000000/-1 triggers an arithmetic exception!
3550 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3551 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003552
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003553 __ cmpl(second_reg, Immediate(-1));
3554 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003555
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556 // edx:eax <- sign-extended of eax
3557 __ cdq();
3558 // eax = quotient, edx = remainder
3559 __ idivl(second_reg);
3560 __ Bind(slow_path->GetExitLabel());
3561 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003562 break;
3563 }
3564
3565 case Primitive::kPrimLong: {
3566 InvokeRuntimeCallingConvention calling_convention;
3567 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3568 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3569 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3570 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3571 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3572 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3573
3574 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003575 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003576 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003577 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003578 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003579 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003580 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003581 break;
3582 }
3583
3584 default:
3585 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3586 }
3587}
3588
Calin Juravle7c4954d2014-10-28 16:57:40 +00003589void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003590 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003591 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003592 : LocationSummary::kNoCall;
3593 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3594
Calin Juravle7c4954d2014-10-28 16:57:40 +00003595 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003596 case Primitive::kPrimInt: {
3597 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003598 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003599 locations->SetOut(Location::SameAsFirstInput());
3600 // Intel uses edx:eax as the dividend.
3601 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003602 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3603 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3604 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003605 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003606 locations->AddTemp(Location::RequiresRegister());
3607 }
Calin Juravled0d48522014-11-04 16:40:20 +00003608 break;
3609 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003610 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003611 InvokeRuntimeCallingConvention calling_convention;
3612 locations->SetInAt(0, Location::RegisterPairLocation(
3613 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3614 locations->SetInAt(1, Location::RegisterPairLocation(
3615 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3616 // Runtime helper puts the result in EAX, EDX.
3617 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003618 break;
3619 }
3620 case Primitive::kPrimFloat:
3621 case Primitive::kPrimDouble: {
3622 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003623 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3624 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003625 } else if (div->InputAt(1)->IsConstant()) {
3626 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003627 } else {
3628 locations->SetInAt(1, Location::Any());
3629 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003630 locations->SetOut(Location::SameAsFirstInput());
3631 break;
3632 }
3633
3634 default:
3635 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3636 }
3637}
3638
3639void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3640 LocationSummary* locations = div->GetLocations();
3641 Location first = locations->InAt(0);
3642 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003643
3644 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003645 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003646 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003647 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003648 break;
3649 }
3650
3651 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003652 if (second.IsFpuRegister()) {
3653 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3654 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3655 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003656 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003657 __ divss(first.AsFpuRegister<XmmRegister>(),
3658 codegen_->LiteralFloatAddress(
3659 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3660 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3661 } else {
3662 DCHECK(second.IsStackSlot());
3663 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3664 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003665 break;
3666 }
3667
3668 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003669 if (second.IsFpuRegister()) {
3670 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3671 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3672 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003673 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003674 __ divsd(first.AsFpuRegister<XmmRegister>(),
3675 codegen_->LiteralDoubleAddress(
3676 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3677 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3678 } else {
3679 DCHECK(second.IsDoubleStackSlot());
3680 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3681 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003682 break;
3683 }
3684
3685 default:
3686 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3687 }
3688}
3689
Calin Juravlebacfec32014-11-14 15:54:36 +00003690void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003691 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003692
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003693 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003694 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003695 : LocationSummary::kNoCall;
3696 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003697
Calin Juravled2ec87d2014-12-08 14:24:46 +00003698 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003699 case Primitive::kPrimInt: {
3700 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003701 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003702 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003703 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3704 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3705 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003706 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003707 locations->AddTemp(Location::RequiresRegister());
3708 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003709 break;
3710 }
3711 case Primitive::kPrimLong: {
3712 InvokeRuntimeCallingConvention calling_convention;
3713 locations->SetInAt(0, Location::RegisterPairLocation(
3714 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3715 locations->SetInAt(1, Location::RegisterPairLocation(
3716 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3717 // Runtime helper puts the result in EAX, EDX.
3718 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3719 break;
3720 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003721 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003722 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003723 locations->SetInAt(0, Location::Any());
3724 locations->SetInAt(1, Location::Any());
3725 locations->SetOut(Location::RequiresFpuRegister());
3726 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003727 break;
3728 }
3729
3730 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003731 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003732 }
3733}
3734
3735void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3736 Primitive::Type type = rem->GetResultType();
3737 switch (type) {
3738 case Primitive::kPrimInt:
3739 case Primitive::kPrimLong: {
3740 GenerateDivRemIntegral(rem);
3741 break;
3742 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003743 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003744 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003745 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003746 break;
3747 }
3748 default:
3749 LOG(FATAL) << "Unexpected rem type " << type;
3750 }
3751}
3752
Calin Juravled0d48522014-11-04 16:40:20 +00003753void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003754 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003755 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003756 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003757 case Primitive::kPrimByte:
3758 case Primitive::kPrimChar:
3759 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003760 case Primitive::kPrimInt: {
3761 locations->SetInAt(0, Location::Any());
3762 break;
3763 }
3764 case Primitive::kPrimLong: {
3765 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3766 if (!instruction->IsConstant()) {
3767 locations->AddTemp(Location::RequiresRegister());
3768 }
3769 break;
3770 }
3771 default:
3772 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3773 }
Calin Juravled0d48522014-11-04 16:40:20 +00003774}
3775
3776void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003777 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003778 codegen_->AddSlowPath(slow_path);
3779
3780 LocationSummary* locations = instruction->GetLocations();
3781 Location value = locations->InAt(0);
3782
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003783 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003784 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003785 case Primitive::kPrimByte:
3786 case Primitive::kPrimChar:
3787 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003788 case Primitive::kPrimInt: {
3789 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003790 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003791 __ j(kEqual, slow_path->GetEntryLabel());
3792 } else if (value.IsStackSlot()) {
3793 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3794 __ j(kEqual, slow_path->GetEntryLabel());
3795 } else {
3796 DCHECK(value.IsConstant()) << value;
3797 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003798 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003799 }
3800 }
3801 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003802 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003803 case Primitive::kPrimLong: {
3804 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003805 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003806 __ movl(temp, value.AsRegisterPairLow<Register>());
3807 __ orl(temp, value.AsRegisterPairHigh<Register>());
3808 __ j(kEqual, slow_path->GetEntryLabel());
3809 } else {
3810 DCHECK(value.IsConstant()) << value;
3811 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3812 __ jmp(slow_path->GetEntryLabel());
3813 }
3814 }
3815 break;
3816 }
3817 default:
3818 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003819 }
Calin Juravled0d48522014-11-04 16:40:20 +00003820}
3821
Calin Juravle9aec02f2014-11-18 23:06:35 +00003822void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3823 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3824
3825 LocationSummary* locations =
3826 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3827
3828 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003829 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003830 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003831 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003832 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003833 // The shift count needs to be in CL or a constant.
3834 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003835 locations->SetOut(Location::SameAsFirstInput());
3836 break;
3837 }
3838 default:
3839 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3840 }
3841}
3842
3843void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3844 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3845
3846 LocationSummary* locations = op->GetLocations();
3847 Location first = locations->InAt(0);
3848 Location second = locations->InAt(1);
3849 DCHECK(first.Equals(locations->Out()));
3850
3851 switch (op->GetResultType()) {
3852 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003853 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003854 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003855 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003856 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003857 DCHECK_EQ(ECX, second_reg);
3858 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003859 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003860 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003861 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003862 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003863 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003864 }
3865 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003866 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003867 if (shift == 0) {
3868 return;
3869 }
3870 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003871 if (op->IsShl()) {
3872 __ shll(first_reg, imm);
3873 } else if (op->IsShr()) {
3874 __ sarl(first_reg, imm);
3875 } else {
3876 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003877 }
3878 }
3879 break;
3880 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003881 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003882 if (second.IsRegister()) {
3883 Register second_reg = second.AsRegister<Register>();
3884 DCHECK_EQ(ECX, second_reg);
3885 if (op->IsShl()) {
3886 GenerateShlLong(first, second_reg);
3887 } else if (op->IsShr()) {
3888 GenerateShrLong(first, second_reg);
3889 } else {
3890 GenerateUShrLong(first, second_reg);
3891 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003892 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003893 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003894 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003895 // Nothing to do if the shift is 0, as the input is already the output.
3896 if (shift != 0) {
3897 if (op->IsShl()) {
3898 GenerateShlLong(first, shift);
3899 } else if (op->IsShr()) {
3900 GenerateShrLong(first, shift);
3901 } else {
3902 GenerateUShrLong(first, shift);
3903 }
3904 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003905 }
3906 break;
3907 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003908 default:
3909 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3910 }
3911}
3912
Mark P Mendell73945692015-04-29 14:56:17 +00003913void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3914 Register low = loc.AsRegisterPairLow<Register>();
3915 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003916 if (shift == 1) {
3917 // This is just an addition.
3918 __ addl(low, low);
3919 __ adcl(high, high);
3920 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003921 // Shift by 32 is easy. High gets low, and low gets 0.
3922 codegen_->EmitParallelMoves(
3923 loc.ToLow(),
3924 loc.ToHigh(),
3925 Primitive::kPrimInt,
3926 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3927 loc.ToLow(),
3928 Primitive::kPrimInt);
3929 } else if (shift > 32) {
3930 // Low part becomes 0. High part is low part << (shift-32).
3931 __ movl(high, low);
3932 __ shll(high, Immediate(shift - 32));
3933 __ xorl(low, low);
3934 } else {
3935 // Between 1 and 31.
3936 __ shld(high, low, Immediate(shift));
3937 __ shll(low, Immediate(shift));
3938 }
3939}
3940
Calin Juravle9aec02f2014-11-18 23:06:35 +00003941void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003942 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003943 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3944 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3945 __ testl(shifter, Immediate(32));
3946 __ j(kEqual, &done);
3947 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3948 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3949 __ Bind(&done);
3950}
3951
Mark P Mendell73945692015-04-29 14:56:17 +00003952void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3953 Register low = loc.AsRegisterPairLow<Register>();
3954 Register high = loc.AsRegisterPairHigh<Register>();
3955 if (shift == 32) {
3956 // Need to copy the sign.
3957 DCHECK_NE(low, high);
3958 __ movl(low, high);
3959 __ sarl(high, Immediate(31));
3960 } else if (shift > 32) {
3961 DCHECK_NE(low, high);
3962 // High part becomes sign. Low part is shifted by shift - 32.
3963 __ movl(low, high);
3964 __ sarl(high, Immediate(31));
3965 __ sarl(low, Immediate(shift - 32));
3966 } else {
3967 // Between 1 and 31.
3968 __ shrd(low, high, Immediate(shift));
3969 __ sarl(high, Immediate(shift));
3970 }
3971}
3972
Calin Juravle9aec02f2014-11-18 23:06:35 +00003973void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003974 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003975 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3976 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3977 __ testl(shifter, Immediate(32));
3978 __ j(kEqual, &done);
3979 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3980 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3981 __ Bind(&done);
3982}
3983
Mark P Mendell73945692015-04-29 14:56:17 +00003984void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3985 Register low = loc.AsRegisterPairLow<Register>();
3986 Register high = loc.AsRegisterPairHigh<Register>();
3987 if (shift == 32) {
3988 // Shift by 32 is easy. Low gets high, and high gets 0.
3989 codegen_->EmitParallelMoves(
3990 loc.ToHigh(),
3991 loc.ToLow(),
3992 Primitive::kPrimInt,
3993 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3994 loc.ToHigh(),
3995 Primitive::kPrimInt);
3996 } else if (shift > 32) {
3997 // Low part is high >> (shift - 32). High part becomes 0.
3998 __ movl(low, high);
3999 __ shrl(low, Immediate(shift - 32));
4000 __ xorl(high, high);
4001 } else {
4002 // Between 1 and 31.
4003 __ shrd(low, high, Immediate(shift));
4004 __ shrl(high, Immediate(shift));
4005 }
4006}
4007
Calin Juravle9aec02f2014-11-18 23:06:35 +00004008void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004009 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004010 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4011 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4012 __ testl(shifter, Immediate(32));
4013 __ j(kEqual, &done);
4014 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4015 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4016 __ Bind(&done);
4017}
4018
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004019void LocationsBuilderX86::VisitRor(HRor* ror) {
4020 LocationSummary* locations =
4021 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4022
4023 switch (ror->GetResultType()) {
4024 case Primitive::kPrimLong:
4025 // Add the temporary needed.
4026 locations->AddTemp(Location::RequiresRegister());
4027 FALLTHROUGH_INTENDED;
4028 case Primitive::kPrimInt:
4029 locations->SetInAt(0, Location::RequiresRegister());
4030 // The shift count needs to be in CL (unless it is a constant).
4031 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4032 locations->SetOut(Location::SameAsFirstInput());
4033 break;
4034 default:
4035 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4036 UNREACHABLE();
4037 }
4038}
4039
4040void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4041 LocationSummary* locations = ror->GetLocations();
4042 Location first = locations->InAt(0);
4043 Location second = locations->InAt(1);
4044
4045 if (ror->GetResultType() == Primitive::kPrimInt) {
4046 Register first_reg = first.AsRegister<Register>();
4047 if (second.IsRegister()) {
4048 Register second_reg = second.AsRegister<Register>();
4049 __ rorl(first_reg, second_reg);
4050 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004051 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004052 __ rorl(first_reg, imm);
4053 }
4054 return;
4055 }
4056
4057 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4058 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4059 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4060 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4061 if (second.IsRegister()) {
4062 Register second_reg = second.AsRegister<Register>();
4063 DCHECK_EQ(second_reg, ECX);
4064 __ movl(temp_reg, first_reg_hi);
4065 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4066 __ shrd(first_reg_lo, temp_reg, second_reg);
4067 __ movl(temp_reg, first_reg_hi);
4068 __ testl(second_reg, Immediate(32));
4069 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4070 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4071 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004072 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004073 if (shift_amt == 0) {
4074 // Already fine.
4075 return;
4076 }
4077 if (shift_amt == 32) {
4078 // Just swap.
4079 __ movl(temp_reg, first_reg_lo);
4080 __ movl(first_reg_lo, first_reg_hi);
4081 __ movl(first_reg_hi, temp_reg);
4082 return;
4083 }
4084
4085 Immediate imm(shift_amt);
4086 // Save the constents of the low value.
4087 __ movl(temp_reg, first_reg_lo);
4088
4089 // Shift right into low, feeding bits from high.
4090 __ shrd(first_reg_lo, first_reg_hi, imm);
4091
4092 // Shift right into high, feeding bits from the original low.
4093 __ shrd(first_reg_hi, temp_reg, imm);
4094
4095 // Swap if needed.
4096 if (shift_amt > 32) {
4097 __ movl(temp_reg, first_reg_lo);
4098 __ movl(first_reg_lo, first_reg_hi);
4099 __ movl(first_reg_hi, temp_reg);
4100 }
4101 }
4102}
4103
Calin Juravle9aec02f2014-11-18 23:06:35 +00004104void LocationsBuilderX86::VisitShl(HShl* shl) {
4105 HandleShift(shl);
4106}
4107
4108void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4109 HandleShift(shl);
4110}
4111
4112void LocationsBuilderX86::VisitShr(HShr* shr) {
4113 HandleShift(shr);
4114}
4115
4116void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4117 HandleShift(shr);
4118}
4119
4120void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4121 HandleShift(ushr);
4122}
4123
4124void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4125 HandleShift(ushr);
4126}
4127
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004128void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004129 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004130 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004131 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004132 if (instruction->IsStringAlloc()) {
4133 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4134 } else {
4135 InvokeRuntimeCallingConvention calling_convention;
4136 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4137 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4138 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004139}
4140
4141void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004142 // Note: if heap poisoning is enabled, the entry point takes cares
4143 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004144 if (instruction->IsStringAlloc()) {
4145 // String is allocated through StringFactory. Call NewEmptyString entry point.
4146 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004147 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004148 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4149 __ call(Address(temp, code_offset.Int32Value()));
4150 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4151 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004152 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004153 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4154 DCHECK(!codegen_->IsLeafMethod());
4155 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004156}
4157
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004158void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4159 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004160 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004161 locations->SetOut(Location::RegisterLocation(EAX));
4162 InvokeRuntimeCallingConvention calling_convention;
4163 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004164 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004165 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004166}
4167
4168void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4169 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004170 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004171 // Note: if heap poisoning is enabled, the entry point takes cares
4172 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004173 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004174 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004175 DCHECK(!codegen_->IsLeafMethod());
4176}
4177
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004178void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004179 LocationSummary* locations =
4180 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004181 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4182 if (location.IsStackSlot()) {
4183 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4184 } else if (location.IsDoubleStackSlot()) {
4185 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004186 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004187 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004188}
4189
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004190void InstructionCodeGeneratorX86::VisitParameterValue(
4191 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4192}
4193
4194void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4195 LocationSummary* locations =
4196 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4197 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4198}
4199
4200void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004201}
4202
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004203void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4204 LocationSummary* locations =
4205 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4206 locations->SetInAt(0, Location::RequiresRegister());
4207 locations->SetOut(Location::RequiresRegister());
4208}
4209
4210void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4211 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004212 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004213 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004214 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004215 __ movl(locations->Out().AsRegister<Register>(),
4216 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004217 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004218 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004219 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004220 __ movl(locations->Out().AsRegister<Register>(),
4221 Address(locations->InAt(0).AsRegister<Register>(),
4222 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4223 // temp = temp->GetImtEntryAt(method_offset);
4224 __ movl(locations->Out().AsRegister<Register>(),
4225 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004226 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004227}
4228
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004229void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004230 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004231 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004232 locations->SetInAt(0, Location::RequiresRegister());
4233 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004234}
4235
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004236void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4237 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004238 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004239 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004240 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004241 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004242 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004243 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004244 break;
4245
4246 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004247 __ notl(out.AsRegisterPairLow<Register>());
4248 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004249 break;
4250
4251 default:
4252 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4253 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004254}
4255
David Brazdil66d126e2015-04-03 16:02:44 +01004256void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4257 LocationSummary* locations =
4258 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4259 locations->SetInAt(0, Location::RequiresRegister());
4260 locations->SetOut(Location::SameAsFirstInput());
4261}
4262
4263void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004264 LocationSummary* locations = bool_not->GetLocations();
4265 Location in = locations->InAt(0);
4266 Location out = locations->Out();
4267 DCHECK(in.Equals(out));
4268 __ xorl(out.AsRegister<Register>(), Immediate(1));
4269}
4270
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004271void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004272 LocationSummary* locations =
4273 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004274 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004275 case Primitive::kPrimBoolean:
4276 case Primitive::kPrimByte:
4277 case Primitive::kPrimShort:
4278 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004279 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004280 case Primitive::kPrimLong: {
4281 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004282 locations->SetInAt(1, Location::Any());
4283 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4284 break;
4285 }
4286 case Primitive::kPrimFloat:
4287 case Primitive::kPrimDouble: {
4288 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004289 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4290 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4291 } else if (compare->InputAt(1)->IsConstant()) {
4292 locations->SetInAt(1, Location::RequiresFpuRegister());
4293 } else {
4294 locations->SetInAt(1, Location::Any());
4295 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004296 locations->SetOut(Location::RequiresRegister());
4297 break;
4298 }
4299 default:
4300 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4301 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004302}
4303
4304void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004305 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004306 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004307 Location left = locations->InAt(0);
4308 Location right = locations->InAt(1);
4309
Mark Mendell0c9497d2015-08-21 09:30:05 -04004310 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004311 Condition less_cond = kLess;
4312
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004313 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004314 case Primitive::kPrimBoolean:
4315 case Primitive::kPrimByte:
4316 case Primitive::kPrimShort:
4317 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004318 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004319 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004320 break;
4321 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004322 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004323 Register left_low = left.AsRegisterPairLow<Register>();
4324 Register left_high = left.AsRegisterPairHigh<Register>();
4325 int32_t val_low = 0;
4326 int32_t val_high = 0;
4327 bool right_is_const = false;
4328
4329 if (right.IsConstant()) {
4330 DCHECK(right.GetConstant()->IsLongConstant());
4331 right_is_const = true;
4332 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4333 val_low = Low32Bits(val);
4334 val_high = High32Bits(val);
4335 }
4336
Calin Juravleddb7df22014-11-25 20:56:51 +00004337 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004338 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004339 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004340 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004341 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004342 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004343 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004344 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004345 __ j(kLess, &less); // Signed compare.
4346 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004347 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004348 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004349 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004350 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004351 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004352 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004353 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004354 }
Aart Bika19616e2016-02-01 18:57:58 -08004355 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004356 break;
4357 }
4358 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004359 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004360 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004361 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004362 break;
4363 }
4364 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004365 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004366 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004367 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004368 break;
4369 }
4370 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004371 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004372 }
Aart Bika19616e2016-02-01 18:57:58 -08004373
Calin Juravleddb7df22014-11-25 20:56:51 +00004374 __ movl(out, Immediate(0));
4375 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004376 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004377
4378 __ Bind(&greater);
4379 __ movl(out, Immediate(1));
4380 __ jmp(&done);
4381
4382 __ Bind(&less);
4383 __ movl(out, Immediate(-1));
4384
4385 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004386}
4387
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004388void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004389 LocationSummary* locations =
4390 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004391 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004392 locations->SetInAt(i, Location::Any());
4393 }
4394 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004395}
4396
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004397void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004398 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004399}
4400
Roland Levillain7c1559a2015-12-15 10:55:36 +00004401void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004402 /*
4403 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4404 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4405 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4406 */
4407 switch (kind) {
4408 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004409 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004410 break;
4411 }
4412 case MemBarrierKind::kAnyStore:
4413 case MemBarrierKind::kLoadAny:
4414 case MemBarrierKind::kStoreStore: {
4415 // nop
4416 break;
4417 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004418 case MemBarrierKind::kNTStoreStore:
4419 // Non-Temporal Store/Store needs an explicit fence.
4420 MemoryFence(/* non-temporal */ true);
4421 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004422 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004423}
4424
Vladimir Markodc151b22015-10-15 18:02:30 +01004425HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4426 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004427 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004428 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4429
4430 // We disable pc-relative load when there is an irreducible loop, as the optimization
4431 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004432 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4433 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004434 if (GetGraph()->HasIrreducibleLoops() &&
4435 (dispatch_info.method_load_kind ==
4436 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4437 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4438 }
4439 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004440 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4441 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4442 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4443 // (Though the direct CALL ptr16:32 is available for consideration).
4444 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004445 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004446 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004447 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004448 0u
4449 };
4450 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004451 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004452 }
4453}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004454
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004455Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4456 Register temp) {
4457 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004458 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004459 if (!invoke->GetLocations()->Intrinsified()) {
4460 return location.AsRegister<Register>();
4461 }
4462 // For intrinsics we allow any location, so it may be on the stack.
4463 if (!location.IsRegister()) {
4464 __ movl(temp, Address(ESP, location.GetStackIndex()));
4465 return temp;
4466 }
4467 // For register locations, check if the register was saved. If so, get it from the stack.
4468 // Note: There is a chance that the register was saved but not overwritten, so we could
4469 // save one load. However, since this is just an intrinsic slow path we prefer this
4470 // simple and more robust approach rather that trying to determine if that's the case.
4471 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004472 if (slow_path != nullptr) {
4473 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4474 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4475 __ movl(temp, Address(ESP, stack_offset));
4476 return temp;
4477 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004478 }
4479 return location.AsRegister<Register>();
4480}
4481
Serguei Katkov288c7a82016-05-16 11:53:15 +06004482Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4483 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004484 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4485 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004486 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004487 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004488 uint32_t offset =
4489 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4490 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004491 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004492 }
Vladimir Marko58155012015-08-19 12:49:41 +00004493 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004494 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004495 break;
4496 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4497 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4498 break;
4499 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004500 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004501 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4502 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004503 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4504 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004505 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4506 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4507 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004508 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004509 // Bind a new fixup label at the end of the "movl" insn.
4510 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004511 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004512 break;
4513 }
Vladimir Marko58155012015-08-19 12:49:41 +00004514 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004515 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004516 Register method_reg;
4517 Register reg = temp.AsRegister<Register>();
4518 if (current_method.IsRegister()) {
4519 method_reg = current_method.AsRegister<Register>();
4520 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004521 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004522 DCHECK(!current_method.IsValid());
4523 method_reg = reg;
4524 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4525 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004526 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004527 __ movl(reg, Address(method_reg,
4528 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004529 // temp = temp[index_in_cache];
4530 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4531 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004532 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4533 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004534 }
Vladimir Marko58155012015-08-19 12:49:41 +00004535 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004536 return callee_method;
4537}
4538
4539void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4540 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004541
4542 switch (invoke->GetCodePtrLocation()) {
4543 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4544 __ call(GetFrameEntryLabel());
4545 break;
4546 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004547 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4548 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004549 Label* label = &relative_call_patches_.back().label;
4550 __ call(label); // Bind to the patch label, override at link time.
4551 __ Bind(label); // Bind the label at the end of the "call" insn.
4552 break;
4553 }
4554 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4555 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004556 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4557 LOG(FATAL) << "Unsupported";
4558 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004559 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4560 // (callee_method + offset_of_quick_compiled_code)()
4561 __ call(Address(callee_method.AsRegister<Register>(),
4562 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004563 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004564 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004565 }
4566
4567 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004568}
4569
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004570void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4571 Register temp = temp_in.AsRegister<Register>();
4572 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4573 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004574
4575 // Use the calling convention instead of the location of the receiver, as
4576 // intrinsics may have put the receiver in a different register. In the intrinsics
4577 // slow path, the arguments have been moved to the right place, so here we are
4578 // guaranteed that the receiver is the first register of the calling convention.
4579 InvokeDexCallingConvention calling_convention;
4580 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004581 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004582 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004583 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004584 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004585 // Instead of simply (possibly) unpoisoning `temp` here, we should
4586 // emit a read barrier for the previous class reference load.
4587 // However this is not required in practice, as this is an
4588 // intermediate/temporary reference and because the current
4589 // concurrent copying collector keeps the from-space memory
4590 // intact/accessible until the end of the marking phase (the
4591 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004592 __ MaybeUnpoisonHeapReference(temp);
4593 // temp = temp->GetMethodAt(method_offset);
4594 __ movl(temp, Address(temp, method_offset));
4595 // call temp->GetEntryPoint();
4596 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004597 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004598}
4599
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004600void CodeGeneratorX86::RecordSimplePatch() {
4601 if (GetCompilerOptions().GetIncludePatchInformation()) {
4602 simple_patches_.emplace_back();
4603 __ Bind(&simple_patches_.back());
4604 }
4605}
4606
Vladimir Markoaad75c62016-10-03 08:46:48 +00004607void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4608 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004609 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4610 __ Bind(&string_patches_.back().label);
4611}
4612
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004613void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4614 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4615 __ Bind(&type_patches_.back().label);
4616}
4617
Vladimir Markoaad75c62016-10-03 08:46:48 +00004618Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4619 DCHECK(!GetCompilerOptions().IsBootImage());
4620 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4621 return &string_patches_.back().label;
4622}
4623
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004624Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4625 uint32_t element_offset) {
4626 // Add the patch entry and bind its label at the end of the instruction.
4627 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4628 return &pc_relative_dex_cache_patches_.back().label;
4629}
4630
Vladimir Markoaad75c62016-10-03 08:46:48 +00004631// The label points to the end of the "movl" or another instruction but the literal offset
4632// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4633constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4634
4635template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4636inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
4637 const ArenaDeque<PatchInfo<Label>>& infos,
4638 ArenaVector<LinkerPatch>* linker_patches) {
4639 for (const PatchInfo<Label>& info : infos) {
4640 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4641 linker_patches->push_back(
4642 Factory(literal_offset, &info.dex_file, GetMethodAddressOffset(), info.index));
4643 }
4644}
4645
Vladimir Marko58155012015-08-19 12:49:41 +00004646void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4647 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004648 size_t size =
4649 method_patches_.size() +
4650 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004651 pc_relative_dex_cache_patches_.size() +
4652 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004653 string_patches_.size() +
4654 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004655 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004656 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004657 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004658 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004659 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004660 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004661 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004662 linker_patches->push_back(
4663 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004664 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004665 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4666 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004667 for (const Label& label : simple_patches_) {
4668 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4669 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4670 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004671 if (!GetCompilerOptions().IsBootImage()) {
4672 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4673 } else if (GetCompilerOptions().GetCompilePic()) {
4674 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004675 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004676 for (const PatchInfo<Label>& info : string_patches_) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004677 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004678 linker_patches->push_back(
4679 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004680 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004681 }
4682 if (GetCompilerOptions().GetCompilePic()) {
4683 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
4684 } else {
4685 for (const PatchInfo<Label>& info : type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004686 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004687 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004688 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004689 }
Vladimir Marko58155012015-08-19 12:49:41 +00004690}
4691
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004692void CodeGeneratorX86::MarkGCCard(Register temp,
4693 Register card,
4694 Register object,
4695 Register value,
4696 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004697 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004698 if (value_can_be_null) {
4699 __ testl(value, value);
4700 __ j(kEqual, &is_null);
4701 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004702 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004703 __ movl(temp, object);
4704 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004705 __ movb(Address(temp, card, TIMES_1, 0),
4706 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004707 if (value_can_be_null) {
4708 __ Bind(&is_null);
4709 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004710}
4711
Calin Juravle52c48962014-12-16 17:02:57 +00004712void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4713 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004714
4715 bool object_field_get_with_read_barrier =
4716 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004717 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004718 new (GetGraph()->GetArena()) LocationSummary(instruction,
4719 kEmitCompilerReadBarrier ?
4720 LocationSummary::kCallOnSlowPath :
4721 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004722 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004723 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004724 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004725 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004726
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004727 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4728 locations->SetOut(Location::RequiresFpuRegister());
4729 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004730 // The output overlaps in case of long: we don't want the low move
4731 // to overwrite the object's location. Likewise, in the case of
4732 // an object field get with read barriers enabled, we do not want
4733 // the move to overwrite the object's location, as we need it to emit
4734 // the read barrier.
4735 locations->SetOut(
4736 Location::RequiresRegister(),
4737 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4738 Location::kOutputOverlap :
4739 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004740 }
Calin Juravle52c48962014-12-16 17:02:57 +00004741
4742 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4743 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004744 // So we use an XMM register as a temp to achieve atomicity (first
4745 // load the temp into the XMM and then copy the XMM into the
4746 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004747 locations->AddTemp(Location::RequiresFpuRegister());
4748 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004749}
4750
Calin Juravle52c48962014-12-16 17:02:57 +00004751void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4752 const FieldInfo& field_info) {
4753 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004754
Calin Juravle52c48962014-12-16 17:02:57 +00004755 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004756 Location base_loc = locations->InAt(0);
4757 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004758 Location out = locations->Out();
4759 bool is_volatile = field_info.IsVolatile();
4760 Primitive::Type field_type = field_info.GetFieldType();
4761 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4762
4763 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004764 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004765 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004766 break;
4767 }
4768
4769 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004770 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004771 break;
4772 }
4773
4774 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004775 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004776 break;
4777 }
4778
4779 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004780 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004781 break;
4782 }
4783
4784 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004785 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004786 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004787
4788 case Primitive::kPrimNot: {
4789 // /* HeapReference<Object> */ out = *(base + offset)
4790 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004791 // Note that a potential implicit null check is handled in this
4792 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4793 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004794 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004795 if (is_volatile) {
4796 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4797 }
4798 } else {
4799 __ movl(out.AsRegister<Register>(), Address(base, offset));
4800 codegen_->MaybeRecordImplicitNullCheck(instruction);
4801 if (is_volatile) {
4802 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4803 }
4804 // If read barriers are enabled, emit read barriers other than
4805 // Baker's using a slow path (and also unpoison the loaded
4806 // reference, if heap poisoning is enabled).
4807 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4808 }
4809 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004810 }
4811
4812 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004813 if (is_volatile) {
4814 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4815 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004816 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004817 __ movd(out.AsRegisterPairLow<Register>(), temp);
4818 __ psrlq(temp, Immediate(32));
4819 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4820 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004821 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004822 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004823 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004824 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4825 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004826 break;
4827 }
4828
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004829 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004830 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004831 break;
4832 }
4833
4834 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004835 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004836 break;
4837 }
4838
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004839 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004840 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004841 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004842 }
Calin Juravle52c48962014-12-16 17:02:57 +00004843
Roland Levillain7c1559a2015-12-15 10:55:36 +00004844 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4845 // Potential implicit null checks, in the case of reference or
4846 // long fields, are handled in the previous switch statement.
4847 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004848 codegen_->MaybeRecordImplicitNullCheck(instruction);
4849 }
4850
Calin Juravle52c48962014-12-16 17:02:57 +00004851 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004852 if (field_type == Primitive::kPrimNot) {
4853 // Memory barriers, in the case of references, are also handled
4854 // in the previous switch statement.
4855 } else {
4856 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4857 }
Roland Levillain4d027112015-07-01 15:41:14 +01004858 }
Calin Juravle52c48962014-12-16 17:02:57 +00004859}
4860
4861void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4862 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4863
4864 LocationSummary* locations =
4865 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4866 locations->SetInAt(0, Location::RequiresRegister());
4867 bool is_volatile = field_info.IsVolatile();
4868 Primitive::Type field_type = field_info.GetFieldType();
4869 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4870 || (field_type == Primitive::kPrimByte);
4871
4872 // The register allocator does not support multiple
4873 // inputs that die at entry with one in a specific register.
4874 if (is_byte_type) {
4875 // Ensure the value is in a byte register.
4876 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004877 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004878 if (is_volatile && field_type == Primitive::kPrimDouble) {
4879 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4880 locations->SetInAt(1, Location::RequiresFpuRegister());
4881 } else {
4882 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4883 }
4884 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4885 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004886 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004887
Calin Juravle52c48962014-12-16 17:02:57 +00004888 // 64bits value can be atomically written to an address with movsd and an XMM register.
4889 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4890 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4891 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4892 // isolated cases when we need this it isn't worth adding the extra complexity.
4893 locations->AddTemp(Location::RequiresFpuRegister());
4894 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004895 } else {
4896 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4897
4898 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4899 // Temporary registers for the write barrier.
4900 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4901 // Ensure the card is in a byte register.
4902 locations->AddTemp(Location::RegisterLocation(ECX));
4903 }
Calin Juravle52c48962014-12-16 17:02:57 +00004904 }
4905}
4906
4907void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004908 const FieldInfo& field_info,
4909 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004910 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4911
4912 LocationSummary* locations = instruction->GetLocations();
4913 Register base = locations->InAt(0).AsRegister<Register>();
4914 Location value = locations->InAt(1);
4915 bool is_volatile = field_info.IsVolatile();
4916 Primitive::Type field_type = field_info.GetFieldType();
4917 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004918 bool needs_write_barrier =
4919 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004920
4921 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004922 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004923 }
4924
Mark Mendell81489372015-11-04 11:30:41 -05004925 bool maybe_record_implicit_null_check_done = false;
4926
Calin Juravle52c48962014-12-16 17:02:57 +00004927 switch (field_type) {
4928 case Primitive::kPrimBoolean:
4929 case Primitive::kPrimByte: {
4930 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4931 break;
4932 }
4933
4934 case Primitive::kPrimShort:
4935 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004936 if (value.IsConstant()) {
4937 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4938 __ movw(Address(base, offset), Immediate(v));
4939 } else {
4940 __ movw(Address(base, offset), value.AsRegister<Register>());
4941 }
Calin Juravle52c48962014-12-16 17:02:57 +00004942 break;
4943 }
4944
4945 case Primitive::kPrimInt:
4946 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004947 if (kPoisonHeapReferences && needs_write_barrier) {
4948 // Note that in the case where `value` is a null reference,
4949 // we do not enter this block, as the reference does not
4950 // need poisoning.
4951 DCHECK_EQ(field_type, Primitive::kPrimNot);
4952 Register temp = locations->GetTemp(0).AsRegister<Register>();
4953 __ movl(temp, value.AsRegister<Register>());
4954 __ PoisonHeapReference(temp);
4955 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004956 } else if (value.IsConstant()) {
4957 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4958 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004959 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004960 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004961 __ movl(Address(base, offset), value.AsRegister<Register>());
4962 }
Calin Juravle52c48962014-12-16 17:02:57 +00004963 break;
4964 }
4965
4966 case Primitive::kPrimLong: {
4967 if (is_volatile) {
4968 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4969 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4970 __ movd(temp1, value.AsRegisterPairLow<Register>());
4971 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4972 __ punpckldq(temp1, temp2);
4973 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004974 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004975 } else if (value.IsConstant()) {
4976 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4977 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4978 codegen_->MaybeRecordImplicitNullCheck(instruction);
4979 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004980 } else {
4981 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004982 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004983 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4984 }
Mark Mendell81489372015-11-04 11:30:41 -05004985 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004986 break;
4987 }
4988
4989 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004990 if (value.IsConstant()) {
4991 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4992 __ movl(Address(base, offset), Immediate(v));
4993 } else {
4994 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4995 }
Calin Juravle52c48962014-12-16 17:02:57 +00004996 break;
4997 }
4998
4999 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005000 if (value.IsConstant()) {
5001 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5002 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5003 codegen_->MaybeRecordImplicitNullCheck(instruction);
5004 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5005 maybe_record_implicit_null_check_done = true;
5006 } else {
5007 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5008 }
Calin Juravle52c48962014-12-16 17:02:57 +00005009 break;
5010 }
5011
5012 case Primitive::kPrimVoid:
5013 LOG(FATAL) << "Unreachable type " << field_type;
5014 UNREACHABLE();
5015 }
5016
Mark Mendell81489372015-11-04 11:30:41 -05005017 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005018 codegen_->MaybeRecordImplicitNullCheck(instruction);
5019 }
5020
Roland Levillain4d027112015-07-01 15:41:14 +01005021 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005022 Register temp = locations->GetTemp(0).AsRegister<Register>();
5023 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005024 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005025 }
5026
Calin Juravle52c48962014-12-16 17:02:57 +00005027 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005028 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005029 }
5030}
5031
5032void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5033 HandleFieldGet(instruction, instruction->GetFieldInfo());
5034}
5035
5036void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5037 HandleFieldGet(instruction, instruction->GetFieldInfo());
5038}
5039
5040void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5041 HandleFieldSet(instruction, instruction->GetFieldInfo());
5042}
5043
5044void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005045 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005046}
5047
5048void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5049 HandleFieldSet(instruction, instruction->GetFieldInfo());
5050}
5051
5052void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005053 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005054}
5055
5056void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5057 HandleFieldGet(instruction, instruction->GetFieldInfo());
5058}
5059
5060void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5061 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005062}
5063
Calin Juravlee460d1d2015-09-29 04:52:17 +01005064void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5065 HUnresolvedInstanceFieldGet* instruction) {
5066 FieldAccessCallingConventionX86 calling_convention;
5067 codegen_->CreateUnresolvedFieldLocationSummary(
5068 instruction, instruction->GetFieldType(), calling_convention);
5069}
5070
5071void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5072 HUnresolvedInstanceFieldGet* instruction) {
5073 FieldAccessCallingConventionX86 calling_convention;
5074 codegen_->GenerateUnresolvedFieldAccess(instruction,
5075 instruction->GetFieldType(),
5076 instruction->GetFieldIndex(),
5077 instruction->GetDexPc(),
5078 calling_convention);
5079}
5080
5081void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5082 HUnresolvedInstanceFieldSet* instruction) {
5083 FieldAccessCallingConventionX86 calling_convention;
5084 codegen_->CreateUnresolvedFieldLocationSummary(
5085 instruction, instruction->GetFieldType(), calling_convention);
5086}
5087
5088void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5089 HUnresolvedInstanceFieldSet* instruction) {
5090 FieldAccessCallingConventionX86 calling_convention;
5091 codegen_->GenerateUnresolvedFieldAccess(instruction,
5092 instruction->GetFieldType(),
5093 instruction->GetFieldIndex(),
5094 instruction->GetDexPc(),
5095 calling_convention);
5096}
5097
5098void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5099 HUnresolvedStaticFieldGet* instruction) {
5100 FieldAccessCallingConventionX86 calling_convention;
5101 codegen_->CreateUnresolvedFieldLocationSummary(
5102 instruction, instruction->GetFieldType(), calling_convention);
5103}
5104
5105void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5106 HUnresolvedStaticFieldGet* instruction) {
5107 FieldAccessCallingConventionX86 calling_convention;
5108 codegen_->GenerateUnresolvedFieldAccess(instruction,
5109 instruction->GetFieldType(),
5110 instruction->GetFieldIndex(),
5111 instruction->GetDexPc(),
5112 calling_convention);
5113}
5114
5115void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5116 HUnresolvedStaticFieldSet* instruction) {
5117 FieldAccessCallingConventionX86 calling_convention;
5118 codegen_->CreateUnresolvedFieldLocationSummary(
5119 instruction, instruction->GetFieldType(), calling_convention);
5120}
5121
5122void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5123 HUnresolvedStaticFieldSet* instruction) {
5124 FieldAccessCallingConventionX86 calling_convention;
5125 codegen_->GenerateUnresolvedFieldAccess(instruction,
5126 instruction->GetFieldType(),
5127 instruction->GetFieldIndex(),
5128 instruction->GetDexPc(),
5129 calling_convention);
5130}
5131
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005132void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005133 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5134 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5135 ? Location::RequiresRegister()
5136 : Location::Any();
5137 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005138}
5139
Calin Juravle2ae48182016-03-16 14:05:09 +00005140void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5141 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005142 return;
5143 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005144 LocationSummary* locations = instruction->GetLocations();
5145 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005146
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005147 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005148 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005149}
5150
Calin Juravle2ae48182016-03-16 14:05:09 +00005151void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005152 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005153 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005154
5155 LocationSummary* locations = instruction->GetLocations();
5156 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005157
5158 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005159 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005160 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005161 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005162 } else {
5163 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005164 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005165 __ jmp(slow_path->GetEntryLabel());
5166 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005167 }
5168 __ j(kEqual, slow_path->GetEntryLabel());
5169}
5170
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005171void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005172 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005173}
5174
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005175void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005176 bool object_array_get_with_read_barrier =
5177 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005178 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005179 new (GetGraph()->GetArena()) LocationSummary(instruction,
5180 object_array_get_with_read_barrier ?
5181 LocationSummary::kCallOnSlowPath :
5182 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005183 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005184 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005185 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005186 locations->SetInAt(0, Location::RequiresRegister());
5187 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005188 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5189 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5190 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005191 // The output overlaps in case of long: we don't want the low move
5192 // to overwrite the array's location. Likewise, in the case of an
5193 // object array get with read barriers enabled, we do not want the
5194 // move to overwrite the array's location, as we need it to emit
5195 // the read barrier.
5196 locations->SetOut(
5197 Location::RequiresRegister(),
5198 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5199 Location::kOutputOverlap :
5200 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005201 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005202}
5203
5204void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5205 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005206 Location obj_loc = locations->InAt(0);
5207 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005208 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005209 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005210 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005211
Calin Juravle77520bc2015-01-12 18:45:46 +00005212 Primitive::Type type = instruction->GetType();
5213 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005214 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005215 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005216 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005217 break;
5218 }
5219
5220 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005221 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005222 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005223 break;
5224 }
5225
5226 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005227 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005228 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005229 break;
5230 }
5231
5232 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005233 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005234 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5235 // Branch cases into compressed and uncompressed for each index's type.
5236 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5237 NearLabel done, not_compressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005238 __ testl(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005239 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005240 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5241 "Expecting 0=compressed, 1=uncompressed");
5242 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005243 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5244 __ jmp(&done);
5245 __ Bind(&not_compressed);
5246 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5247 __ Bind(&done);
5248 } else {
5249 // Common case for charAt of array of char or when string compression's
5250 // feature is turned off.
5251 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5252 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005253 break;
5254 }
5255
Roland Levillain7c1559a2015-12-15 10:55:36 +00005256 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005257 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005258 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259 break;
5260 }
5261
Roland Levillain7c1559a2015-12-15 10:55:36 +00005262 case Primitive::kPrimNot: {
5263 static_assert(
5264 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5265 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005266 // /* HeapReference<Object> */ out =
5267 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5268 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005269 // Note that a potential implicit null check is handled in this
5270 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5271 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005272 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005273 } else {
5274 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005275 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5276 codegen_->MaybeRecordImplicitNullCheck(instruction);
5277 // If read barriers are enabled, emit read barriers other than
5278 // Baker's using a slow path (and also unpoison the loaded
5279 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005280 if (index.IsConstant()) {
5281 uint32_t offset =
5282 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005283 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5284 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005285 codegen_->MaybeGenerateReadBarrierSlow(
5286 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5287 }
5288 }
5289 break;
5290 }
5291
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005292 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005293 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005294 __ movl(out_loc.AsRegisterPairLow<Register>(),
5295 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5296 codegen_->MaybeRecordImplicitNullCheck(instruction);
5297 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5298 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 break;
5300 }
5301
Mark Mendell7c8d0092015-01-26 11:21:33 -05005302 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005303 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005304 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005305 break;
5306 }
5307
5308 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005309 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005310 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005311 break;
5312 }
5313
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005314 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005315 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005316 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005318
Roland Levillain7c1559a2015-12-15 10:55:36 +00005319 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5320 // Potential implicit null checks, in the case of reference or
5321 // long arrays, are handled in the previous switch statement.
5322 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005323 codegen_->MaybeRecordImplicitNullCheck(instruction);
5324 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005325}
5326
5327void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005328 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005329
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005330 bool needs_write_barrier =
5331 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005332 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005333
Nicolas Geoffray39468442014-09-02 15:17:15 +01005334 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5335 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005336 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005337 LocationSummary::kCallOnSlowPath :
5338 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005339
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005340 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5341 || (value_type == Primitive::kPrimByte);
5342 // We need the inputs to be different than the output in case of long operation.
5343 // In case of a byte operation, the register allocator does not support multiple
5344 // inputs that die at entry with one in a specific register.
5345 locations->SetInAt(0, Location::RequiresRegister());
5346 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5347 if (is_byte_type) {
5348 // Ensure the value is in a byte register.
5349 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5350 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005351 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005352 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005353 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5354 }
5355 if (needs_write_barrier) {
5356 // Temporary registers for the write barrier.
5357 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5358 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005359 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005360 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361}
5362
5363void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5364 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005365 Location array_loc = locations->InAt(0);
5366 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005367 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005368 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005369 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005370 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5371 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5372 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005373 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005374 bool needs_write_barrier =
5375 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005376
5377 switch (value_type) {
5378 case Primitive::kPrimBoolean:
5379 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005380 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005381 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005382 if (value.IsRegister()) {
5383 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005384 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005385 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005387 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005388 break;
5389 }
5390
5391 case Primitive::kPrimShort:
5392 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005393 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005394 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005395 if (value.IsRegister()) {
5396 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005397 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005398 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005399 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005400 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005401 break;
5402 }
5403
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005404 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005405 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005406 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005407
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005408 if (!value.IsRegister()) {
5409 // Just setting null.
5410 DCHECK(instruction->InputAt(2)->IsNullConstant());
5411 DCHECK(value.IsConstant()) << value;
5412 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005413 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005414 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005415 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005416 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005417 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005418
5419 DCHECK(needs_write_barrier);
5420 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005421 // We cannot use a NearLabel for `done`, as its range may be too
5422 // short when Baker read barriers are enabled.
5423 Label done;
5424 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005425 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005426 Location temp_loc = locations->GetTemp(0);
5427 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005428 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005429 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5430 codegen_->AddSlowPath(slow_path);
5431 if (instruction->GetValueCanBeNull()) {
5432 __ testl(register_value, register_value);
5433 __ j(kNotEqual, &not_null);
5434 __ movl(address, Immediate(0));
5435 codegen_->MaybeRecordImplicitNullCheck(instruction);
5436 __ jmp(&done);
5437 __ Bind(&not_null);
5438 }
5439
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005440 // Note that when Baker read barriers are enabled, the type
5441 // checks are performed without read barriers. This is fine,
5442 // even in the case where a class object is in the from-space
5443 // after the flip, as a comparison involving such a type would
5444 // not produce a false positive; it may of course produce a
5445 // false negative, in which case we would take the ArraySet
5446 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005447
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005448 // /* HeapReference<Class> */ temp = array->klass_
5449 __ movl(temp, Address(array, class_offset));
5450 codegen_->MaybeRecordImplicitNullCheck(instruction);
5451 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005452
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005453 // /* HeapReference<Class> */ temp = temp->component_type_
5454 __ movl(temp, Address(temp, component_offset));
5455 // If heap poisoning is enabled, no need to unpoison `temp`
5456 // nor the object reference in `register_value->klass`, as
5457 // we are comparing two poisoned references.
5458 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005459
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005460 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5461 __ j(kEqual, &do_put);
5462 // If heap poisoning is enabled, the `temp` reference has
5463 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005464 __ MaybeUnpoisonHeapReference(temp);
5465
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005466 // If heap poisoning is enabled, no need to unpoison the
5467 // heap reference loaded below, as it is only used for a
5468 // comparison with null.
5469 __ cmpl(Address(temp, super_offset), Immediate(0));
5470 __ j(kNotEqual, slow_path->GetEntryLabel());
5471 __ Bind(&do_put);
5472 } else {
5473 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005474 }
5475 }
5476
5477 if (kPoisonHeapReferences) {
5478 __ movl(temp, register_value);
5479 __ PoisonHeapReference(temp);
5480 __ movl(address, temp);
5481 } else {
5482 __ movl(address, register_value);
5483 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005484 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005485 codegen_->MaybeRecordImplicitNullCheck(instruction);
5486 }
5487
5488 Register card = locations->GetTemp(1).AsRegister<Register>();
5489 codegen_->MarkGCCard(
5490 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5491 __ Bind(&done);
5492
5493 if (slow_path != nullptr) {
5494 __ Bind(slow_path->GetExitLabel());
5495 }
5496
5497 break;
5498 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005499
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005500 case Primitive::kPrimInt: {
5501 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005502 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005503 if (value.IsRegister()) {
5504 __ movl(address, value.AsRegister<Register>());
5505 } else {
5506 DCHECK(value.IsConstant()) << value;
5507 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5508 __ movl(address, Immediate(v));
5509 }
5510 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005511 break;
5512 }
5513
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005514 case Primitive::kPrimLong: {
5515 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005516 if (value.IsRegisterPair()) {
5517 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5518 value.AsRegisterPairLow<Register>());
5519 codegen_->MaybeRecordImplicitNullCheck(instruction);
5520 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5521 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005522 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005523 DCHECK(value.IsConstant());
5524 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5525 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5526 Immediate(Low32Bits(val)));
5527 codegen_->MaybeRecordImplicitNullCheck(instruction);
5528 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5529 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005530 }
5531 break;
5532 }
5533
Mark Mendell7c8d0092015-01-26 11:21:33 -05005534 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005535 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005536 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005537 if (value.IsFpuRegister()) {
5538 __ movss(address, value.AsFpuRegister<XmmRegister>());
5539 } else {
5540 DCHECK(value.IsConstant());
5541 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5542 __ movl(address, Immediate(v));
5543 }
5544 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005545 break;
5546 }
5547
5548 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005549 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005550 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005551 if (value.IsFpuRegister()) {
5552 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5553 } else {
5554 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005555 Address address_hi =
5556 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005557 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5558 __ movl(address, Immediate(Low32Bits(v)));
5559 codegen_->MaybeRecordImplicitNullCheck(instruction);
5560 __ movl(address_hi, Immediate(High32Bits(v)));
5561 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005562 break;
5563 }
5564
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005565 case Primitive::kPrimVoid:
5566 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005567 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005568 }
5569}
5570
5571void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5572 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005573 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005574 if (!instruction->IsEmittedAtUseSite()) {
5575 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5576 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005577}
5578
5579void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005580 if (instruction->IsEmittedAtUseSite()) {
5581 return;
5582 }
5583
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005584 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005585 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005586 Register obj = locations->InAt(0).AsRegister<Register>();
5587 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005588 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005589 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005590 // Mask out most significant bit in case the array is String's array of char.
5591 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005592 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005593 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005594}
5595
5596void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005597 RegisterSet caller_saves = RegisterSet::Empty();
5598 InvokeRuntimeCallingConvention calling_convention;
5599 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5600 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5601 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005602 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005603 HInstruction* length = instruction->InputAt(1);
5604 if (!length->IsEmittedAtUseSite()) {
5605 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5606 }
jessicahandojo4877b792016-09-08 19:49:13 -07005607 // Need register to see array's length.
5608 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5609 locations->AddTemp(Location::RequiresRegister());
5610 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005611}
5612
5613void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005614 const bool is_string_compressed_char_at =
5615 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005616 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005617 Location index_loc = locations->InAt(0);
5618 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005619 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005620 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005621
Mark Mendell99dbd682015-04-22 16:18:52 -04005622 if (length_loc.IsConstant()) {
5623 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5624 if (index_loc.IsConstant()) {
5625 // BCE will remove the bounds check if we are guarenteed to pass.
5626 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5627 if (index < 0 || index >= length) {
5628 codegen_->AddSlowPath(slow_path);
5629 __ jmp(slow_path->GetEntryLabel());
5630 } else {
5631 // Some optimization after BCE may have generated this, and we should not
5632 // generate a bounds check if it is a valid range.
5633 }
5634 return;
5635 }
5636
5637 // We have to reverse the jump condition because the length is the constant.
5638 Register index_reg = index_loc.AsRegister<Register>();
5639 __ cmpl(index_reg, Immediate(length));
5640 codegen_->AddSlowPath(slow_path);
5641 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005642 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005643 HInstruction* array_length = instruction->InputAt(1);
5644 if (array_length->IsEmittedAtUseSite()) {
5645 // Address the length field in the array.
5646 DCHECK(array_length->IsArrayLength());
5647 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5648 Location array_loc = array_length->GetLocations()->InAt(0);
5649 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005650 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005651 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5652 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005653 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5654 __ movl(length_reg, array_len);
5655 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005656 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005657 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005658 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005659 // Checking bounds for general case:
5660 // Array of char or string's array with feature compression off.
5661 if (index_loc.IsConstant()) {
5662 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5663 __ cmpl(array_len, Immediate(value));
5664 } else {
5665 __ cmpl(array_len, index_loc.AsRegister<Register>());
5666 }
5667 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005668 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005669 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005670 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005671 }
5672 codegen_->AddSlowPath(slow_path);
5673 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005674 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005675}
5676
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005677void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005678 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005679}
5680
5681void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005682 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5683}
5684
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005685void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005686 LocationSummary* locations =
5687 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005688 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005689}
5690
5691void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005692 HBasicBlock* block = instruction->GetBlock();
5693 if (block->GetLoopInformation() != nullptr) {
5694 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5695 // The back edge will generate the suspend check.
5696 return;
5697 }
5698 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5699 // The goto will generate the suspend check.
5700 return;
5701 }
5702 GenerateSuspendCheck(instruction, nullptr);
5703}
5704
5705void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5706 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005707 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005708 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5709 if (slow_path == nullptr) {
5710 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5711 instruction->SetSlowPath(slow_path);
5712 codegen_->AddSlowPath(slow_path);
5713 if (successor != nullptr) {
5714 DCHECK(successor->IsLoopHeader());
5715 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5716 }
5717 } else {
5718 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5719 }
5720
Andreas Gampe542451c2016-07-26 09:02:02 -07005721 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005722 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005723 if (successor == nullptr) {
5724 __ j(kNotEqual, slow_path->GetEntryLabel());
5725 __ Bind(slow_path->GetReturnLabel());
5726 } else {
5727 __ j(kEqual, codegen_->GetLabelOf(successor));
5728 __ jmp(slow_path->GetEntryLabel());
5729 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005730}
5731
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005732X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5733 return codegen_->GetAssembler();
5734}
5735
Mark Mendell7c8d0092015-01-26 11:21:33 -05005736void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005737 ScratchRegisterScope ensure_scratch(
5738 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5739 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5740 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5741 __ movl(temp_reg, Address(ESP, src + stack_offset));
5742 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005743}
5744
5745void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005746 ScratchRegisterScope ensure_scratch(
5747 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5748 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5749 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5750 __ movl(temp_reg, Address(ESP, src + stack_offset));
5751 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5752 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5753 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005754}
5755
5756void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005757 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005758 Location source = move->GetSource();
5759 Location destination = move->GetDestination();
5760
5761 if (source.IsRegister()) {
5762 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005763 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005764 } else if (destination.IsFpuRegister()) {
5765 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005766 } else {
5767 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005768 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005769 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005770 } else if (source.IsRegisterPair()) {
5771 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5772 // Create stack space for 2 elements.
5773 __ subl(ESP, Immediate(2 * elem_size));
5774 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5775 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5776 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5777 // And remove the temporary stack space we allocated.
5778 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005779 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005780 if (destination.IsRegister()) {
5781 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5782 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005783 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005784 } else if (destination.IsRegisterPair()) {
5785 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5786 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5787 __ psrlq(src_reg, Immediate(32));
5788 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005789 } else if (destination.IsStackSlot()) {
5790 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5791 } else {
5792 DCHECK(destination.IsDoubleStackSlot());
5793 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5794 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005795 } else if (source.IsStackSlot()) {
5796 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005797 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005798 } else if (destination.IsFpuRegister()) {
5799 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005800 } else {
5801 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005802 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5803 }
5804 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005805 if (destination.IsRegisterPair()) {
5806 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5807 __ movl(destination.AsRegisterPairHigh<Register>(),
5808 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5809 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005810 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5811 } else {
5812 DCHECK(destination.IsDoubleStackSlot()) << destination;
5813 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005814 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005815 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005816 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005817 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005818 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005819 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005820 if (value == 0) {
5821 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5822 } else {
5823 __ movl(destination.AsRegister<Register>(), Immediate(value));
5824 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005825 } else {
5826 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005827 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005828 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005829 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005830 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005831 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005832 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005833 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005834 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5835 if (value == 0) {
5836 // Easy handling of 0.0.
5837 __ xorps(dest, dest);
5838 } else {
5839 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005840 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5841 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5842 __ movl(temp, Immediate(value));
5843 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005844 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005845 } else {
5846 DCHECK(destination.IsStackSlot()) << destination;
5847 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5848 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005849 } else if (constant->IsLongConstant()) {
5850 int64_t value = constant->AsLongConstant()->GetValue();
5851 int32_t low_value = Low32Bits(value);
5852 int32_t high_value = High32Bits(value);
5853 Immediate low(low_value);
5854 Immediate high(high_value);
5855 if (destination.IsDoubleStackSlot()) {
5856 __ movl(Address(ESP, destination.GetStackIndex()), low);
5857 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5858 } else {
5859 __ movl(destination.AsRegisterPairLow<Register>(), low);
5860 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5861 }
5862 } else {
5863 DCHECK(constant->IsDoubleConstant());
5864 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005865 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005866 int32_t low_value = Low32Bits(value);
5867 int32_t high_value = High32Bits(value);
5868 Immediate low(low_value);
5869 Immediate high(high_value);
5870 if (destination.IsFpuRegister()) {
5871 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5872 if (value == 0) {
5873 // Easy handling of 0.0.
5874 __ xorpd(dest, dest);
5875 } else {
5876 __ pushl(high);
5877 __ pushl(low);
5878 __ movsd(dest, Address(ESP, 0));
5879 __ addl(ESP, Immediate(8));
5880 }
5881 } else {
5882 DCHECK(destination.IsDoubleStackSlot()) << destination;
5883 __ movl(Address(ESP, destination.GetStackIndex()), low);
5884 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5885 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005886 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005887 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005888 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005889 }
5890}
5891
Mark Mendella5c19ce2015-04-01 12:51:05 -04005892void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005893 Register suggested_scratch = reg == EAX ? EBX : EAX;
5894 ScratchRegisterScope ensure_scratch(
5895 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5896
5897 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5898 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5899 __ movl(Address(ESP, mem + stack_offset), reg);
5900 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005901}
5902
Mark Mendell7c8d0092015-01-26 11:21:33 -05005903void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005904 ScratchRegisterScope ensure_scratch(
5905 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5906
5907 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5908 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5909 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5910 __ movss(Address(ESP, mem + stack_offset), reg);
5911 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005912}
5913
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005914void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005915 ScratchRegisterScope ensure_scratch1(
5916 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005917
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005918 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5919 ScratchRegisterScope ensure_scratch2(
5920 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005921
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005922 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5923 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5924 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5925 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5926 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5927 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005928}
5929
5930void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005931 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005932 Location source = move->GetSource();
5933 Location destination = move->GetDestination();
5934
5935 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005936 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5937 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5938 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5939 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5940 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005941 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005942 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005943 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005944 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005945 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5946 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005947 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5948 // Use XOR Swap algorithm to avoid a temporary.
5949 DCHECK_NE(source.reg(), destination.reg());
5950 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5951 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5952 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5953 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5954 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5955 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5956 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005957 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5958 // Take advantage of the 16 bytes in the XMM register.
5959 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5960 Address stack(ESP, destination.GetStackIndex());
5961 // Load the double into the high doubleword.
5962 __ movhpd(reg, stack);
5963
5964 // Store the low double into the destination.
5965 __ movsd(stack, reg);
5966
5967 // Move the high double to the low double.
5968 __ psrldq(reg, Immediate(8));
5969 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5970 // Take advantage of the 16 bytes in the XMM register.
5971 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5972 Address stack(ESP, source.GetStackIndex());
5973 // Load the double into the high doubleword.
5974 __ movhpd(reg, stack);
5975
5976 // Store the low double into the destination.
5977 __ movsd(stack, reg);
5978
5979 // Move the high double to the low double.
5980 __ psrldq(reg, Immediate(8));
5981 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5982 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5983 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005984 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005985 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005986 }
5987}
5988
5989void ParallelMoveResolverX86::SpillScratch(int reg) {
5990 __ pushl(static_cast<Register>(reg));
5991}
5992
5993void ParallelMoveResolverX86::RestoreScratch(int reg) {
5994 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005995}
5996
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005997HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5998 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005999 switch (desired_class_load_kind) {
6000 case HLoadClass::LoadKind::kReferrersClass:
6001 break;
6002 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6003 DCHECK(!GetCompilerOptions().GetCompilePic());
6004 break;
6005 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6006 DCHECK(GetCompilerOptions().GetCompilePic());
6007 FALLTHROUGH_INTENDED;
6008 case HLoadClass::LoadKind::kDexCachePcRelative:
6009 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
6010 // We disable pc-relative load when there is an irreducible loop, as the optimization
6011 // is incompatible with it.
6012 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6013 // with irreducible loops.
6014 if (GetGraph()->HasIrreducibleLoops()) {
6015 return HLoadClass::LoadKind::kDexCacheViaMethod;
6016 }
6017 break;
6018 case HLoadClass::LoadKind::kBootImageAddress:
6019 break;
6020 case HLoadClass::LoadKind::kDexCacheAddress:
6021 DCHECK(Runtime::Current()->UseJitCompilation());
6022 break;
6023 case HLoadClass::LoadKind::kDexCacheViaMethod:
6024 break;
6025 }
6026 return desired_class_load_kind;
6027}
6028
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006029void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006030 if (cls->NeedsAccessCheck()) {
6031 InvokeRuntimeCallingConvention calling_convention;
6032 CodeGenerator::CreateLoadClassLocationSummary(
6033 cls,
6034 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6035 Location::RegisterLocation(EAX),
6036 /* code_generator_supports_read_barrier */ true);
6037 return;
6038 }
6039
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006040 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6041 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006042 ? LocationSummary::kCallOnSlowPath
6043 : LocationSummary::kNoCall;
6044 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006045 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006046 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006047 }
6048
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006049 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6050 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6051 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6052 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6053 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6054 locations->SetInAt(0, Location::RequiresRegister());
6055 }
6056 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006057}
6058
6059void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006060 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006061 if (cls->NeedsAccessCheck()) {
6062 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01006063 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006064 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006065 return;
6066 }
6067
Roland Levillain0d5a2812015-11-13 10:07:31 +00006068 Location out_loc = locations->Out();
6069 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006070
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006071 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006072 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6073 ? kWithoutReadBarrier
6074 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006075 switch (cls->GetLoadKind()) {
6076 case HLoadClass::LoadKind::kReferrersClass: {
6077 DCHECK(!cls->CanCallRuntime());
6078 DCHECK(!cls->MustGenerateClinitCheck());
6079 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6080 Register current_method = locations->InAt(0).AsRegister<Register>();
6081 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006082 cls,
6083 out_loc,
6084 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006085 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006086 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006087 break;
6088 }
6089 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006090 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006091 __ movl(out, Immediate(/* placeholder */ 0));
6092 codegen_->RecordTypePatch(cls);
6093 break;
6094 }
6095 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006096 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006097 Register method_address = locations->InAt(0).AsRegister<Register>();
6098 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6099 codegen_->RecordTypePatch(cls);
6100 break;
6101 }
6102 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006103 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006104 DCHECK_NE(cls->GetAddress(), 0u);
6105 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6106 __ movl(out, Immediate(address));
6107 codegen_->RecordSimplePatch();
6108 break;
6109 }
6110 case HLoadClass::LoadKind::kDexCacheAddress: {
6111 DCHECK_NE(cls->GetAddress(), 0u);
6112 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6113 // /* GcRoot<mirror::Class> */ out = *address
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006114 GenerateGcRootFieldLoad(cls,
6115 out_loc,
6116 Address::Absolute(address),
Roland Levillain00468f32016-10-27 18:02:48 +01006117 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006118 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006119 generate_null_check = !cls->IsInDexCache();
6120 break;
6121 }
6122 case HLoadClass::LoadKind::kDexCachePcRelative: {
6123 Register base_reg = locations->InAt(0).AsRegister<Register>();
6124 uint32_t offset = cls->GetDexCacheElementOffset();
6125 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6126 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006127 GenerateGcRootFieldLoad(cls,
6128 out_loc,
6129 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
6130 fixup_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006131 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006132 generate_null_check = !cls->IsInDexCache();
6133 break;
6134 }
6135 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6136 // /* GcRoot<mirror::Class>[] */ out =
6137 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6138 Register current_method = locations->InAt(0).AsRegister<Register>();
6139 __ movl(out, Address(current_method,
6140 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6141 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006142 GenerateGcRootFieldLoad(cls,
6143 out_loc,
6144 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
Roland Levillain00468f32016-10-27 18:02:48 +01006145 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006146 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006147 generate_null_check = !cls->IsInDexCache();
6148 break;
6149 }
6150 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006151
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006152 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6153 DCHECK(cls->CanCallRuntime());
6154 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6155 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6156 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006157
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006158 if (generate_null_check) {
6159 __ testl(out, out);
6160 __ j(kEqual, slow_path->GetEntryLabel());
6161 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006162
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006163 if (cls->MustGenerateClinitCheck()) {
6164 GenerateClassInitializationCheck(slow_path, out);
6165 } else {
6166 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006167 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006168 }
6169}
6170
6171void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6172 LocationSummary* locations =
6173 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6174 locations->SetInAt(0, Location::RequiresRegister());
6175 if (check->HasUses()) {
6176 locations->SetOut(Location::SameAsFirstInput());
6177 }
6178}
6179
6180void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006181 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006182 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006183 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006184 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006185 GenerateClassInitializationCheck(slow_path,
6186 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006187}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006188
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006189void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006190 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006191 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6192 Immediate(mirror::Class::kStatusInitialized));
6193 __ j(kLess, slow_path->GetEntryLabel());
6194 __ Bind(slow_path->GetExitLabel());
6195 // No need for memory fence, thanks to the X86 memory model.
6196}
6197
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006198HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6199 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006200 switch (desired_string_load_kind) {
6201 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6202 DCHECK(!GetCompilerOptions().GetCompilePic());
6203 break;
6204 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6205 DCHECK(GetCompilerOptions().GetCompilePic());
6206 FALLTHROUGH_INTENDED;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006207 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006208 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006209 // We disable pc-relative load when there is an irreducible loop, as the optimization
6210 // is incompatible with it.
6211 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6212 // with irreducible loops.
6213 if (GetGraph()->HasIrreducibleLoops()) {
6214 return HLoadString::LoadKind::kDexCacheViaMethod;
6215 }
6216 break;
6217 case HLoadString::LoadKind::kBootImageAddress:
6218 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006219 case HLoadString::LoadKind::kDexCacheViaMethod:
6220 break;
6221 }
6222 return desired_string_load_kind;
6223}
6224
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006225void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00006226 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
6227 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
6228 ? LocationSummary::kCallOnMainOnly
6229 : LocationSummary::kCallOnSlowPath)
6230 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006231 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006232 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006233 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006234 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006235 locations->SetInAt(0, Location::RequiresRegister());
6236 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006237 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6238 locations->SetOut(Location::RegisterLocation(EAX));
6239 } else {
6240 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006241 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6242 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6243 // Rely on the pResolveString and/or marking to save everything.
6244 RegisterSet caller_saves = RegisterSet::Empty();
6245 InvokeRuntimeCallingConvention calling_convention;
6246 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6247 locations->SetCustomSlowPathCallerSaves(caller_saves);
6248 } else {
6249 // For non-Baker read barrier we have a temp-clobbering call.
6250 }
6251 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006252 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006253}
6254
6255void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006256 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006257 Location out_loc = locations->Out();
6258 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006259
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006260 switch (load->GetLoadKind()) {
6261 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006262 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006263 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006264 return; // No dex cache slow path.
6265 }
6266 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006267 Register method_address = locations->InAt(0).AsRegister<Register>();
6268 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006269 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006270 return; // No dex cache slow path.
6271 }
6272 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006273 DCHECK_NE(load->GetAddress(), 0u);
6274 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6275 __ movl(out, Immediate(address));
6276 codegen_->RecordSimplePatch();
6277 return; // No dex cache slow path.
6278 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006279 case HLoadString::LoadKind::kBssEntry: {
6280 Register method_address = locations->InAt(0).AsRegister<Register>();
6281 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6282 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray3395fbc2016-11-14 12:40:52 +00006283 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006284 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006285 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6286 codegen_->AddSlowPath(slow_path);
6287 __ testl(out, out);
6288 __ j(kEqual, slow_path->GetEntryLabel());
6289 __ Bind(slow_path->GetExitLabel());
6290 return;
6291 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006292 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006293 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006294 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006295
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006296 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006297 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006298 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006299 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex()));
6300 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6301 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006302}
6303
David Brazdilcb1c0552015-08-04 16:22:25 +01006304static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006305 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006306}
6307
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006308void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6309 LocationSummary* locations =
6310 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6311 locations->SetOut(Location::RequiresRegister());
6312}
6313
6314void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006315 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6316}
6317
6318void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6319 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6320}
6321
6322void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6323 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006324}
6325
6326void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6327 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006328 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006329 InvokeRuntimeCallingConvention calling_convention;
6330 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6331}
6332
6333void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006334 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006335 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006336}
6337
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006338// Temp is used for read barrier.
6339static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6340 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006341 !kUseBakerReadBarrier &&
6342 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006343 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006344 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6345 return 1;
6346 }
6347 return 0;
6348}
6349
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006350// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006351// interface pointer, one for loading the current interface.
6352// The other checks have one temp for loading the object's class.
6353static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6354 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6355 return 2;
6356 }
6357 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006358}
6359
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006360void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006361 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006362 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006363 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006364 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006365 case TypeCheckKind::kExactCheck:
6366 case TypeCheckKind::kAbstractClassCheck:
6367 case TypeCheckKind::kClassHierarchyCheck:
6368 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006369 call_kind =
6370 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006371 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006372 break;
6373 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006374 case TypeCheckKind::kUnresolvedCheck:
6375 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006376 call_kind = LocationSummary::kCallOnSlowPath;
6377 break;
6378 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006379
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006380 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006381 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006382 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006383 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006384 locations->SetInAt(0, Location::RequiresRegister());
6385 locations->SetInAt(1, Location::Any());
6386 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6387 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006388 // When read barriers are enabled, we need a temporary register for some cases.
6389 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006390}
6391
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006392void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006393 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006394 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006395 Location obj_loc = locations->InAt(0);
6396 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006397 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006398 Location out_loc = locations->Out();
6399 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006400 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6401 DCHECK_LE(num_temps, 1u);
6402 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006403 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006404 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6405 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6406 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006407 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006408 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006409
6410 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006411 // Avoid null check if we know obj is not null.
6412 if (instruction->MustDoNullCheck()) {
6413 __ testl(obj, obj);
6414 __ j(kEqual, &zero);
6415 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006416
Roland Levillain7c1559a2015-12-15 10:55:36 +00006417 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006418 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006419 // /* HeapReference<Class> */ out = obj->klass_
6420 GenerateReferenceLoadTwoRegisters(instruction,
6421 out_loc,
6422 obj_loc,
6423 class_offset,
6424 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006425 if (cls.IsRegister()) {
6426 __ cmpl(out, cls.AsRegister<Register>());
6427 } else {
6428 DCHECK(cls.IsStackSlot()) << cls;
6429 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6430 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006431
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006432 // Classes must be equal for the instanceof to succeed.
6433 __ j(kNotEqual, &zero);
6434 __ movl(out, Immediate(1));
6435 __ jmp(&done);
6436 break;
6437 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006438
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006439 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006440 // /* HeapReference<Class> */ out = obj->klass_
6441 GenerateReferenceLoadTwoRegisters(instruction,
6442 out_loc,
6443 obj_loc,
6444 class_offset,
6445 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006446 // If the class is abstract, we eagerly fetch the super class of the
6447 // object to avoid doing a comparison we know will fail.
6448 NearLabel loop;
6449 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006450 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006451 GenerateReferenceLoadOneRegister(instruction,
6452 out_loc,
6453 super_offset,
6454 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006455 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006456 __ testl(out, out);
6457 // If `out` is null, we use it for the result, and jump to `done`.
6458 __ j(kEqual, &done);
6459 if (cls.IsRegister()) {
6460 __ cmpl(out, cls.AsRegister<Register>());
6461 } else {
6462 DCHECK(cls.IsStackSlot()) << cls;
6463 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6464 }
6465 __ j(kNotEqual, &loop);
6466 __ movl(out, Immediate(1));
6467 if (zero.IsLinked()) {
6468 __ jmp(&done);
6469 }
6470 break;
6471 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006472
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006473 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006474 // /* HeapReference<Class> */ out = obj->klass_
6475 GenerateReferenceLoadTwoRegisters(instruction,
6476 out_loc,
6477 obj_loc,
6478 class_offset,
6479 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006480 // Walk over the class hierarchy to find a match.
6481 NearLabel loop, success;
6482 __ Bind(&loop);
6483 if (cls.IsRegister()) {
6484 __ cmpl(out, cls.AsRegister<Register>());
6485 } else {
6486 DCHECK(cls.IsStackSlot()) << cls;
6487 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6488 }
6489 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006490 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006491 GenerateReferenceLoadOneRegister(instruction,
6492 out_loc,
6493 super_offset,
6494 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006495 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006496 __ testl(out, out);
6497 __ j(kNotEqual, &loop);
6498 // If `out` is null, we use it for the result, and jump to `done`.
6499 __ jmp(&done);
6500 __ Bind(&success);
6501 __ movl(out, Immediate(1));
6502 if (zero.IsLinked()) {
6503 __ jmp(&done);
6504 }
6505 break;
6506 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006507
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006508 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006509 // /* HeapReference<Class> */ out = obj->klass_
6510 GenerateReferenceLoadTwoRegisters(instruction,
6511 out_loc,
6512 obj_loc,
6513 class_offset,
6514 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006515 // Do an exact check.
6516 NearLabel exact_check;
6517 if (cls.IsRegister()) {
6518 __ cmpl(out, cls.AsRegister<Register>());
6519 } else {
6520 DCHECK(cls.IsStackSlot()) << cls;
6521 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6522 }
6523 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006524 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006525 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006526 GenerateReferenceLoadOneRegister(instruction,
6527 out_loc,
6528 component_offset,
6529 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006530 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006531 __ testl(out, out);
6532 // If `out` is null, we use it for the result, and jump to `done`.
6533 __ j(kEqual, &done);
6534 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6535 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006536 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006537 __ movl(out, Immediate(1));
6538 __ jmp(&done);
6539 break;
6540 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006541
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006542 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006543 // No read barrier since the slow path will retry upon failure.
6544 // /* HeapReference<Class> */ out = obj->klass_
6545 GenerateReferenceLoadTwoRegisters(instruction,
6546 out_loc,
6547 obj_loc,
6548 class_offset,
6549 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006550 if (cls.IsRegister()) {
6551 __ cmpl(out, cls.AsRegister<Register>());
6552 } else {
6553 DCHECK(cls.IsStackSlot()) << cls;
6554 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6555 }
6556 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006557 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6558 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006559 codegen_->AddSlowPath(slow_path);
6560 __ j(kNotEqual, slow_path->GetEntryLabel());
6561 __ movl(out, Immediate(1));
6562 if (zero.IsLinked()) {
6563 __ jmp(&done);
6564 }
6565 break;
6566 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006567
Calin Juravle98893e12015-10-02 21:05:03 +01006568 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006569 case TypeCheckKind::kInterfaceCheck: {
6570 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006571 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006572 // cases.
6573 //
6574 // We cannot directly call the InstanceofNonTrivial runtime
6575 // entry point without resorting to a type checking slow path
6576 // here (i.e. by calling InvokeRuntime directly), as it would
6577 // require to assign fixed registers for the inputs of this
6578 // HInstanceOf instruction (following the runtime calling
6579 // convention), which might be cluttered by the potential first
6580 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006581 //
6582 // TODO: Introduce a new runtime entry point taking the object
6583 // to test (instead of its class) as argument, and let it deal
6584 // with the read barrier issues. This will let us refactor this
6585 // case of the `switch` code as it was previously (with a direct
6586 // call to the runtime not using a type checking slow path).
6587 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006588 DCHECK(locations->OnlyCallsOnSlowPath());
6589 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6590 /* is_fatal */ false);
6591 codegen_->AddSlowPath(slow_path);
6592 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006593 if (zero.IsLinked()) {
6594 __ jmp(&done);
6595 }
6596 break;
6597 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006598 }
6599
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006600 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006601 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006602 __ xorl(out, out);
6603 }
6604
6605 if (done.IsLinked()) {
6606 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006607 }
6608
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006609 if (slow_path != nullptr) {
6610 __ Bind(slow_path->GetExitLabel());
6611 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006612}
6613
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006614static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6615 switch (type_check_kind) {
6616 case TypeCheckKind::kExactCheck:
6617 case TypeCheckKind::kAbstractClassCheck:
6618 case TypeCheckKind::kClassHierarchyCheck:
6619 case TypeCheckKind::kArrayObjectCheck:
6620 return !throws_into_catch && !kEmitCompilerReadBarrier;
6621 case TypeCheckKind::kInterfaceCheck:
6622 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6623 case TypeCheckKind::kArrayCheck:
6624 case TypeCheckKind::kUnresolvedCheck:
6625 return false;
6626 }
6627 LOG(FATAL) << "Unreachable";
6628 UNREACHABLE();
6629}
6630
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006631void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006632 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006633 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006634 LocationSummary::CallKind call_kind =
6635 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6636 ? LocationSummary::kNoCall
6637 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006638 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6639 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006640 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6641 // Require a register for the interface check since there is a loop that compares the class to
6642 // a memory address.
6643 locations->SetInAt(1, Location::RequiresRegister());
6644 } else {
6645 locations->SetInAt(1, Location::Any());
6646 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006647 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6648 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006649 // When read barriers are enabled, we need an additional temporary register for some cases.
6650 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6651}
6652
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006653void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006654 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006655 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006656 Location obj_loc = locations->InAt(0);
6657 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006658 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006659 Location temp_loc = locations->GetTemp(0);
6660 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006661 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6662 DCHECK_GE(num_temps, 1u);
6663 DCHECK_LE(num_temps, 2u);
6664 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6665 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6666 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6667 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6668 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6669 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6670 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6671 const uint32_t object_array_data_offset =
6672 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006673
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006674 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6675 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6676 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006677 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006678 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6679
Roland Levillain0d5a2812015-11-13 10:07:31 +00006680 SlowPathCode* type_check_slow_path =
6681 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6682 is_type_check_slow_path_fatal);
6683 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006684
Roland Levillain0d5a2812015-11-13 10:07:31 +00006685 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006686 // Avoid null check if we know obj is not null.
6687 if (instruction->MustDoNullCheck()) {
6688 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006689 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006690 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006691
Roland Levillain0d5a2812015-11-13 10:07:31 +00006692 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006693 case TypeCheckKind::kExactCheck:
6694 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006695 // /* HeapReference<Class> */ temp = obj->klass_
6696 GenerateReferenceLoadTwoRegisters(instruction,
6697 temp_loc,
6698 obj_loc,
6699 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006700 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006701
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006702 if (cls.IsRegister()) {
6703 __ cmpl(temp, cls.AsRegister<Register>());
6704 } else {
6705 DCHECK(cls.IsStackSlot()) << cls;
6706 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6707 }
6708 // Jump to slow path for throwing the exception or doing a
6709 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006710 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006711 break;
6712 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006713
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006714 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006715 // /* HeapReference<Class> */ temp = obj->klass_
6716 GenerateReferenceLoadTwoRegisters(instruction,
6717 temp_loc,
6718 obj_loc,
6719 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006720 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006721
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006722 // If the class is abstract, we eagerly fetch the super class of the
6723 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006724 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006725 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006726 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006727 GenerateReferenceLoadOneRegister(instruction,
6728 temp_loc,
6729 super_offset,
6730 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006731 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006732
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006733 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6734 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006735 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006736 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006737
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006738 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006739 if (cls.IsRegister()) {
6740 __ cmpl(temp, cls.AsRegister<Register>());
6741 } else {
6742 DCHECK(cls.IsStackSlot()) << cls;
6743 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6744 }
6745 __ j(kNotEqual, &loop);
6746 break;
6747 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006748
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006749 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006750 // /* HeapReference<Class> */ temp = obj->klass_
6751 GenerateReferenceLoadTwoRegisters(instruction,
6752 temp_loc,
6753 obj_loc,
6754 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006755 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006756
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006757 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006758 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006759 __ Bind(&loop);
6760 if (cls.IsRegister()) {
6761 __ cmpl(temp, cls.AsRegister<Register>());
6762 } else {
6763 DCHECK(cls.IsStackSlot()) << cls;
6764 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6765 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006766 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006767
Roland Levillain0d5a2812015-11-13 10:07:31 +00006768 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006769 GenerateReferenceLoadOneRegister(instruction,
6770 temp_loc,
6771 super_offset,
6772 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006773 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006774
6775 // If the class reference currently in `temp` is not null, jump
6776 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006777 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006778 __ j(kNotZero, &loop);
6779 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006780 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006781 break;
6782 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006783
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006784 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006785 // /* HeapReference<Class> */ temp = obj->klass_
6786 GenerateReferenceLoadTwoRegisters(instruction,
6787 temp_loc,
6788 obj_loc,
6789 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006790 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006791
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006792 // Do an exact check.
6793 if (cls.IsRegister()) {
6794 __ cmpl(temp, cls.AsRegister<Register>());
6795 } else {
6796 DCHECK(cls.IsStackSlot()) << cls;
6797 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6798 }
6799 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006800
6801 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006802 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006803 GenerateReferenceLoadOneRegister(instruction,
6804 temp_loc,
6805 component_offset,
6806 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006807 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006808
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006809 // If the component type is null (i.e. the object not an array), jump to the slow path to
6810 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006811 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006812 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006813
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006814 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006815 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006816 break;
6817 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006818
Calin Juravle98893e12015-10-02 21:05:03 +01006819 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006820 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006821 // We cannot directly call the CheckCast runtime entry point
6822 // without resorting to a type checking slow path here (i.e. by
6823 // calling InvokeRuntime directly), as it would require to
6824 // assign fixed registers for the inputs of this HInstanceOf
6825 // instruction (following the runtime calling convention), which
6826 // might be cluttered by the potential first read barrier
6827 // emission at the beginning of this method.
6828 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006829 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006830
6831 case TypeCheckKind::kInterfaceCheck: {
6832 // Fast path for the interface check. Since we compare with a memory location in the inner
6833 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6834 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006835 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006836 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006837 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6838 // doing this.
6839 // /* HeapReference<Class> */ temp = obj->klass_
6840 GenerateReferenceLoadTwoRegisters(instruction,
6841 temp_loc,
6842 obj_loc,
6843 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006844 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006845
6846 // /* HeapReference<Class> */ temp = temp->iftable_
6847 GenerateReferenceLoadTwoRegisters(instruction,
6848 temp_loc,
6849 temp_loc,
6850 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006851 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006852 // Null iftable means it is empty.
6853 __ testl(temp, temp);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006854 __ j(kZero, type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006855
6856 // Loop through the iftable and check if any class matches.
6857 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
6858
6859 NearLabel start_loop;
6860 __ Bind(&start_loop);
6861 __ cmpl(cls.AsRegister<Register>(), Address(temp, object_array_data_offset));
6862 __ j(kEqual, &done); // Return if same class.
6863 // Go to next interface.
6864 __ addl(temp, Immediate(2 * kHeapReferenceSize));
6865 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
6866 __ j(kNotZero, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006867 }
6868
6869 __ jmp(type_check_slow_path->GetEntryLabel());
6870 break;
6871 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006872 }
6873 __ Bind(&done);
6874
Roland Levillain0d5a2812015-11-13 10:07:31 +00006875 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006876}
6877
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006878void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6879 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006880 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006881 InvokeRuntimeCallingConvention calling_convention;
6882 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6883}
6884
6885void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006886 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6887 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006888 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006889 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006890 if (instruction->IsEnter()) {
6891 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6892 } else {
6893 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6894 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006895}
6896
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006897void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6898void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6899void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6900
6901void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6902 LocationSummary* locations =
6903 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6904 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6905 || instruction->GetResultType() == Primitive::kPrimLong);
6906 locations->SetInAt(0, Location::RequiresRegister());
6907 locations->SetInAt(1, Location::Any());
6908 locations->SetOut(Location::SameAsFirstInput());
6909}
6910
6911void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6912 HandleBitwiseOperation(instruction);
6913}
6914
6915void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6916 HandleBitwiseOperation(instruction);
6917}
6918
6919void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6920 HandleBitwiseOperation(instruction);
6921}
6922
6923void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6924 LocationSummary* locations = instruction->GetLocations();
6925 Location first = locations->InAt(0);
6926 Location second = locations->InAt(1);
6927 DCHECK(first.Equals(locations->Out()));
6928
6929 if (instruction->GetResultType() == Primitive::kPrimInt) {
6930 if (second.IsRegister()) {
6931 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006932 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006933 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006934 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006935 } else {
6936 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006937 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006938 }
6939 } else if (second.IsConstant()) {
6940 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006941 __ andl(first.AsRegister<Register>(),
6942 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006943 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006944 __ orl(first.AsRegister<Register>(),
6945 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006946 } else {
6947 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006948 __ xorl(first.AsRegister<Register>(),
6949 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006950 }
6951 } else {
6952 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006953 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006954 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006955 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006956 } else {
6957 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006958 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006959 }
6960 }
6961 } else {
6962 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6963 if (second.IsRegisterPair()) {
6964 if (instruction->IsAnd()) {
6965 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6966 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6967 } else if (instruction->IsOr()) {
6968 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6969 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6970 } else {
6971 DCHECK(instruction->IsXor());
6972 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6973 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6974 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006975 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006976 if (instruction->IsAnd()) {
6977 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6978 __ andl(first.AsRegisterPairHigh<Register>(),
6979 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6980 } else if (instruction->IsOr()) {
6981 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6982 __ orl(first.AsRegisterPairHigh<Register>(),
6983 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6984 } else {
6985 DCHECK(instruction->IsXor());
6986 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6987 __ xorl(first.AsRegisterPairHigh<Register>(),
6988 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6989 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006990 } else {
6991 DCHECK(second.IsConstant()) << second;
6992 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006993 int32_t low_value = Low32Bits(value);
6994 int32_t high_value = High32Bits(value);
6995 Immediate low(low_value);
6996 Immediate high(high_value);
6997 Register first_low = first.AsRegisterPairLow<Register>();
6998 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006999 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007000 if (low_value == 0) {
7001 __ xorl(first_low, first_low);
7002 } else if (low_value != -1) {
7003 __ andl(first_low, low);
7004 }
7005 if (high_value == 0) {
7006 __ xorl(first_high, first_high);
7007 } else if (high_value != -1) {
7008 __ andl(first_high, high);
7009 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007010 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007011 if (low_value != 0) {
7012 __ orl(first_low, low);
7013 }
7014 if (high_value != 0) {
7015 __ orl(first_high, high);
7016 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007017 } else {
7018 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007019 if (low_value != 0) {
7020 __ xorl(first_low, low);
7021 }
7022 if (high_value != 0) {
7023 __ xorl(first_high, high);
7024 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007025 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007026 }
7027 }
7028}
7029
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007030void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7031 HInstruction* instruction,
7032 Location out,
7033 uint32_t offset,
7034 Location maybe_temp,
7035 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007036 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007037 if (read_barrier_option == kWithReadBarrier) {
7038 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007039 if (kUseBakerReadBarrier) {
7040 // Load with fast path based Baker's read barrier.
7041 // /* HeapReference<Object> */ out = *(out + offset)
7042 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007043 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007044 } else {
7045 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007046 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007047 // in the following move operation, as we will need it for the
7048 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007049 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007050 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007051 // /* HeapReference<Object> */ out = *(out + offset)
7052 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007053 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007054 }
7055 } else {
7056 // Plain load with no read barrier.
7057 // /* HeapReference<Object> */ out = *(out + offset)
7058 __ movl(out_reg, Address(out_reg, offset));
7059 __ MaybeUnpoisonHeapReference(out_reg);
7060 }
7061}
7062
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007063void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7064 HInstruction* instruction,
7065 Location out,
7066 Location obj,
7067 uint32_t offset,
7068 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007069 Register out_reg = out.AsRegister<Register>();
7070 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007071 if (read_barrier_option == kWithReadBarrier) {
7072 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007073 if (kUseBakerReadBarrier) {
7074 // Load with fast path based Baker's read barrier.
7075 // /* HeapReference<Object> */ out = *(obj + offset)
7076 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007077 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007078 } else {
7079 // Load with slow path based read barrier.
7080 // /* HeapReference<Object> */ out = *(obj + offset)
7081 __ movl(out_reg, Address(obj_reg, offset));
7082 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7083 }
7084 } else {
7085 // Plain load with no read barrier.
7086 // /* HeapReference<Object> */ out = *(obj + offset)
7087 __ movl(out_reg, Address(obj_reg, offset));
7088 __ MaybeUnpoisonHeapReference(out_reg);
7089 }
7090}
7091
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007092void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7093 HInstruction* instruction,
7094 Location root,
7095 const Address& address,
7096 Label* fixup_label,
7097 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007098 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007099 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007100 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007101 if (kUseBakerReadBarrier) {
7102 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7103 // Baker's read barrier are used:
7104 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007105 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00007106 // if (Thread::Current()->GetIsGcMarking()) {
7107 // root = ReadBarrier::Mark(root)
7108 // }
7109
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007110 // /* GcRoot<mirror::Object> */ root = *address
7111 __ movl(root_reg, address);
7112 if (fixup_label != nullptr) {
7113 __ Bind(fixup_label);
7114 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007115 static_assert(
7116 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7117 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7118 "have different sizes.");
7119 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7120 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7121 "have different sizes.");
7122
Vladimir Marko953437b2016-08-24 08:30:46 +00007123 // Slow path marking the GC root `root`.
7124 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007125 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007126 codegen_->AddSlowPath(slow_path);
7127
Andreas Gampe542451c2016-07-26 09:02:02 -07007128 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00007129 Immediate(0));
7130 __ j(kNotEqual, slow_path->GetEntryLabel());
7131 __ Bind(slow_path->GetExitLabel());
7132 } else {
7133 // GC root loaded through a slow path for read barriers other
7134 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007135 // /* GcRoot<mirror::Object>* */ root = address
7136 __ leal(root_reg, address);
7137 if (fixup_label != nullptr) {
7138 __ Bind(fixup_label);
7139 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007140 // /* mirror::Object* */ root = root->Read()
7141 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7142 }
7143 } else {
7144 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007145 // /* GcRoot<mirror::Object> */ root = *address
7146 __ movl(root_reg, address);
7147 if (fixup_label != nullptr) {
7148 __ Bind(fixup_label);
7149 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007150 // Note that GC roots are not affected by heap poisoning, thus we
7151 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007152 }
7153}
7154
7155void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7156 Location ref,
7157 Register obj,
7158 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007159 bool needs_null_check) {
7160 DCHECK(kEmitCompilerReadBarrier);
7161 DCHECK(kUseBakerReadBarrier);
7162
7163 // /* HeapReference<Object> */ ref = *(obj + offset)
7164 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007165 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007166}
7167
7168void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7169 Location ref,
7170 Register obj,
7171 uint32_t data_offset,
7172 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007173 bool needs_null_check) {
7174 DCHECK(kEmitCompilerReadBarrier);
7175 DCHECK(kUseBakerReadBarrier);
7176
Roland Levillain3d312422016-06-23 13:53:42 +01007177 static_assert(
7178 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7179 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007180 // /* HeapReference<Object> */ ref =
7181 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007182 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007183 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007184}
7185
7186void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7187 Location ref,
7188 Register obj,
7189 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007190 bool needs_null_check,
7191 bool always_update_field,
7192 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007193 DCHECK(kEmitCompilerReadBarrier);
7194 DCHECK(kUseBakerReadBarrier);
7195
7196 // In slow path based read barriers, the read barrier call is
7197 // inserted after the original load. However, in fast path based
7198 // Baker's read barriers, we need to perform the load of
7199 // mirror::Object::monitor_ *before* the original reference load.
7200 // This load-load ordering is required by the read barrier.
7201 // The fast path/slow path (for Baker's algorithm) should look like:
7202 //
7203 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7204 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7205 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007206 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007207 // if (is_gray) {
7208 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7209 // }
7210 //
7211 // Note: the original implementation in ReadBarrier::Barrier is
7212 // slightly more complex as:
7213 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007214 // the high-bits of rb_state, which are expected to be all zeroes
7215 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7216 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007217 // - it performs additional checks that we do not do here for
7218 // performance reasons.
7219
7220 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007221 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7222
Vladimir Marko953437b2016-08-24 08:30:46 +00007223 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007224 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7225 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007226 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7227 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7228 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7229
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007230 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007231 // ref = ReadBarrier::Mark(ref);
7232 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7233 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007234 if (needs_null_check) {
7235 MaybeRecordImplicitNullCheck(instruction);
7236 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007237
7238 // Load fence to prevent load-load reordering.
7239 // Note that this is a no-op, thanks to the x86 memory model.
7240 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7241
7242 // The actual reference load.
7243 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007244 __ movl(ref_reg, src); // Flags are unaffected.
7245
7246 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7247 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007248 SlowPathCode* slow_path;
7249 if (always_update_field) {
7250 DCHECK(temp != nullptr);
7251 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7252 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7253 } else {
7254 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7255 instruction, ref, /* unpoison_ref_before_marking */ true);
7256 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007257 AddSlowPath(slow_path);
7258
7259 // We have done the "if" of the gray bit check above, now branch based on the flags.
7260 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007261
7262 // Object* ref = ref_addr->AsMirrorPtr()
7263 __ MaybeUnpoisonHeapReference(ref_reg);
7264
Roland Levillain7c1559a2015-12-15 10:55:36 +00007265 __ Bind(slow_path->GetExitLabel());
7266}
7267
7268void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7269 Location out,
7270 Location ref,
7271 Location obj,
7272 uint32_t offset,
7273 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007274 DCHECK(kEmitCompilerReadBarrier);
7275
Roland Levillain7c1559a2015-12-15 10:55:36 +00007276 // Insert a slow path based read barrier *after* the reference load.
7277 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007278 // If heap poisoning is enabled, the unpoisoning of the loaded
7279 // reference will be carried out by the runtime within the slow
7280 // path.
7281 //
7282 // Note that `ref` currently does not get unpoisoned (when heap
7283 // poisoning is enabled), which is alright as the `ref` argument is
7284 // not used by the artReadBarrierSlow entry point.
7285 //
7286 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7287 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7288 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7289 AddSlowPath(slow_path);
7290
Roland Levillain0d5a2812015-11-13 10:07:31 +00007291 __ jmp(slow_path->GetEntryLabel());
7292 __ Bind(slow_path->GetExitLabel());
7293}
7294
Roland Levillain7c1559a2015-12-15 10:55:36 +00007295void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7296 Location out,
7297 Location ref,
7298 Location obj,
7299 uint32_t offset,
7300 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007301 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007302 // Baker's read barriers shall be handled by the fast path
7303 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7304 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007305 // If heap poisoning is enabled, unpoisoning will be taken care of
7306 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007307 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007308 } else if (kPoisonHeapReferences) {
7309 __ UnpoisonHeapReference(out.AsRegister<Register>());
7310 }
7311}
7312
Roland Levillain7c1559a2015-12-15 10:55:36 +00007313void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7314 Location out,
7315 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007316 DCHECK(kEmitCompilerReadBarrier);
7317
Roland Levillain7c1559a2015-12-15 10:55:36 +00007318 // Insert a slow path based read barrier *after* the GC root load.
7319 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007320 // Note that GC roots are not affected by heap poisoning, so we do
7321 // not need to do anything special for this here.
7322 SlowPathCode* slow_path =
7323 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7324 AddSlowPath(slow_path);
7325
Roland Levillain0d5a2812015-11-13 10:07:31 +00007326 __ jmp(slow_path->GetEntryLabel());
7327 __ Bind(slow_path->GetExitLabel());
7328}
7329
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007330void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007331 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007332 LOG(FATAL) << "Unreachable";
7333}
7334
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007335void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007336 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007337 LOG(FATAL) << "Unreachable";
7338}
7339
Mark Mendellfe57faa2015-09-18 09:26:15 -04007340// Simple implementation of packed switch - generate cascaded compare/jumps.
7341void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7342 LocationSummary* locations =
7343 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7344 locations->SetInAt(0, Location::RequiresRegister());
7345}
7346
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007347void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7348 int32_t lower_bound,
7349 uint32_t num_entries,
7350 HBasicBlock* switch_block,
7351 HBasicBlock* default_block) {
7352 // Figure out the correct compare values and jump conditions.
7353 // Handle the first compare/branch as a special case because it might
7354 // jump to the default case.
7355 DCHECK_GT(num_entries, 2u);
7356 Condition first_condition;
7357 uint32_t index;
7358 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7359 if (lower_bound != 0) {
7360 first_condition = kLess;
7361 __ cmpl(value_reg, Immediate(lower_bound));
7362 __ j(first_condition, codegen_->GetLabelOf(default_block));
7363 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007364
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007365 index = 1;
7366 } else {
7367 // Handle all the compare/jumps below.
7368 first_condition = kBelow;
7369 index = 0;
7370 }
7371
7372 // Handle the rest of the compare/jumps.
7373 for (; index + 1 < num_entries; index += 2) {
7374 int32_t compare_to_value = lower_bound + index + 1;
7375 __ cmpl(value_reg, Immediate(compare_to_value));
7376 // Jump to successors[index] if value < case_value[index].
7377 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7378 // Jump to successors[index + 1] if value == case_value[index + 1].
7379 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7380 }
7381
7382 if (index != num_entries) {
7383 // There are an odd number of entries. Handle the last one.
7384 DCHECK_EQ(index + 1, num_entries);
7385 __ cmpl(value_reg, Immediate(lower_bound + index));
7386 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007387 }
7388
7389 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007390 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7391 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007392 }
7393}
7394
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007395void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7396 int32_t lower_bound = switch_instr->GetStartValue();
7397 uint32_t num_entries = switch_instr->GetNumEntries();
7398 LocationSummary* locations = switch_instr->GetLocations();
7399 Register value_reg = locations->InAt(0).AsRegister<Register>();
7400
7401 GenPackedSwitchWithCompares(value_reg,
7402 lower_bound,
7403 num_entries,
7404 switch_instr->GetBlock(),
7405 switch_instr->GetDefaultBlock());
7406}
7407
Mark Mendell805b3b52015-09-18 14:10:29 -04007408void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7409 LocationSummary* locations =
7410 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7411 locations->SetInAt(0, Location::RequiresRegister());
7412
7413 // Constant area pointer.
7414 locations->SetInAt(1, Location::RequiresRegister());
7415
7416 // And the temporary we need.
7417 locations->AddTemp(Location::RequiresRegister());
7418}
7419
7420void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7421 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007422 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007423 LocationSummary* locations = switch_instr->GetLocations();
7424 Register value_reg = locations->InAt(0).AsRegister<Register>();
7425 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7426
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007427 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7428 GenPackedSwitchWithCompares(value_reg,
7429 lower_bound,
7430 num_entries,
7431 switch_instr->GetBlock(),
7432 default_block);
7433 return;
7434 }
7435
Mark Mendell805b3b52015-09-18 14:10:29 -04007436 // Optimizing has a jump area.
7437 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7438 Register constant_area = locations->InAt(1).AsRegister<Register>();
7439
7440 // Remove the bias, if needed.
7441 if (lower_bound != 0) {
7442 __ leal(temp_reg, Address(value_reg, -lower_bound));
7443 value_reg = temp_reg;
7444 }
7445
7446 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007447 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007448 __ cmpl(value_reg, Immediate(num_entries - 1));
7449 __ j(kAbove, codegen_->GetLabelOf(default_block));
7450
7451 // We are in the range of the table.
7452 // Load (target-constant_area) from the jump table, indexing by the value.
7453 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7454
7455 // Compute the actual target address by adding in constant_area.
7456 __ addl(temp_reg, constant_area);
7457
7458 // And jump.
7459 __ jmp(temp_reg);
7460}
7461
Mark Mendell0616ae02015-04-17 12:49:27 -04007462void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7463 HX86ComputeBaseMethodAddress* insn) {
7464 LocationSummary* locations =
7465 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7466 locations->SetOut(Location::RequiresRegister());
7467}
7468
7469void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7470 HX86ComputeBaseMethodAddress* insn) {
7471 LocationSummary* locations = insn->GetLocations();
7472 Register reg = locations->Out().AsRegister<Register>();
7473
7474 // Generate call to next instruction.
7475 Label next_instruction;
7476 __ call(&next_instruction);
7477 __ Bind(&next_instruction);
7478
7479 // Remember this offset for later use with constant area.
7480 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7481
7482 // Grab the return address off the stack.
7483 __ popl(reg);
7484}
7485
7486void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7487 HX86LoadFromConstantTable* insn) {
7488 LocationSummary* locations =
7489 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7490
7491 locations->SetInAt(0, Location::RequiresRegister());
7492 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7493
7494 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007495 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007496 return;
7497 }
7498
7499 switch (insn->GetType()) {
7500 case Primitive::kPrimFloat:
7501 case Primitive::kPrimDouble:
7502 locations->SetOut(Location::RequiresFpuRegister());
7503 break;
7504
7505 case Primitive::kPrimInt:
7506 locations->SetOut(Location::RequiresRegister());
7507 break;
7508
7509 default:
7510 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7511 }
7512}
7513
7514void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007515 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007516 return;
7517 }
7518
7519 LocationSummary* locations = insn->GetLocations();
7520 Location out = locations->Out();
7521 Register const_area = locations->InAt(0).AsRegister<Register>();
7522 HConstant *value = insn->GetConstant();
7523
7524 switch (insn->GetType()) {
7525 case Primitive::kPrimFloat:
7526 __ movss(out.AsFpuRegister<XmmRegister>(),
7527 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7528 break;
7529
7530 case Primitive::kPrimDouble:
7531 __ movsd(out.AsFpuRegister<XmmRegister>(),
7532 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7533 break;
7534
7535 case Primitive::kPrimInt:
7536 __ movl(out.AsRegister<Register>(),
7537 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7538 break;
7539
7540 default:
7541 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7542 }
7543}
7544
Mark Mendell0616ae02015-04-17 12:49:27 -04007545/**
7546 * Class to handle late fixup of offsets into constant area.
7547 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007548class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007549 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007550 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7551 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7552
7553 protected:
7554 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7555
7556 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007557
7558 private:
7559 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7560 // Patch the correct offset for the instruction. The place to patch is the
7561 // last 4 bytes of the instruction.
7562 // The value to patch is the distance from the offset in the constant area
7563 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007564 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7565 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007566
7567 // Patch in the right value.
7568 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7569 }
7570
Mark Mendell0616ae02015-04-17 12:49:27 -04007571 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007572 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007573};
7574
Mark Mendell805b3b52015-09-18 14:10:29 -04007575/**
7576 * Class to handle late fixup of offsets to a jump table that will be created in the
7577 * constant area.
7578 */
7579class JumpTableRIPFixup : public RIPFixup {
7580 public:
7581 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7582 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7583
7584 void CreateJumpTable() {
7585 X86Assembler* assembler = codegen_->GetAssembler();
7586
7587 // Ensure that the reference to the jump table has the correct offset.
7588 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7589 SetOffset(offset_in_constant_table);
7590
7591 // The label values in the jump table are computed relative to the
7592 // instruction addressing the constant area.
7593 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7594
7595 // Populate the jump table with the correct values for the jump table.
7596 int32_t num_entries = switch_instr_->GetNumEntries();
7597 HBasicBlock* block = switch_instr_->GetBlock();
7598 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7599 // The value that we want is the target offset - the position of the table.
7600 for (int32_t i = 0; i < num_entries; i++) {
7601 HBasicBlock* b = successors[i];
7602 Label* l = codegen_->GetLabelOf(b);
7603 DCHECK(l->IsBound());
7604 int32_t offset_to_block = l->Position() - relative_offset;
7605 assembler->AppendInt32(offset_to_block);
7606 }
7607 }
7608
7609 private:
7610 const HX86PackedSwitch* switch_instr_;
7611};
7612
7613void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7614 // Generate the constant area if needed.
7615 X86Assembler* assembler = GetAssembler();
7616 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7617 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7618 // byte values.
7619 assembler->Align(4, 0);
7620 constant_area_start_ = assembler->CodeSize();
7621
7622 // Populate any jump tables.
7623 for (auto jump_table : fixups_to_jump_tables_) {
7624 jump_table->CreateJumpTable();
7625 }
7626
7627 // And now add the constant area to the generated code.
7628 assembler->AddConstantArea();
7629 }
7630
7631 // And finish up.
7632 CodeGenerator::Finalize(allocator);
7633}
7634
Mark Mendell0616ae02015-04-17 12:49:27 -04007635Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7636 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7637 return Address(reg, kDummy32BitOffset, fixup);
7638}
7639
7640Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7641 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7642 return Address(reg, kDummy32BitOffset, fixup);
7643}
7644
7645Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7646 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7647 return Address(reg, kDummy32BitOffset, fixup);
7648}
7649
7650Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7651 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7652 return Address(reg, kDummy32BitOffset, fixup);
7653}
7654
Aart Bika19616e2016-02-01 18:57:58 -08007655void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7656 if (value == 0) {
7657 __ xorl(dest, dest);
7658 } else {
7659 __ movl(dest, Immediate(value));
7660 }
7661}
7662
7663void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7664 if (value == 0) {
7665 __ testl(dest, dest);
7666 } else {
7667 __ cmpl(dest, Immediate(value));
7668 }
7669}
7670
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007671void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7672 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007673 GenerateIntCompare(lhs_reg, rhs);
7674}
7675
7676void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007677 if (rhs.IsConstant()) {
7678 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007679 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007680 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007681 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007682 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007683 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007684 }
7685}
7686
7687Address CodeGeneratorX86::ArrayAddress(Register obj,
7688 Location index,
7689 ScaleFactor scale,
7690 uint32_t data_offset) {
7691 return index.IsConstant() ?
7692 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7693 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7694}
7695
Mark Mendell805b3b52015-09-18 14:10:29 -04007696Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7697 Register reg,
7698 Register value) {
7699 // Create a fixup to be used to create and address the jump table.
7700 JumpTableRIPFixup* table_fixup =
7701 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7702
7703 // We have to populate the jump tables.
7704 fixups_to_jump_tables_.push_back(table_fixup);
7705
7706 // We want a scaled address, as we are extracting the correct offset from the table.
7707 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7708}
7709
Andreas Gampe85b62f22015-09-09 13:15:38 -07007710// TODO: target as memory.
7711void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7712 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007713 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007714 return;
7715 }
7716
7717 DCHECK_NE(type, Primitive::kPrimVoid);
7718
7719 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7720 if (target.Equals(return_loc)) {
7721 return;
7722 }
7723
7724 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7725 // with the else branch.
7726 if (type == Primitive::kPrimLong) {
7727 HParallelMove parallel_move(GetGraph()->GetArena());
7728 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7729 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7730 GetMoveResolver()->EmitNativeCode(&parallel_move);
7731 } else {
7732 // Let the parallel move resolver take care of all of this.
7733 HParallelMove parallel_move(GetGraph()->GetArena());
7734 parallel_move.AddMove(return_loc, target, type, nullptr);
7735 GetMoveResolver()->EmitNativeCode(&parallel_move);
7736 }
7737}
7738
Roland Levillain4d027112015-07-01 15:41:14 +01007739#undef __
7740
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007741} // namespace x86
7742} // namespace art