blob: 5d3f5bae4c949e663ae8a742ec5752114c6ae9b7 [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) {
154 __ andl(length_loc.AsRegister<Register>(), Immediate(INT32_MAX));
155 }
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();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800315 Location arg0, arg1;
316 if (instruction_->IsInstanceOf()) {
317 arg0 = locations->InAt(1);
318 arg1 = locations->Out();
319 } else {
320 arg0 = locations->InAt(0);
321 arg1 = locations->InAt(1);
322 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 DCHECK(instruction_->IsCheckCast()
324 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000325
326 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
327 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000328
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000329 if (!is_fatal_) {
330 SaveLiveRegisters(codegen, locations);
331 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
333 // We're moving two locations to locations that could overlap, so we need a parallel
334 // move resolver.
335 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800336 x86_codegen->EmitParallelMoves(arg0,
337 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
338 Primitive::kPrimNot,
339 arg1,
340 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
341 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000342 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100343 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100344 instruction_,
345 instruction_->GetDexPc(),
346 this);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800347 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Class*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000348 } else {
349 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800350 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
351 instruction_,
352 instruction_->GetDexPc(),
353 this);
354 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000355 }
356
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000357 if (!is_fatal_) {
358 if (instruction_->IsInstanceOf()) {
359 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
360 }
361 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000362
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000363 __ jmp(GetExitLabel());
364 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000365 }
366
Alexandre Rames9931f312015-06-19 14:47:01 +0100367 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000368 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100369
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000370 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000371 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000372
373 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
374};
375
Andreas Gampe85b62f22015-09-09 13:15:38 -0700376class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700377 public:
Aart Bik42249c32016-01-07 15:33:50 -0800378 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000379 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700380
381 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100382 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700383 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100384 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000385 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386 }
387
Alexandre Rames9931f312015-06-19 14:47:01 +0100388 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
389
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700390 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700391 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
392};
393
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100394class ArraySetSlowPathX86 : public SlowPathCode {
395 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000396 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100397
398 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
399 LocationSummary* locations = instruction_->GetLocations();
400 __ Bind(GetEntryLabel());
401 SaveLiveRegisters(codegen, locations);
402
403 InvokeRuntimeCallingConvention calling_convention;
404 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
405 parallel_move.AddMove(
406 locations->InAt(0),
407 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
408 Primitive::kPrimNot,
409 nullptr);
410 parallel_move.AddMove(
411 locations->InAt(1),
412 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
413 Primitive::kPrimInt,
414 nullptr);
415 parallel_move.AddMove(
416 locations->InAt(2),
417 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
418 Primitive::kPrimNot,
419 nullptr);
420 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
421
422 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100423 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000424 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100425 RestoreLiveRegisters(codegen, locations);
426 __ jmp(GetExitLabel());
427 }
428
429 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
430
431 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100432 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
433};
434
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100435// Slow path marking an object reference `ref` during a read
436// barrier. The field `obj.field` in the object `obj` holding this
437// reference does not get updated by this slow path after marking (see
438// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
439//
440// This means that after the execution of this slow path, `ref` will
441// always be up-to-date, but `obj.field` may not; i.e., after the
442// flip, `ref` will be a to-space reference, but `obj.field` will
443// probably still be a from-space reference (unless it gets updated by
444// another thread, or if another thread installed another object
445// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000446class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
447 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100448 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
449 Location ref,
450 bool unpoison_ref_before_marking)
451 : SlowPathCode(instruction),
452 ref_(ref),
453 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000454 DCHECK(kEmitCompilerReadBarrier);
455 }
456
457 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
458
459 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
460 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100461 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000462 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100463 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000464 DCHECK(instruction_->IsInstanceFieldGet() ||
465 instruction_->IsStaticFieldGet() ||
466 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100467 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000468 instruction_->IsLoadClass() ||
469 instruction_->IsLoadString() ||
470 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100471 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100472 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
473 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000474 << "Unexpected instruction in read barrier marking slow path: "
475 << instruction_->DebugName();
476
477 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100478 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000479 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100480 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000481 }
Roland Levillain4359e612016-07-20 11:32:19 +0100482 // No need to save live registers; it's taken care of by the
483 // entrypoint. Also, there is no need to update the stack mask,
484 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000485 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100486 DCHECK_NE(ref_reg, ESP);
487 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100488 // "Compact" slow path, saving two moves.
489 //
490 // Instead of using the standard runtime calling convention (input
491 // and output in EAX):
492 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100493 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100494 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100495 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100496 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100497 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100498 // of a dedicated entrypoint:
499 //
500 // rX <- ReadBarrierMarkRegX(rX)
501 //
502 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100503 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100504 // This runtime call does not require a stack map.
505 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000506 __ jmp(GetExitLabel());
507 }
508
509 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100510 // The location (register) of the marked object reference.
511 const Location ref_;
512 // Should the reference in `ref_` be unpoisoned prior to marking it?
513 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000514
515 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
516};
517
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100518// Slow path marking an object reference `ref` during a read barrier,
519// and if needed, atomically updating the field `obj.field` in the
520// object `obj` holding this reference after marking (contrary to
521// ReadBarrierMarkSlowPathX86 above, which never tries to update
522// `obj.field`).
523//
524// This means that after the execution of this slow path, both `ref`
525// and `obj.field` will be up-to-date; i.e., after the flip, both will
526// hold the same to-space reference (unless another thread installed
527// another object reference (different from `ref`) in `obj.field`).
528class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
529 public:
530 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
531 Location ref,
532 Register obj,
533 const Address& field_addr,
534 bool unpoison_ref_before_marking,
535 Register temp)
536 : SlowPathCode(instruction),
537 ref_(ref),
538 obj_(obj),
539 field_addr_(field_addr),
540 unpoison_ref_before_marking_(unpoison_ref_before_marking),
541 temp_(temp) {
542 DCHECK(kEmitCompilerReadBarrier);
543 }
544
545 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
546
547 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
548 LocationSummary* locations = instruction_->GetLocations();
549 Register ref_reg = ref_.AsRegister<Register>();
550 DCHECK(locations->CanCall());
551 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
552 // This slow path is only used by the UnsafeCASObject intrinsic.
553 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
554 << "Unexpected instruction in read barrier marking and field updating slow path: "
555 << instruction_->DebugName();
556 DCHECK(instruction_->GetLocations()->Intrinsified());
557 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
558
559 __ Bind(GetEntryLabel());
560 if (unpoison_ref_before_marking_) {
561 // Object* ref = ref_addr->AsMirrorPtr()
562 __ MaybeUnpoisonHeapReference(ref_reg);
563 }
564
565 // Save the old (unpoisoned) reference.
566 __ movl(temp_, ref_reg);
567
568 // No need to save live registers; it's taken care of by the
569 // entrypoint. Also, there is no need to update the stack mask,
570 // as this runtime call will not trigger a garbage collection.
571 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
572 DCHECK_NE(ref_reg, ESP);
573 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
574 // "Compact" slow path, saving two moves.
575 //
576 // Instead of using the standard runtime calling convention (input
577 // and output in EAX):
578 //
579 // EAX <- ref
580 // EAX <- ReadBarrierMark(EAX)
581 // ref <- EAX
582 //
583 // we just use rX (the register containing `ref`) as input and output
584 // of a dedicated entrypoint:
585 //
586 // rX <- ReadBarrierMarkRegX(rX)
587 //
588 int32_t entry_point_offset =
589 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
590 // This runtime call does not require a stack map.
591 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
592
593 // If the new reference is different from the old reference,
594 // update the field in the holder (`*field_addr`).
595 //
596 // Note that this field could also hold a different object, if
597 // another thread had concurrently changed it. In that case, the
598 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
599 // operation below would abort the CAS, leaving the field as-is.
600 NearLabel done;
601 __ cmpl(temp_, ref_reg);
602 __ j(kEqual, &done);
603
604 // Update the the holder's field atomically. This may fail if
605 // mutator updates before us, but it's OK. This is achieved
606 // using a strong compare-and-set (CAS) operation with relaxed
607 // memory synchronization ordering, where the expected value is
608 // the old reference and the desired value is the new reference.
609 // This operation is implemented with a 32-bit LOCK CMPXLCHG
610 // instruction, which requires the expected value (the old
611 // reference) to be in EAX. Save EAX beforehand, and move the
612 // expected value (stored in `temp_`) into EAX.
613 __ pushl(EAX);
614 __ movl(EAX, temp_);
615
616 // Convenience aliases.
617 Register base = obj_;
618 Register expected = EAX;
619 Register value = ref_reg;
620
621 bool base_equals_value = (base == value);
622 if (kPoisonHeapReferences) {
623 if (base_equals_value) {
624 // If `base` and `value` are the same register location, move
625 // `value` to a temporary register. This way, poisoning
626 // `value` won't invalidate `base`.
627 value = temp_;
628 __ movl(value, base);
629 }
630
631 // Check that the register allocator did not assign the location
632 // of `expected` (EAX) to `value` nor to `base`, so that heap
633 // poisoning (when enabled) works as intended below.
634 // - If `value` were equal to `expected`, both references would
635 // be poisoned twice, meaning they would not be poisoned at
636 // all, as heap poisoning uses address negation.
637 // - If `base` were equal to `expected`, poisoning `expected`
638 // would invalidate `base`.
639 DCHECK_NE(value, expected);
640 DCHECK_NE(base, expected);
641
642 __ PoisonHeapReference(expected);
643 __ PoisonHeapReference(value);
644 }
645
646 __ LockCmpxchgl(field_addr_, value);
647
648 // If heap poisoning is enabled, we need to unpoison the values
649 // that were poisoned earlier.
650 if (kPoisonHeapReferences) {
651 if (base_equals_value) {
652 // `value` has been moved to a temporary register, no need
653 // to unpoison it.
654 } else {
655 __ UnpoisonHeapReference(value);
656 }
657 // No need to unpoison `expected` (EAX), as it is be overwritten below.
658 }
659
660 // Restore EAX.
661 __ popl(EAX);
662
663 __ Bind(&done);
664 __ jmp(GetExitLabel());
665 }
666
667 private:
668 // The location (register) of the marked object reference.
669 const Location ref_;
670 // The register containing the object holding the marked object reference field.
671 const Register obj_;
672 // The address of the marked reference field. The base of this address must be `obj_`.
673 const Address field_addr_;
674
675 // Should the reference in `ref_` be unpoisoned prior to marking it?
676 const bool unpoison_ref_before_marking_;
677
678 const Register temp_;
679
680 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
681};
682
Roland Levillain0d5a2812015-11-13 10:07:31 +0000683// Slow path generating a read barrier for a heap reference.
684class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
685 public:
686 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
687 Location out,
688 Location ref,
689 Location obj,
690 uint32_t offset,
691 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000692 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000693 out_(out),
694 ref_(ref),
695 obj_(obj),
696 offset_(offset),
697 index_(index) {
698 DCHECK(kEmitCompilerReadBarrier);
699 // If `obj` is equal to `out` or `ref`, it means the initial object
700 // has been overwritten by (or after) the heap object reference load
701 // to be instrumented, e.g.:
702 //
703 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000704 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000705 //
706 // In that case, we have lost the information about the original
707 // object, and the emitted read barrier cannot work properly.
708 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
709 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
710 }
711
712 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
713 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
714 LocationSummary* locations = instruction_->GetLocations();
715 Register reg_out = out_.AsRegister<Register>();
716 DCHECK(locations->CanCall());
717 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100718 DCHECK(instruction_->IsInstanceFieldGet() ||
719 instruction_->IsStaticFieldGet() ||
720 instruction_->IsArrayGet() ||
721 instruction_->IsInstanceOf() ||
722 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100723 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000724 << "Unexpected instruction in read barrier for heap reference slow path: "
725 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000726
727 __ Bind(GetEntryLabel());
728 SaveLiveRegisters(codegen, locations);
729
730 // We may have to change the index's value, but as `index_` is a
731 // constant member (like other "inputs" of this slow path),
732 // introduce a copy of it, `index`.
733 Location index = index_;
734 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100735 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000736 if (instruction_->IsArrayGet()) {
737 // Compute the actual memory offset and store it in `index`.
738 Register index_reg = index_.AsRegister<Register>();
739 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
740 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
741 // We are about to change the value of `index_reg` (see the
742 // calls to art::x86::X86Assembler::shll and
743 // art::x86::X86Assembler::AddImmediate below), but it has
744 // not been saved by the previous call to
745 // art::SlowPathCode::SaveLiveRegisters, as it is a
746 // callee-save register --
747 // art::SlowPathCode::SaveLiveRegisters does not consider
748 // callee-save registers, as it has been designed with the
749 // assumption that callee-save registers are supposed to be
750 // handled by the called function. So, as a callee-save
751 // register, `index_reg` _would_ eventually be saved onto
752 // the stack, but it would be too late: we would have
753 // changed its value earlier. Therefore, we manually save
754 // it here into another freely available register,
755 // `free_reg`, chosen of course among the caller-save
756 // registers (as a callee-save `free_reg` register would
757 // exhibit the same problem).
758 //
759 // Note we could have requested a temporary register from
760 // the register allocator instead; but we prefer not to, as
761 // this is a slow path, and we know we can find a
762 // caller-save register that is available.
763 Register free_reg = FindAvailableCallerSaveRegister(codegen);
764 __ movl(free_reg, index_reg);
765 index_reg = free_reg;
766 index = Location::RegisterLocation(index_reg);
767 } else {
768 // The initial register stored in `index_` has already been
769 // saved in the call to art::SlowPathCode::SaveLiveRegisters
770 // (as it is not a callee-save register), so we can freely
771 // use it.
772 }
773 // Shifting the index value contained in `index_reg` by the scale
774 // factor (2) cannot overflow in practice, as the runtime is
775 // unable to allocate object arrays with a size larger than
776 // 2^26 - 1 (that is, 2^28 - 4 bytes).
777 __ shll(index_reg, Immediate(TIMES_4));
778 static_assert(
779 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
780 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
781 __ AddImmediate(index_reg, Immediate(offset_));
782 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100783 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
784 // intrinsics, `index_` is not shifted by a scale factor of 2
785 // (as in the case of ArrayGet), as it is actually an offset
786 // to an object field within an object.
787 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000788 DCHECK(instruction_->GetLocations()->Intrinsified());
789 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
790 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
791 << instruction_->AsInvoke()->GetIntrinsic();
792 DCHECK_EQ(offset_, 0U);
793 DCHECK(index_.IsRegisterPair());
794 // UnsafeGet's offset location is a register pair, the low
795 // part contains the correct offset.
796 index = index_.ToLow();
797 }
798 }
799
800 // We're moving two or three locations to locations that could
801 // overlap, so we need a parallel move resolver.
802 InvokeRuntimeCallingConvention calling_convention;
803 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
804 parallel_move.AddMove(ref_,
805 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
806 Primitive::kPrimNot,
807 nullptr);
808 parallel_move.AddMove(obj_,
809 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
810 Primitive::kPrimNot,
811 nullptr);
812 if (index.IsValid()) {
813 parallel_move.AddMove(index,
814 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
815 Primitive::kPrimInt,
816 nullptr);
817 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
818 } else {
819 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
820 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
821 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100822 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000823 CheckEntrypointTypes<
824 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
825 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
826
827 RestoreLiveRegisters(codegen, locations);
828 __ jmp(GetExitLabel());
829 }
830
831 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
832
833 private:
834 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
835 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
836 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
837 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
838 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
839 return static_cast<Register>(i);
840 }
841 }
842 // We shall never fail to find a free caller-save register, as
843 // there are more than two core caller-save registers on x86
844 // (meaning it is possible to find one which is different from
845 // `ref` and `obj`).
846 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
847 LOG(FATAL) << "Could not find a free caller-save register";
848 UNREACHABLE();
849 }
850
Roland Levillain0d5a2812015-11-13 10:07:31 +0000851 const Location out_;
852 const Location ref_;
853 const Location obj_;
854 const uint32_t offset_;
855 // An additional location containing an index to an array.
856 // Only used for HArrayGet and the UnsafeGetObject &
857 // UnsafeGetObjectVolatile intrinsics.
858 const Location index_;
859
860 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
861};
862
863// Slow path generating a read barrier for a GC root.
864class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
865 public:
866 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000867 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000868 DCHECK(kEmitCompilerReadBarrier);
869 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000870
871 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
872 LocationSummary* locations = instruction_->GetLocations();
873 Register reg_out = out_.AsRegister<Register>();
874 DCHECK(locations->CanCall());
875 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000876 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
877 << "Unexpected instruction in read barrier for GC root slow path: "
878 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000879
880 __ Bind(GetEntryLabel());
881 SaveLiveRegisters(codegen, locations);
882
883 InvokeRuntimeCallingConvention calling_convention;
884 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
885 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100886 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000887 instruction_,
888 instruction_->GetDexPc(),
889 this);
890 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
891 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
892
893 RestoreLiveRegisters(codegen, locations);
894 __ jmp(GetExitLabel());
895 }
896
897 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
898
899 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000900 const Location out_;
901 const Location root_;
902
903 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
904};
905
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100906#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100907// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
908#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100909
Aart Bike9f37602015-10-09 11:15:55 -0700910inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700911 switch (cond) {
912 case kCondEQ: return kEqual;
913 case kCondNE: return kNotEqual;
914 case kCondLT: return kLess;
915 case kCondLE: return kLessEqual;
916 case kCondGT: return kGreater;
917 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700918 case kCondB: return kBelow;
919 case kCondBE: return kBelowEqual;
920 case kCondA: return kAbove;
921 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700922 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100923 LOG(FATAL) << "Unreachable";
924 UNREACHABLE();
925}
926
Aart Bike9f37602015-10-09 11:15:55 -0700927// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100928inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
929 switch (cond) {
930 case kCondEQ: return kEqual;
931 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700932 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100933 case kCondLT: return kBelow;
934 case kCondLE: return kBelowEqual;
935 case kCondGT: return kAbove;
936 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700937 // Unsigned remain unchanged.
938 case kCondB: return kBelow;
939 case kCondBE: return kBelowEqual;
940 case kCondA: return kAbove;
941 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100942 }
943 LOG(FATAL) << "Unreachable";
944 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700945}
946
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100947void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100948 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100949}
950
951void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100952 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100953}
954
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100955size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
956 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
957 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100958}
959
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100960size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
961 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
962 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100963}
964
Mark Mendell7c8d0092015-01-26 11:21:33 -0500965size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
966 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
967 return GetFloatingPointSpillSlotSize();
968}
969
970size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
971 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
972 return GetFloatingPointSpillSlotSize();
973}
974
Calin Juravle175dc732015-08-25 15:42:32 +0100975void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
976 HInstruction* instruction,
977 uint32_t dex_pc,
978 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100979 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100980 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
981 if (EntrypointRequiresStackMap(entrypoint)) {
982 RecordPcInfo(instruction, dex_pc, slow_path);
983 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100984}
985
Roland Levillaindec8f632016-07-22 17:10:06 +0100986void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
987 HInstruction* instruction,
988 SlowPathCode* slow_path) {
989 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100990 GenerateInvokeRuntime(entry_point_offset);
991}
992
993void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +0100994 __ fs()->call(Address::Absolute(entry_point_offset));
995}
996
Mark Mendellfb8d2792015-03-31 22:16:59 -0400997CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000998 const X86InstructionSetFeatures& isa_features,
999 const CompilerOptions& compiler_options,
1000 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001001 : CodeGenerator(graph,
1002 kNumberOfCpuRegisters,
1003 kNumberOfXmmRegisters,
1004 kNumberOfRegisterPairs,
1005 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1006 arraysize(kCoreCalleeSaves))
1007 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001008 0,
1009 compiler_options,
1010 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001011 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001012 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001013 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001014 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001015 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001016 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +01001017 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -04001018 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001019 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001020 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1021 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001022 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray997d1212016-11-09 10:36:29 +00001023 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001024 constant_area_start_(-1),
1025 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1026 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001027 // Use a fake return address register to mimic Quick.
1028 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001029}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001030
David Brazdil58282f42016-01-14 12:45:10 +00001031void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001032 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001033 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001034}
1035
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001036InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001037 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001038 assembler_(codegen->GetAssembler()),
1039 codegen_(codegen) {}
1040
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001041static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001042 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001043}
1044
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001045void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001046 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001047 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001048 bool skip_overflow_check =
1049 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001050 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001051
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001052 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001053 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001054 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001055 }
1056
Mark Mendell5f874182015-03-04 15:42:45 -05001057 if (HasEmptyFrame()) {
1058 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001059 }
Mark Mendell5f874182015-03-04 15:42:45 -05001060
1061 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1062 Register reg = kCoreCalleeSaves[i];
1063 if (allocated_registers_.ContainsCoreRegister(reg)) {
1064 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001065 __ cfi().AdjustCFAOffset(kX86WordSize);
1066 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001067 }
1068 }
1069
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001070 int adjust = GetFrameSize() - FrameEntrySpillSize();
1071 __ subl(ESP, Immediate(adjust));
1072 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001073 // Save the current method if we need it. Note that we do not
1074 // do this in HCurrentMethod, as the instruction might have been removed
1075 // in the SSA graph.
1076 if (RequiresCurrentMethod()) {
1077 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1078 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001079}
1080
1081void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001082 __ cfi().RememberState();
1083 if (!HasEmptyFrame()) {
1084 int adjust = GetFrameSize() - FrameEntrySpillSize();
1085 __ addl(ESP, Immediate(adjust));
1086 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001087
David Srbeckyc34dc932015-04-12 09:27:43 +01001088 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1089 Register reg = kCoreCalleeSaves[i];
1090 if (allocated_registers_.ContainsCoreRegister(reg)) {
1091 __ popl(reg);
1092 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1093 __ cfi().Restore(DWARFReg(reg));
1094 }
Mark Mendell5f874182015-03-04 15:42:45 -05001095 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001096 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001097 __ ret();
1098 __ cfi().RestoreState();
1099 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001100}
1101
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001102void CodeGeneratorX86::Bind(HBasicBlock* block) {
1103 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001104}
1105
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001106Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1107 switch (type) {
1108 case Primitive::kPrimBoolean:
1109 case Primitive::kPrimByte:
1110 case Primitive::kPrimChar:
1111 case Primitive::kPrimShort:
1112 case Primitive::kPrimInt:
1113 case Primitive::kPrimNot:
1114 return Location::RegisterLocation(EAX);
1115
1116 case Primitive::kPrimLong:
1117 return Location::RegisterPairLocation(EAX, EDX);
1118
1119 case Primitive::kPrimVoid:
1120 return Location::NoLocation();
1121
1122 case Primitive::kPrimDouble:
1123 case Primitive::kPrimFloat:
1124 return Location::FpuRegisterLocation(XMM0);
1125 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001126
1127 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001128}
1129
1130Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1131 return Location::RegisterLocation(kMethodRegisterArgument);
1132}
1133
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001134Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001135 switch (type) {
1136 case Primitive::kPrimBoolean:
1137 case Primitive::kPrimByte:
1138 case Primitive::kPrimChar:
1139 case Primitive::kPrimShort:
1140 case Primitive::kPrimInt:
1141 case Primitive::kPrimNot: {
1142 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001143 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001144 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001145 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001146 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001147 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001148 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001149 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001150
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001151 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001152 uint32_t index = gp_index_;
1153 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001154 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001155 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001156 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1157 calling_convention.GetRegisterPairAt(index));
1158 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001159 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001160 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1161 }
1162 }
1163
1164 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001165 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001166 stack_index_++;
1167 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1168 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1169 } else {
1170 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1171 }
1172 }
1173
1174 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001175 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001176 stack_index_ += 2;
1177 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1178 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1179 } else {
1180 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001181 }
1182 }
1183
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001184 case Primitive::kPrimVoid:
1185 LOG(FATAL) << "Unexpected parameter type " << type;
1186 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001187 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001188 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001189}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001190
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001191void CodeGeneratorX86::Move32(Location destination, Location source) {
1192 if (source.Equals(destination)) {
1193 return;
1194 }
1195 if (destination.IsRegister()) {
1196 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001197 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001198 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001199 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001200 } else {
1201 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001202 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001203 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001204 } else if (destination.IsFpuRegister()) {
1205 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001206 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001207 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001208 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001209 } else {
1210 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001211 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001212 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001214 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001215 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001216 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001217 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001218 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001219 } else if (source.IsConstant()) {
1220 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001221 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001222 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223 } else {
1224 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001225 __ pushl(Address(ESP, source.GetStackIndex()));
1226 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001227 }
1228 }
1229}
1230
1231void CodeGeneratorX86::Move64(Location destination, Location source) {
1232 if (source.Equals(destination)) {
1233 return;
1234 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001235 if (destination.IsRegisterPair()) {
1236 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001237 EmitParallelMoves(
1238 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1239 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001240 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001241 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001242 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1243 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001244 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001245 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1246 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1247 __ psrlq(src_reg, Immediate(32));
1248 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001250 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001251 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001252 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1253 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001254 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1255 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001256 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001257 if (source.IsFpuRegister()) {
1258 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1259 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001260 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001261 } else if (source.IsRegisterPair()) {
1262 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1263 // Create stack space for 2 elements.
1264 __ subl(ESP, Immediate(2 * elem_size));
1265 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1266 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1267 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1268 // And remove the temporary stack space we allocated.
1269 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001270 } else {
1271 LOG(FATAL) << "Unimplemented";
1272 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001273 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001274 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001275 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001276 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001277 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001278 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001279 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001280 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001281 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001282 } else if (source.IsConstant()) {
1283 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001284 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1285 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001286 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001287 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1288 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001289 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001290 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001291 EmitParallelMoves(
1292 Location::StackSlot(source.GetStackIndex()),
1293 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001294 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001295 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001296 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1297 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001298 }
1299 }
1300}
1301
Calin Juravle175dc732015-08-25 15:42:32 +01001302void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1303 DCHECK(location.IsRegister());
1304 __ movl(location.AsRegister<Register>(), Immediate(value));
1305}
1306
Calin Juravlee460d1d2015-09-29 04:52:17 +01001307void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001308 HParallelMove move(GetGraph()->GetArena());
1309 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1310 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1311 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001312 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001313 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001314 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001315 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001316}
1317
1318void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1319 if (location.IsRegister()) {
1320 locations->AddTemp(location);
1321 } else if (location.IsRegisterPair()) {
1322 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1323 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1324 } else {
1325 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1326 }
1327}
1328
David Brazdilfc6a86a2015-06-26 10:33:45 +00001329void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001330 DCHECK(!successor->IsExitBlock());
1331
1332 HBasicBlock* block = got->GetBlock();
1333 HInstruction* previous = got->GetPrevious();
1334
1335 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001336 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001337 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1338 return;
1339 }
1340
1341 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1342 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1343 }
1344 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001345 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001346 }
1347}
1348
David Brazdilfc6a86a2015-06-26 10:33:45 +00001349void LocationsBuilderX86::VisitGoto(HGoto* got) {
1350 got->SetLocations(nullptr);
1351}
1352
1353void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1354 HandleGoto(got, got->GetSuccessor());
1355}
1356
1357void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1358 try_boundary->SetLocations(nullptr);
1359}
1360
1361void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1362 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1363 if (!successor->IsExitBlock()) {
1364 HandleGoto(try_boundary, successor);
1365 }
1366}
1367
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001368void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001369 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001370}
1371
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001372void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001373}
1374
Mark Mendell152408f2015-12-31 12:28:50 -05001375template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001376void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001377 LabelType* true_label,
1378 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001379 if (cond->IsFPConditionTrueIfNaN()) {
1380 __ j(kUnordered, true_label);
1381 } else if (cond->IsFPConditionFalseIfNaN()) {
1382 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001383 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001384 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001385}
1386
Mark Mendell152408f2015-12-31 12:28:50 -05001387template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001388void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001389 LabelType* true_label,
1390 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001391 LocationSummary* locations = cond->GetLocations();
1392 Location left = locations->InAt(0);
1393 Location right = locations->InAt(1);
1394 IfCondition if_cond = cond->GetCondition();
1395
Mark Mendellc4701932015-04-10 13:18:51 -04001396 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001397 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001398 IfCondition true_high_cond = if_cond;
1399 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001400 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001401
1402 // Set the conditions for the test, remembering that == needs to be
1403 // decided using the low words.
1404 switch (if_cond) {
1405 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001406 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001407 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001408 break;
1409 case kCondLT:
1410 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001411 break;
1412 case kCondLE:
1413 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001414 break;
1415 case kCondGT:
1416 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001417 break;
1418 case kCondGE:
1419 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001420 break;
Aart Bike9f37602015-10-09 11:15:55 -07001421 case kCondB:
1422 false_high_cond = kCondA;
1423 break;
1424 case kCondBE:
1425 true_high_cond = kCondB;
1426 break;
1427 case kCondA:
1428 false_high_cond = kCondB;
1429 break;
1430 case kCondAE:
1431 true_high_cond = kCondA;
1432 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001433 }
1434
1435 if (right.IsConstant()) {
1436 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001437 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001438 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001439
Aart Bika19616e2016-02-01 18:57:58 -08001440 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001441 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001442 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001443 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001444 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001445 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001446 __ j(X86Condition(true_high_cond), true_label);
1447 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001448 }
1449 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001450 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001451 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001452 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001453 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001454
1455 __ cmpl(left_high, right_high);
1456 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001457 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001458 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001459 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001460 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001461 __ j(X86Condition(true_high_cond), true_label);
1462 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001463 }
1464 // Must be equal high, so compare the lows.
1465 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001466 } else {
1467 DCHECK(right.IsDoubleStackSlot());
1468 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1469 if (if_cond == kCondNE) {
1470 __ j(X86Condition(true_high_cond), true_label);
1471 } else if (if_cond == kCondEQ) {
1472 __ j(X86Condition(false_high_cond), false_label);
1473 } else {
1474 __ j(X86Condition(true_high_cond), true_label);
1475 __ j(X86Condition(false_high_cond), false_label);
1476 }
1477 // Must be equal high, so compare the lows.
1478 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001479 }
1480 // The last comparison might be unsigned.
1481 __ j(final_condition, true_label);
1482}
1483
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001484void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1485 Location rhs,
1486 HInstruction* insn,
1487 bool is_double) {
1488 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1489 if (is_double) {
1490 if (rhs.IsFpuRegister()) {
1491 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1492 } else if (const_area != nullptr) {
1493 DCHECK(const_area->IsEmittedAtUseSite());
1494 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1495 codegen_->LiteralDoubleAddress(
1496 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1497 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1498 } else {
1499 DCHECK(rhs.IsDoubleStackSlot());
1500 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1501 }
1502 } else {
1503 if (rhs.IsFpuRegister()) {
1504 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1505 } else if (const_area != nullptr) {
1506 DCHECK(const_area->IsEmittedAtUseSite());
1507 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1508 codegen_->LiteralFloatAddress(
1509 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1510 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1511 } else {
1512 DCHECK(rhs.IsStackSlot());
1513 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1514 }
1515 }
1516}
1517
Mark Mendell152408f2015-12-31 12:28:50 -05001518template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001519void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001520 LabelType* true_target_in,
1521 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001522 // Generated branching requires both targets to be explicit. If either of the
1523 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001524 LabelType fallthrough_target;
1525 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1526 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001527
Mark Mendellc4701932015-04-10 13:18:51 -04001528 LocationSummary* locations = condition->GetLocations();
1529 Location left = locations->InAt(0);
1530 Location right = locations->InAt(1);
1531
Mark Mendellc4701932015-04-10 13:18:51 -04001532 Primitive::Type type = condition->InputAt(0)->GetType();
1533 switch (type) {
1534 case Primitive::kPrimLong:
1535 GenerateLongComparesAndJumps(condition, true_target, false_target);
1536 break;
1537 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001538 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001539 GenerateFPJumps(condition, true_target, false_target);
1540 break;
1541 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001542 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001543 GenerateFPJumps(condition, true_target, false_target);
1544 break;
1545 default:
1546 LOG(FATAL) << "Unexpected compare type " << type;
1547 }
1548
David Brazdil0debae72015-11-12 18:37:00 +00001549 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001550 __ jmp(false_target);
1551 }
David Brazdil0debae72015-11-12 18:37:00 +00001552
1553 if (fallthrough_target.IsLinked()) {
1554 __ Bind(&fallthrough_target);
1555 }
Mark Mendellc4701932015-04-10 13:18:51 -04001556}
1557
David Brazdil0debae72015-11-12 18:37:00 +00001558static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1559 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1560 // are set only strictly before `branch`. We can't use the eflags on long/FP
1561 // conditions if they are materialized due to the complex branching.
1562 return cond->IsCondition() &&
1563 cond->GetNext() == branch &&
1564 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1565 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1566}
1567
Mark Mendell152408f2015-12-31 12:28:50 -05001568template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001569void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001570 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001571 LabelType* true_target,
1572 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001573 HInstruction* cond = instruction->InputAt(condition_input_index);
1574
1575 if (true_target == nullptr && false_target == nullptr) {
1576 // Nothing to do. The code always falls through.
1577 return;
1578 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001579 // Constant condition, statically compared against "true" (integer value 1).
1580 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001581 if (true_target != nullptr) {
1582 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001583 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001584 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001585 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001586 if (false_target != nullptr) {
1587 __ jmp(false_target);
1588 }
1589 }
1590 return;
1591 }
1592
1593 // The following code generates these patterns:
1594 // (1) true_target == nullptr && false_target != nullptr
1595 // - opposite condition true => branch to false_target
1596 // (2) true_target != nullptr && false_target == nullptr
1597 // - condition true => branch to true_target
1598 // (3) true_target != nullptr && false_target != nullptr
1599 // - condition true => branch to true_target
1600 // - branch to false_target
1601 if (IsBooleanValueOrMaterializedCondition(cond)) {
1602 if (AreEflagsSetFrom(cond, instruction)) {
1603 if (true_target == nullptr) {
1604 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1605 } else {
1606 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1607 }
1608 } else {
1609 // Materialized condition, compare against 0.
1610 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1611 if (lhs.IsRegister()) {
1612 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1613 } else {
1614 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1615 }
1616 if (true_target == nullptr) {
1617 __ j(kEqual, false_target);
1618 } else {
1619 __ j(kNotEqual, true_target);
1620 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001621 }
1622 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001623 // Condition has not been materialized, use its inputs as the comparison and
1624 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001625 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001626
1627 // If this is a long or FP comparison that has been folded into
1628 // the HCondition, generate the comparison directly.
1629 Primitive::Type type = condition->InputAt(0)->GetType();
1630 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1631 GenerateCompareTestAndBranch(condition, true_target, false_target);
1632 return;
1633 }
1634
1635 Location lhs = condition->GetLocations()->InAt(0);
1636 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001637 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001638 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001639 if (true_target == nullptr) {
1640 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1641 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001642 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001643 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001644 }
David Brazdil0debae72015-11-12 18:37:00 +00001645
1646 // If neither branch falls through (case 3), the conditional branch to `true_target`
1647 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1648 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001649 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001650 }
1651}
1652
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001653void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001654 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1655 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001656 locations->SetInAt(0, Location::Any());
1657 }
1658}
1659
1660void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001661 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1662 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1663 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1664 nullptr : codegen_->GetLabelOf(true_successor);
1665 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1666 nullptr : codegen_->GetLabelOf(false_successor);
1667 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001668}
1669
1670void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1671 LocationSummary* locations = new (GetGraph()->GetArena())
1672 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001673 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001674 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001675 locations->SetInAt(0, Location::Any());
1676 }
1677}
1678
1679void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001680 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001681 GenerateTestAndBranch<Label>(deoptimize,
1682 /* condition_input_index */ 0,
1683 slow_path->GetEntryLabel(),
1684 /* false_target */ nullptr);
1685}
1686
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001687static bool SelectCanUseCMOV(HSelect* select) {
1688 // There are no conditional move instructions for XMMs.
1689 if (Primitive::IsFloatingPointType(select->GetType())) {
1690 return false;
1691 }
1692
1693 // A FP condition doesn't generate the single CC that we need.
1694 // In 32 bit mode, a long condition doesn't generate a single CC either.
1695 HInstruction* condition = select->GetCondition();
1696 if (condition->IsCondition()) {
1697 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1698 if (compare_type == Primitive::kPrimLong ||
1699 Primitive::IsFloatingPointType(compare_type)) {
1700 return false;
1701 }
1702 }
1703
1704 // We can generate a CMOV for this Select.
1705 return true;
1706}
1707
David Brazdil74eb1b22015-12-14 11:44:01 +00001708void LocationsBuilderX86::VisitSelect(HSelect* select) {
1709 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001710 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001711 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001712 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001713 } else {
1714 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001715 if (SelectCanUseCMOV(select)) {
1716 if (select->InputAt(1)->IsConstant()) {
1717 // Cmov can't handle a constant value.
1718 locations->SetInAt(1, Location::RequiresRegister());
1719 } else {
1720 locations->SetInAt(1, Location::Any());
1721 }
1722 } else {
1723 locations->SetInAt(1, Location::Any());
1724 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001725 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001726 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1727 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001728 }
1729 locations->SetOut(Location::SameAsFirstInput());
1730}
1731
1732void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1733 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001734 DCHECK(locations->InAt(0).Equals(locations->Out()));
1735 if (SelectCanUseCMOV(select)) {
1736 // If both the condition and the source types are integer, we can generate
1737 // a CMOV to implement Select.
1738
1739 HInstruction* select_condition = select->GetCondition();
1740 Condition cond = kNotEqual;
1741
1742 // Figure out how to test the 'condition'.
1743 if (select_condition->IsCondition()) {
1744 HCondition* condition = select_condition->AsCondition();
1745 if (!condition->IsEmittedAtUseSite()) {
1746 // This was a previously materialized condition.
1747 // Can we use the existing condition code?
1748 if (AreEflagsSetFrom(condition, select)) {
1749 // Materialization was the previous instruction. Condition codes are right.
1750 cond = X86Condition(condition->GetCondition());
1751 } else {
1752 // No, we have to recreate the condition code.
1753 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1754 __ testl(cond_reg, cond_reg);
1755 }
1756 } else {
1757 // We can't handle FP or long here.
1758 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1759 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1760 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001761 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001762 cond = X86Condition(condition->GetCondition());
1763 }
1764 } else {
1765 // Must be a boolean condition, which needs to be compared to 0.
1766 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1767 __ testl(cond_reg, cond_reg);
1768 }
1769
1770 // If the condition is true, overwrite the output, which already contains false.
1771 Location false_loc = locations->InAt(0);
1772 Location true_loc = locations->InAt(1);
1773 if (select->GetType() == Primitive::kPrimLong) {
1774 // 64 bit conditional move.
1775 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1776 Register false_low = false_loc.AsRegisterPairLow<Register>();
1777 if (true_loc.IsRegisterPair()) {
1778 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1779 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1780 } else {
1781 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1782 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1783 }
1784 } else {
1785 // 32 bit conditional move.
1786 Register false_reg = false_loc.AsRegister<Register>();
1787 if (true_loc.IsRegister()) {
1788 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1789 } else {
1790 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1791 }
1792 }
1793 } else {
1794 NearLabel false_target;
1795 GenerateTestAndBranch<NearLabel>(
1796 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1797 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1798 __ Bind(&false_target);
1799 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001800}
1801
David Srbecky0cf44932015-12-09 14:09:59 +00001802void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1803 new (GetGraph()->GetArena()) LocationSummary(info);
1804}
1805
David Srbeckyd28f4a02016-03-14 17:14:24 +00001806void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1807 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001808}
1809
1810void CodeGeneratorX86::GenerateNop() {
1811 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001812}
1813
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001814void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001815 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001816 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001817 // Handle the long/FP comparisons made in instruction simplification.
1818 switch (cond->InputAt(0)->GetType()) {
1819 case Primitive::kPrimLong: {
1820 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001821 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001822 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001823 locations->SetOut(Location::RequiresRegister());
1824 }
1825 break;
1826 }
1827 case Primitive::kPrimFloat:
1828 case Primitive::kPrimDouble: {
1829 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001830 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1831 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1832 } else if (cond->InputAt(1)->IsConstant()) {
1833 locations->SetInAt(1, Location::RequiresFpuRegister());
1834 } else {
1835 locations->SetInAt(1, Location::Any());
1836 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001837 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001838 locations->SetOut(Location::RequiresRegister());
1839 }
1840 break;
1841 }
1842 default:
1843 locations->SetInAt(0, Location::RequiresRegister());
1844 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001845 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001846 // We need a byte register.
1847 locations->SetOut(Location::RegisterLocation(ECX));
1848 }
1849 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001850 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001851}
1852
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001853void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001854 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001855 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001856 }
Mark Mendellc4701932015-04-10 13:18:51 -04001857
1858 LocationSummary* locations = cond->GetLocations();
1859 Location lhs = locations->InAt(0);
1860 Location rhs = locations->InAt(1);
1861 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001862 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001863
1864 switch (cond->InputAt(0)->GetType()) {
1865 default: {
1866 // Integer case.
1867
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001868 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001869 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001870 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001871 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001872 return;
1873 }
1874 case Primitive::kPrimLong:
1875 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1876 break;
1877 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001878 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001879 GenerateFPJumps(cond, &true_label, &false_label);
1880 break;
1881 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001882 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001883 GenerateFPJumps(cond, &true_label, &false_label);
1884 break;
1885 }
1886
1887 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001888 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001889
Roland Levillain4fa13f62015-07-06 18:11:54 +01001890 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001891 __ Bind(&false_label);
1892 __ xorl(reg, reg);
1893 __ jmp(&done_label);
1894
Roland Levillain4fa13f62015-07-06 18:11:54 +01001895 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001896 __ Bind(&true_label);
1897 __ movl(reg, Immediate(1));
1898 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001899}
1900
1901void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001902 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001903}
1904
1905void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001906 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001907}
1908
1909void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001910 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001911}
1912
1913void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001914 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001915}
1916
1917void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001918 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001919}
1920
1921void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001922 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001923}
1924
1925void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001926 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001927}
1928
1929void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001930 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001931}
1932
1933void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001934 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001935}
1936
1937void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001938 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001939}
1940
1941void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001942 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001943}
1944
1945void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001946 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001947}
1948
Aart Bike9f37602015-10-09 11:15:55 -07001949void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001950 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001951}
1952
1953void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001954 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001955}
1956
1957void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001958 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001959}
1960
1961void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001962 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001963}
1964
1965void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001966 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001967}
1968
1969void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001970 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001971}
1972
1973void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001974 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001975}
1976
1977void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001978 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001979}
1980
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001981void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001982 LocationSummary* locations =
1983 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001984 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001985}
1986
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001987void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001988 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001989}
1990
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001991void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1992 LocationSummary* locations =
1993 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1994 locations->SetOut(Location::ConstantLocation(constant));
1995}
1996
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001997void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001998 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001999}
2000
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002001void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002002 LocationSummary* locations =
2003 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002004 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002005}
2006
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002007void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002008 // Will be generated at use site.
2009}
2010
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002011void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2012 LocationSummary* locations =
2013 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2014 locations->SetOut(Location::ConstantLocation(constant));
2015}
2016
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002017void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002018 // Will be generated at use site.
2019}
2020
2021void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2022 LocationSummary* locations =
2023 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2024 locations->SetOut(Location::ConstantLocation(constant));
2025}
2026
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002027void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002028 // Will be generated at use site.
2029}
2030
Calin Juravle27df7582015-04-17 19:12:31 +01002031void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2032 memory_barrier->SetLocations(nullptr);
2033}
2034
2035void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002036 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002037}
2038
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002039void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002040 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002041}
2042
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002043void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002044 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002045}
2046
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002047void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002048 LocationSummary* locations =
2049 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002050 switch (ret->InputAt(0)->GetType()) {
2051 case Primitive::kPrimBoolean:
2052 case Primitive::kPrimByte:
2053 case Primitive::kPrimChar:
2054 case Primitive::kPrimShort:
2055 case Primitive::kPrimInt:
2056 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002057 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002058 break;
2059
2060 case Primitive::kPrimLong:
2061 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002062 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002063 break;
2064
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002065 case Primitive::kPrimFloat:
2066 case Primitive::kPrimDouble:
2067 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002068 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002069 break;
2070
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002071 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002072 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002073 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002074}
2075
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002076void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002077 if (kIsDebugBuild) {
2078 switch (ret->InputAt(0)->GetType()) {
2079 case Primitive::kPrimBoolean:
2080 case Primitive::kPrimByte:
2081 case Primitive::kPrimChar:
2082 case Primitive::kPrimShort:
2083 case Primitive::kPrimInt:
2084 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002085 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002086 break;
2087
2088 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002089 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2090 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002091 break;
2092
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002093 case Primitive::kPrimFloat:
2094 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002095 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002096 break;
2097
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002098 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002099 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002100 }
2101 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002102 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002103}
2104
Calin Juravle175dc732015-08-25 15:42:32 +01002105void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2106 // The trampoline uses the same calling convention as dex calling conventions,
2107 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2108 // the method_idx.
2109 HandleInvoke(invoke);
2110}
2111
2112void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2113 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2114}
2115
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002116void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002117 // Explicit clinit checks triggered by static invokes must have been pruned by
2118 // art::PrepareForRegisterAllocation.
2119 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002120
Mark Mendellfb8d2792015-03-31 22:16:59 -04002121 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002122 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002123 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002124 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002125 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002126 return;
2127 }
2128
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002129 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002130
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002131 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2132 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002133 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002134 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002135}
2136
Mark Mendell09ed1a32015-03-25 08:30:06 -04002137static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2138 if (invoke->GetLocations()->Intrinsified()) {
2139 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2140 intrinsic.Dispatch(invoke);
2141 return true;
2142 }
2143 return false;
2144}
2145
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002146void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002147 // Explicit clinit checks triggered by static invokes must have been pruned by
2148 // art::PrepareForRegisterAllocation.
2149 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002150
Mark Mendell09ed1a32015-03-25 08:30:06 -04002151 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2152 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002153 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002154
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002155 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002156 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002157 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002158 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002159}
2160
2161void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002162 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2163 if (intrinsic.TryDispatch(invoke)) {
2164 return;
2165 }
2166
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002167 HandleInvoke(invoke);
2168}
2169
2170void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002171 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002172 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002173}
2174
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002175void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002176 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2177 return;
2178 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002179
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002180 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002181 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002182 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002183}
2184
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002185void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002186 // This call to HandleInvoke allocates a temporary (core) register
2187 // which is also used to transfer the hidden argument from FP to
2188 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002189 HandleInvoke(invoke);
2190 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002191 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002192}
2193
2194void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2195 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002196 LocationSummary* locations = invoke->GetLocations();
2197 Register temp = locations->GetTemp(0).AsRegister<Register>();
2198 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002199 Location receiver = locations->InAt(0);
2200 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2201
Roland Levillain0d5a2812015-11-13 10:07:31 +00002202 // Set the hidden argument. This is safe to do this here, as XMM7
2203 // won't be modified thereafter, before the `call` instruction.
2204 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002205 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002206 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002207
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002208 if (receiver.IsStackSlot()) {
2209 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002210 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002211 __ movl(temp, Address(temp, class_offset));
2212 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002213 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002214 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002215 }
Roland Levillain4d027112015-07-01 15:41:14 +01002216 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002217 // Instead of simply (possibly) unpoisoning `temp` here, we should
2218 // emit a read barrier for the previous class reference load.
2219 // However this is not required in practice, as this is an
2220 // intermediate/temporary reference and because the current
2221 // concurrent copying collector keeps the from-space memory
2222 // intact/accessible until the end of the marking phase (the
2223 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002224 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002225 // temp = temp->GetAddressOfIMT()
2226 __ movl(temp,
2227 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002228 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002229 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002230 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002231 __ movl(temp, Address(temp, method_offset));
2232 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002233 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002234 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002235
2236 DCHECK(!codegen_->IsLeafMethod());
2237 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2238}
2239
Roland Levillain88cb1752014-10-20 16:36:47 +01002240void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2241 LocationSummary* locations =
2242 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2243 switch (neg->GetResultType()) {
2244 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002245 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002246 locations->SetInAt(0, Location::RequiresRegister());
2247 locations->SetOut(Location::SameAsFirstInput());
2248 break;
2249
Roland Levillain88cb1752014-10-20 16:36:47 +01002250 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002251 locations->SetInAt(0, Location::RequiresFpuRegister());
2252 locations->SetOut(Location::SameAsFirstInput());
2253 locations->AddTemp(Location::RequiresRegister());
2254 locations->AddTemp(Location::RequiresFpuRegister());
2255 break;
2256
Roland Levillain88cb1752014-10-20 16:36:47 +01002257 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002258 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002259 locations->SetOut(Location::SameAsFirstInput());
2260 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002261 break;
2262
2263 default:
2264 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2265 }
2266}
2267
2268void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2269 LocationSummary* locations = neg->GetLocations();
2270 Location out = locations->Out();
2271 Location in = locations->InAt(0);
2272 switch (neg->GetResultType()) {
2273 case Primitive::kPrimInt:
2274 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002275 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002276 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002277 break;
2278
2279 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002280 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002281 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002282 __ negl(out.AsRegisterPairLow<Register>());
2283 // Negation is similar to subtraction from zero. The least
2284 // significant byte triggers a borrow when it is different from
2285 // zero; to take it into account, add 1 to the most significant
2286 // byte if the carry flag (CF) is set to 1 after the first NEGL
2287 // operation.
2288 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2289 __ negl(out.AsRegisterPairHigh<Register>());
2290 break;
2291
Roland Levillain5368c212014-11-27 15:03:41 +00002292 case Primitive::kPrimFloat: {
2293 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002294 Register constant = locations->GetTemp(0).AsRegister<Register>();
2295 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002296 // Implement float negation with an exclusive or with value
2297 // 0x80000000 (mask for bit 31, representing the sign of a
2298 // single-precision floating-point number).
2299 __ movl(constant, Immediate(INT32_C(0x80000000)));
2300 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002301 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002302 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002303 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002304
Roland Levillain5368c212014-11-27 15:03:41 +00002305 case Primitive::kPrimDouble: {
2306 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002307 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002308 // Implement double negation with an exclusive or with value
2309 // 0x8000000000000000 (mask for bit 63, representing the sign of
2310 // a double-precision floating-point number).
2311 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002312 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002313 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002314 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002315
2316 default:
2317 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2318 }
2319}
2320
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002321void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2322 LocationSummary* locations =
2323 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2324 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2325 locations->SetInAt(0, Location::RequiresFpuRegister());
2326 locations->SetInAt(1, Location::RequiresRegister());
2327 locations->SetOut(Location::SameAsFirstInput());
2328 locations->AddTemp(Location::RequiresFpuRegister());
2329}
2330
2331void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2332 LocationSummary* locations = neg->GetLocations();
2333 Location out = locations->Out();
2334 DCHECK(locations->InAt(0).Equals(out));
2335
2336 Register constant_area = locations->InAt(1).AsRegister<Register>();
2337 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2338 if (neg->GetType() == Primitive::kPrimFloat) {
2339 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2340 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2341 } else {
2342 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2343 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2344 }
2345}
2346
Roland Levillaindff1f282014-11-05 14:15:05 +00002347void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002348 Primitive::Type result_type = conversion->GetResultType();
2349 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002350 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002351
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002352 // The float-to-long and double-to-long type conversions rely on a
2353 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002354 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002355 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2356 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002357 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002358 : LocationSummary::kNoCall;
2359 LocationSummary* locations =
2360 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2361
David Brazdilb2bd1c52015-03-25 11:17:37 +00002362 // The Java language does not allow treating boolean as an integral type but
2363 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002364
Roland Levillaindff1f282014-11-05 14:15:05 +00002365 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002366 case Primitive::kPrimByte:
2367 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002368 case Primitive::kPrimLong: {
2369 // Type conversion from long to byte is a result of code transformations.
2370 HInstruction* input = conversion->InputAt(0);
2371 Location input_location = input->IsConstant()
2372 ? Location::ConstantLocation(input->AsConstant())
2373 : Location::RegisterPairLocation(EAX, EDX);
2374 locations->SetInAt(0, input_location);
2375 // Make the output overlap to please the register allocator. This greatly simplifies
2376 // the validation of the linear scan implementation
2377 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2378 break;
2379 }
David Brazdil46e2a392015-03-16 17:31:52 +00002380 case Primitive::kPrimBoolean:
2381 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002382 case Primitive::kPrimShort:
2383 case Primitive::kPrimInt:
2384 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002385 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002386 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2387 // Make the output overlap to please the register allocator. This greatly simplifies
2388 // the validation of the linear scan implementation
2389 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002390 break;
2391
2392 default:
2393 LOG(FATAL) << "Unexpected type conversion from " << input_type
2394 << " to " << result_type;
2395 }
2396 break;
2397
Roland Levillain01a8d712014-11-14 16:27:39 +00002398 case Primitive::kPrimShort:
2399 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002400 case Primitive::kPrimLong:
2401 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002402 case Primitive::kPrimBoolean:
2403 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002404 case Primitive::kPrimByte:
2405 case Primitive::kPrimInt:
2406 case Primitive::kPrimChar:
2407 // Processing a Dex `int-to-short' instruction.
2408 locations->SetInAt(0, Location::Any());
2409 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2410 break;
2411
2412 default:
2413 LOG(FATAL) << "Unexpected type conversion from " << input_type
2414 << " to " << result_type;
2415 }
2416 break;
2417
Roland Levillain946e1432014-11-11 17:35:19 +00002418 case Primitive::kPrimInt:
2419 switch (input_type) {
2420 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002421 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002422 locations->SetInAt(0, Location::Any());
2423 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2424 break;
2425
2426 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002427 // Processing a Dex `float-to-int' instruction.
2428 locations->SetInAt(0, Location::RequiresFpuRegister());
2429 locations->SetOut(Location::RequiresRegister());
2430 locations->AddTemp(Location::RequiresFpuRegister());
2431 break;
2432
Roland Levillain946e1432014-11-11 17:35:19 +00002433 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002434 // Processing a Dex `double-to-int' instruction.
2435 locations->SetInAt(0, Location::RequiresFpuRegister());
2436 locations->SetOut(Location::RequiresRegister());
2437 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002438 break;
2439
2440 default:
2441 LOG(FATAL) << "Unexpected type conversion from " << input_type
2442 << " to " << result_type;
2443 }
2444 break;
2445
Roland Levillaindff1f282014-11-05 14:15:05 +00002446 case Primitive::kPrimLong:
2447 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002448 case Primitive::kPrimBoolean:
2449 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002450 case Primitive::kPrimByte:
2451 case Primitive::kPrimShort:
2452 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002453 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002454 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002455 locations->SetInAt(0, Location::RegisterLocation(EAX));
2456 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2457 break;
2458
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002459 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002460 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002461 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002462 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002463 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2464 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2465
Vladimir Marko949c91f2015-01-27 10:48:44 +00002466 // The runtime helper puts the result in EAX, EDX.
2467 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002468 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002469 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002470
2471 default:
2472 LOG(FATAL) << "Unexpected type conversion from " << input_type
2473 << " to " << result_type;
2474 }
2475 break;
2476
Roland Levillain981e4542014-11-14 11:47:14 +00002477 case Primitive::kPrimChar:
2478 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002479 case Primitive::kPrimLong:
2480 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002481 case Primitive::kPrimBoolean:
2482 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002483 case Primitive::kPrimByte:
2484 case Primitive::kPrimShort:
2485 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002486 // Processing a Dex `int-to-char' instruction.
2487 locations->SetInAt(0, Location::Any());
2488 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2489 break;
2490
2491 default:
2492 LOG(FATAL) << "Unexpected type conversion from " << input_type
2493 << " to " << result_type;
2494 }
2495 break;
2496
Roland Levillaindff1f282014-11-05 14:15:05 +00002497 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002498 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002499 case Primitive::kPrimBoolean:
2500 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002501 case Primitive::kPrimByte:
2502 case Primitive::kPrimShort:
2503 case Primitive::kPrimInt:
2504 case Primitive::kPrimChar:
2505 // Processing a Dex `int-to-float' instruction.
2506 locations->SetInAt(0, Location::RequiresRegister());
2507 locations->SetOut(Location::RequiresFpuRegister());
2508 break;
2509
2510 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002511 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002512 locations->SetInAt(0, Location::Any());
2513 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002514 break;
2515
Roland Levillaincff13742014-11-17 14:32:17 +00002516 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002517 // Processing a Dex `double-to-float' instruction.
2518 locations->SetInAt(0, Location::RequiresFpuRegister());
2519 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002520 break;
2521
2522 default:
2523 LOG(FATAL) << "Unexpected type conversion from " << input_type
2524 << " to " << result_type;
2525 };
2526 break;
2527
Roland Levillaindff1f282014-11-05 14:15:05 +00002528 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002529 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002530 case Primitive::kPrimBoolean:
2531 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002532 case Primitive::kPrimByte:
2533 case Primitive::kPrimShort:
2534 case Primitive::kPrimInt:
2535 case Primitive::kPrimChar:
2536 // Processing a Dex `int-to-double' instruction.
2537 locations->SetInAt(0, Location::RequiresRegister());
2538 locations->SetOut(Location::RequiresFpuRegister());
2539 break;
2540
2541 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002542 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002543 locations->SetInAt(0, Location::Any());
2544 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002545 break;
2546
Roland Levillaincff13742014-11-17 14:32:17 +00002547 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002548 // Processing a Dex `float-to-double' instruction.
2549 locations->SetInAt(0, Location::RequiresFpuRegister());
2550 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002551 break;
2552
2553 default:
2554 LOG(FATAL) << "Unexpected type conversion from " << input_type
2555 << " to " << result_type;
2556 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002557 break;
2558
2559 default:
2560 LOG(FATAL) << "Unexpected type conversion from " << input_type
2561 << " to " << result_type;
2562 }
2563}
2564
2565void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2566 LocationSummary* locations = conversion->GetLocations();
2567 Location out = locations->Out();
2568 Location in = locations->InAt(0);
2569 Primitive::Type result_type = conversion->GetResultType();
2570 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002571 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002572 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002573 case Primitive::kPrimByte:
2574 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002575 case Primitive::kPrimLong:
2576 // Type conversion from long to byte is a result of code transformations.
2577 if (in.IsRegisterPair()) {
2578 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2579 } else {
2580 DCHECK(in.GetConstant()->IsLongConstant());
2581 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2582 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2583 }
2584 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002585 case Primitive::kPrimBoolean:
2586 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002587 case Primitive::kPrimShort:
2588 case Primitive::kPrimInt:
2589 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002590 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002591 if (in.IsRegister()) {
2592 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002593 } else {
2594 DCHECK(in.GetConstant()->IsIntConstant());
2595 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2596 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2597 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002598 break;
2599
2600 default:
2601 LOG(FATAL) << "Unexpected type conversion from " << input_type
2602 << " to " << result_type;
2603 }
2604 break;
2605
Roland Levillain01a8d712014-11-14 16:27:39 +00002606 case Primitive::kPrimShort:
2607 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002608 case Primitive::kPrimLong:
2609 // Type conversion from long to short is a result of code transformations.
2610 if (in.IsRegisterPair()) {
2611 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2612 } else if (in.IsDoubleStackSlot()) {
2613 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2614 } else {
2615 DCHECK(in.GetConstant()->IsLongConstant());
2616 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2617 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2618 }
2619 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002620 case Primitive::kPrimBoolean:
2621 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002622 case Primitive::kPrimByte:
2623 case Primitive::kPrimInt:
2624 case Primitive::kPrimChar:
2625 // Processing a Dex `int-to-short' instruction.
2626 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002627 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002628 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002629 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002630 } else {
2631 DCHECK(in.GetConstant()->IsIntConstant());
2632 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002633 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002634 }
2635 break;
2636
2637 default:
2638 LOG(FATAL) << "Unexpected type conversion from " << input_type
2639 << " to " << result_type;
2640 }
2641 break;
2642
Roland Levillain946e1432014-11-11 17:35:19 +00002643 case Primitive::kPrimInt:
2644 switch (input_type) {
2645 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002646 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002647 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002648 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002649 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002650 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002651 } else {
2652 DCHECK(in.IsConstant());
2653 DCHECK(in.GetConstant()->IsLongConstant());
2654 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002655 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002656 }
2657 break;
2658
Roland Levillain3f8f9362014-12-02 17:45:01 +00002659 case Primitive::kPrimFloat: {
2660 // Processing a Dex `float-to-int' instruction.
2661 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2662 Register output = out.AsRegister<Register>();
2663 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002664 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002665
2666 __ movl(output, Immediate(kPrimIntMax));
2667 // temp = int-to-float(output)
2668 __ cvtsi2ss(temp, output);
2669 // if input >= temp goto done
2670 __ comiss(input, temp);
2671 __ j(kAboveEqual, &done);
2672 // if input == NaN goto nan
2673 __ j(kUnordered, &nan);
2674 // output = float-to-int-truncate(input)
2675 __ cvttss2si(output, input);
2676 __ jmp(&done);
2677 __ Bind(&nan);
2678 // output = 0
2679 __ xorl(output, output);
2680 __ Bind(&done);
2681 break;
2682 }
2683
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002684 case Primitive::kPrimDouble: {
2685 // Processing a Dex `double-to-int' instruction.
2686 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2687 Register output = out.AsRegister<Register>();
2688 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002689 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002690
2691 __ movl(output, Immediate(kPrimIntMax));
2692 // temp = int-to-double(output)
2693 __ cvtsi2sd(temp, output);
2694 // if input >= temp goto done
2695 __ comisd(input, temp);
2696 __ j(kAboveEqual, &done);
2697 // if input == NaN goto nan
2698 __ j(kUnordered, &nan);
2699 // output = double-to-int-truncate(input)
2700 __ cvttsd2si(output, input);
2701 __ jmp(&done);
2702 __ Bind(&nan);
2703 // output = 0
2704 __ xorl(output, output);
2705 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002706 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002707 }
Roland Levillain946e1432014-11-11 17:35:19 +00002708
2709 default:
2710 LOG(FATAL) << "Unexpected type conversion from " << input_type
2711 << " to " << result_type;
2712 }
2713 break;
2714
Roland Levillaindff1f282014-11-05 14:15:05 +00002715 case Primitive::kPrimLong:
2716 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002717 case Primitive::kPrimBoolean:
2718 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002719 case Primitive::kPrimByte:
2720 case Primitive::kPrimShort:
2721 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002722 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002723 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002724 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2725 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002726 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002727 __ cdq();
2728 break;
2729
2730 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002731 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002732 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002733 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002734 break;
2735
Roland Levillaindff1f282014-11-05 14:15:05 +00002736 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002737 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002738 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002739 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002740 break;
2741
2742 default:
2743 LOG(FATAL) << "Unexpected type conversion from " << input_type
2744 << " to " << result_type;
2745 }
2746 break;
2747
Roland Levillain981e4542014-11-14 11:47:14 +00002748 case Primitive::kPrimChar:
2749 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002750 case Primitive::kPrimLong:
2751 // Type conversion from long to short is a result of code transformations.
2752 if (in.IsRegisterPair()) {
2753 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2754 } else if (in.IsDoubleStackSlot()) {
2755 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2756 } else {
2757 DCHECK(in.GetConstant()->IsLongConstant());
2758 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2759 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2760 }
2761 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002762 case Primitive::kPrimBoolean:
2763 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002764 case Primitive::kPrimByte:
2765 case Primitive::kPrimShort:
2766 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002767 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2768 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002769 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002770 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002771 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002772 } else {
2773 DCHECK(in.GetConstant()->IsIntConstant());
2774 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002775 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002776 }
2777 break;
2778
2779 default:
2780 LOG(FATAL) << "Unexpected type conversion from " << input_type
2781 << " to " << result_type;
2782 }
2783 break;
2784
Roland Levillaindff1f282014-11-05 14:15:05 +00002785 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002786 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002787 case Primitive::kPrimBoolean:
2788 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002789 case Primitive::kPrimByte:
2790 case Primitive::kPrimShort:
2791 case Primitive::kPrimInt:
2792 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002793 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002794 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002795 break;
2796
Roland Levillain6d0e4832014-11-27 18:31:21 +00002797 case Primitive::kPrimLong: {
2798 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002799 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002800
Roland Levillain232ade02015-04-20 15:14:36 +01002801 // Create stack space for the call to
2802 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2803 // TODO: enhance register allocator to ask for stack temporaries.
2804 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2805 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2806 __ subl(ESP, Immediate(adjustment));
2807 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002808
Roland Levillain232ade02015-04-20 15:14:36 +01002809 // Load the value to the FP stack, using temporaries if needed.
2810 PushOntoFPStack(in, 0, adjustment, false, true);
2811
2812 if (out.IsStackSlot()) {
2813 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2814 } else {
2815 __ fstps(Address(ESP, 0));
2816 Location stack_temp = Location::StackSlot(0);
2817 codegen_->Move32(out, stack_temp);
2818 }
2819
2820 // Remove the temporary stack space we allocated.
2821 if (adjustment != 0) {
2822 __ addl(ESP, Immediate(adjustment));
2823 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002824 break;
2825 }
2826
Roland Levillaincff13742014-11-17 14:32:17 +00002827 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002828 // Processing a Dex `double-to-float' instruction.
2829 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002830 break;
2831
2832 default:
2833 LOG(FATAL) << "Unexpected type conversion from " << input_type
2834 << " to " << result_type;
2835 };
2836 break;
2837
Roland Levillaindff1f282014-11-05 14:15:05 +00002838 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002839 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002840 case Primitive::kPrimBoolean:
2841 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002842 case Primitive::kPrimByte:
2843 case Primitive::kPrimShort:
2844 case Primitive::kPrimInt:
2845 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002846 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002847 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002848 break;
2849
Roland Levillain647b9ed2014-11-27 12:06:00 +00002850 case Primitive::kPrimLong: {
2851 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002852 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002853
Roland Levillain232ade02015-04-20 15:14:36 +01002854 // Create stack space for the call to
2855 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2856 // TODO: enhance register allocator to ask for stack temporaries.
2857 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2858 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2859 __ subl(ESP, Immediate(adjustment));
2860 }
2861
2862 // Load the value to the FP stack, using temporaries if needed.
2863 PushOntoFPStack(in, 0, adjustment, false, true);
2864
2865 if (out.IsDoubleStackSlot()) {
2866 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2867 } else {
2868 __ fstpl(Address(ESP, 0));
2869 Location stack_temp = Location::DoubleStackSlot(0);
2870 codegen_->Move64(out, stack_temp);
2871 }
2872
2873 // Remove the temporary stack space we allocated.
2874 if (adjustment != 0) {
2875 __ addl(ESP, Immediate(adjustment));
2876 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002877 break;
2878 }
2879
Roland Levillaincff13742014-11-17 14:32:17 +00002880 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002881 // Processing a Dex `float-to-double' instruction.
2882 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002883 break;
2884
2885 default:
2886 LOG(FATAL) << "Unexpected type conversion from " << input_type
2887 << " to " << result_type;
2888 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002889 break;
2890
2891 default:
2892 LOG(FATAL) << "Unexpected type conversion from " << input_type
2893 << " to " << result_type;
2894 }
2895}
2896
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002897void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002898 LocationSummary* locations =
2899 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002900 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002901 case Primitive::kPrimInt: {
2902 locations->SetInAt(0, Location::RequiresRegister());
2903 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2904 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2905 break;
2906 }
2907
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002908 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002909 locations->SetInAt(0, Location::RequiresRegister());
2910 locations->SetInAt(1, Location::Any());
2911 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002912 break;
2913 }
2914
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002915 case Primitive::kPrimFloat:
2916 case Primitive::kPrimDouble: {
2917 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002918 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2919 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002920 } else if (add->InputAt(1)->IsConstant()) {
2921 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002922 } else {
2923 locations->SetInAt(1, Location::Any());
2924 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002925 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002926 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002927 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002928
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002929 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002930 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2931 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002932 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002933}
2934
2935void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2936 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002937 Location first = locations->InAt(0);
2938 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002939 Location out = locations->Out();
2940
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002941 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002942 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002943 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002944 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2945 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002946 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2947 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002948 } else {
2949 __ leal(out.AsRegister<Register>(), Address(
2950 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2951 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002952 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002953 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2954 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2955 __ addl(out.AsRegister<Register>(), Immediate(value));
2956 } else {
2957 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2958 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002959 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002960 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002961 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002962 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002963 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002964 }
2965
2966 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002967 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002968 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2969 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002970 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002971 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2972 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002973 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002974 } else {
2975 DCHECK(second.IsConstant()) << second;
2976 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2977 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2978 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002979 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002980 break;
2981 }
2982
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002983 case Primitive::kPrimFloat: {
2984 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002985 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002986 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2987 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002988 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002989 __ addss(first.AsFpuRegister<XmmRegister>(),
2990 codegen_->LiteralFloatAddress(
2991 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2992 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2993 } else {
2994 DCHECK(second.IsStackSlot());
2995 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002996 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002997 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002998 }
2999
3000 case Primitive::kPrimDouble: {
3001 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003002 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003003 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3004 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003005 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003006 __ addsd(first.AsFpuRegister<XmmRegister>(),
3007 codegen_->LiteralDoubleAddress(
3008 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3009 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3010 } else {
3011 DCHECK(second.IsDoubleStackSlot());
3012 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003013 }
3014 break;
3015 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003016
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003017 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003018 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003019 }
3020}
3021
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003022void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003023 LocationSummary* locations =
3024 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003025 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003026 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003027 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003028 locations->SetInAt(0, Location::RequiresRegister());
3029 locations->SetInAt(1, Location::Any());
3030 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003031 break;
3032 }
Calin Juravle11351682014-10-23 15:38:15 +01003033 case Primitive::kPrimFloat:
3034 case Primitive::kPrimDouble: {
3035 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003036 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3037 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003038 } else if (sub->InputAt(1)->IsConstant()) {
3039 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003040 } else {
3041 locations->SetInAt(1, Location::Any());
3042 }
Calin Juravle11351682014-10-23 15:38:15 +01003043 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003044 break;
Calin Juravle11351682014-10-23 15:38:15 +01003045 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003046
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003047 default:
Calin Juravle11351682014-10-23 15:38:15 +01003048 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003049 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003050}
3051
3052void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3053 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003054 Location first = locations->InAt(0);
3055 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003056 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003057 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003058 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003059 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003060 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003061 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003062 __ subl(first.AsRegister<Register>(),
3063 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003064 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003065 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003066 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003067 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003068 }
3069
3070 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003071 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003072 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3073 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003074 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003075 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003076 __ sbbl(first.AsRegisterPairHigh<Register>(),
3077 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003078 } else {
3079 DCHECK(second.IsConstant()) << second;
3080 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3081 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3082 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003083 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003084 break;
3085 }
3086
Calin Juravle11351682014-10-23 15:38:15 +01003087 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003088 if (second.IsFpuRegister()) {
3089 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3090 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3091 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003092 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003093 __ subss(first.AsFpuRegister<XmmRegister>(),
3094 codegen_->LiteralFloatAddress(
3095 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3096 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3097 } else {
3098 DCHECK(second.IsStackSlot());
3099 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3100 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003101 break;
Calin Juravle11351682014-10-23 15:38:15 +01003102 }
3103
3104 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003105 if (second.IsFpuRegister()) {
3106 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3107 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3108 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003109 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003110 __ subsd(first.AsFpuRegister<XmmRegister>(),
3111 codegen_->LiteralDoubleAddress(
3112 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3113 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3114 } else {
3115 DCHECK(second.IsDoubleStackSlot());
3116 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3117 }
Calin Juravle11351682014-10-23 15:38:15 +01003118 break;
3119 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003120
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003121 default:
Calin Juravle11351682014-10-23 15:38:15 +01003122 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003123 }
3124}
3125
Calin Juravle34bacdf2014-10-07 20:23:36 +01003126void LocationsBuilderX86::VisitMul(HMul* mul) {
3127 LocationSummary* locations =
3128 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3129 switch (mul->GetResultType()) {
3130 case Primitive::kPrimInt:
3131 locations->SetInAt(0, Location::RequiresRegister());
3132 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003133 if (mul->InputAt(1)->IsIntConstant()) {
3134 // Can use 3 operand multiply.
3135 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3136 } else {
3137 locations->SetOut(Location::SameAsFirstInput());
3138 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003139 break;
3140 case Primitive::kPrimLong: {
3141 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003142 locations->SetInAt(1, Location::Any());
3143 locations->SetOut(Location::SameAsFirstInput());
3144 // Needed for imul on 32bits with 64bits output.
3145 locations->AddTemp(Location::RegisterLocation(EAX));
3146 locations->AddTemp(Location::RegisterLocation(EDX));
3147 break;
3148 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003149 case Primitive::kPrimFloat:
3150 case Primitive::kPrimDouble: {
3151 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003152 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3153 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003154 } else if (mul->InputAt(1)->IsConstant()) {
3155 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003156 } else {
3157 locations->SetInAt(1, Location::Any());
3158 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003159 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003160 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003161 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003162
3163 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003164 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003165 }
3166}
3167
3168void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3169 LocationSummary* locations = mul->GetLocations();
3170 Location first = locations->InAt(0);
3171 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003172 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003173
3174 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003175 case Primitive::kPrimInt:
3176 // The constant may have ended up in a register, so test explicitly to avoid
3177 // problems where the output may not be the same as the first operand.
3178 if (mul->InputAt(1)->IsIntConstant()) {
3179 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3180 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3181 } else if (second.IsRegister()) {
3182 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003183 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003184 } else {
3185 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003186 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003187 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003188 }
3189 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003190
3191 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003192 Register in1_hi = first.AsRegisterPairHigh<Register>();
3193 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003194 Register eax = locations->GetTemp(0).AsRegister<Register>();
3195 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003196
3197 DCHECK_EQ(EAX, eax);
3198 DCHECK_EQ(EDX, edx);
3199
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003200 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003201 // output: in1
3202 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3203 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3204 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003205 if (second.IsConstant()) {
3206 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003207
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003208 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3209 int32_t low_value = Low32Bits(value);
3210 int32_t high_value = High32Bits(value);
3211 Immediate low(low_value);
3212 Immediate high(high_value);
3213
3214 __ movl(eax, high);
3215 // eax <- in1.lo * in2.hi
3216 __ imull(eax, in1_lo);
3217 // in1.hi <- in1.hi * in2.lo
3218 __ imull(in1_hi, low);
3219 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3220 __ addl(in1_hi, eax);
3221 // move in2_lo to eax to prepare for double precision
3222 __ movl(eax, low);
3223 // edx:eax <- in1.lo * in2.lo
3224 __ mull(in1_lo);
3225 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3226 __ addl(in1_hi, edx);
3227 // in1.lo <- (in1.lo * in2.lo)[31:0];
3228 __ movl(in1_lo, eax);
3229 } else if (second.IsRegisterPair()) {
3230 Register in2_hi = second.AsRegisterPairHigh<Register>();
3231 Register in2_lo = second.AsRegisterPairLow<Register>();
3232
3233 __ movl(eax, in2_hi);
3234 // eax <- in1.lo * in2.hi
3235 __ imull(eax, in1_lo);
3236 // in1.hi <- in1.hi * in2.lo
3237 __ imull(in1_hi, in2_lo);
3238 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3239 __ addl(in1_hi, eax);
3240 // move in1_lo to eax to prepare for double precision
3241 __ movl(eax, in1_lo);
3242 // edx:eax <- in1.lo * in2.lo
3243 __ mull(in2_lo);
3244 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3245 __ addl(in1_hi, edx);
3246 // in1.lo <- (in1.lo * in2.lo)[31:0];
3247 __ movl(in1_lo, eax);
3248 } else {
3249 DCHECK(second.IsDoubleStackSlot()) << second;
3250 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3251 Address in2_lo(ESP, second.GetStackIndex());
3252
3253 __ movl(eax, in2_hi);
3254 // eax <- in1.lo * in2.hi
3255 __ imull(eax, in1_lo);
3256 // in1.hi <- in1.hi * in2.lo
3257 __ imull(in1_hi, in2_lo);
3258 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3259 __ addl(in1_hi, eax);
3260 // move in1_lo to eax to prepare for double precision
3261 __ movl(eax, in1_lo);
3262 // edx:eax <- in1.lo * in2.lo
3263 __ mull(in2_lo);
3264 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3265 __ addl(in1_hi, edx);
3266 // in1.lo <- (in1.lo * in2.lo)[31:0];
3267 __ movl(in1_lo, eax);
3268 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003269
3270 break;
3271 }
3272
Calin Juravleb5bfa962014-10-21 18:02:24 +01003273 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003274 DCHECK(first.Equals(locations->Out()));
3275 if (second.IsFpuRegister()) {
3276 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3277 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3278 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003279 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003280 __ mulss(first.AsFpuRegister<XmmRegister>(),
3281 codegen_->LiteralFloatAddress(
3282 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3283 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3284 } else {
3285 DCHECK(second.IsStackSlot());
3286 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3287 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003288 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003289 }
3290
3291 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003292 DCHECK(first.Equals(locations->Out()));
3293 if (second.IsFpuRegister()) {
3294 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3295 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3296 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003297 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003298 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3299 codegen_->LiteralDoubleAddress(
3300 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3301 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3302 } else {
3303 DCHECK(second.IsDoubleStackSlot());
3304 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3305 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003306 break;
3307 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003308
3309 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003310 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003311 }
3312}
3313
Roland Levillain232ade02015-04-20 15:14:36 +01003314void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3315 uint32_t temp_offset,
3316 uint32_t stack_adjustment,
3317 bool is_fp,
3318 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003319 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003320 DCHECK(!is_wide);
3321 if (is_fp) {
3322 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3323 } else {
3324 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3325 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003326 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003327 DCHECK(is_wide);
3328 if (is_fp) {
3329 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3330 } else {
3331 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3332 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003333 } else {
3334 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003335 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003336 Location stack_temp = Location::StackSlot(temp_offset);
3337 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003338 if (is_fp) {
3339 __ flds(Address(ESP, temp_offset));
3340 } else {
3341 __ filds(Address(ESP, temp_offset));
3342 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003343 } else {
3344 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3345 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003346 if (is_fp) {
3347 __ fldl(Address(ESP, temp_offset));
3348 } else {
3349 __ fildl(Address(ESP, temp_offset));
3350 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003351 }
3352 }
3353}
3354
3355void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3356 Primitive::Type type = rem->GetResultType();
3357 bool is_float = type == Primitive::kPrimFloat;
3358 size_t elem_size = Primitive::ComponentSize(type);
3359 LocationSummary* locations = rem->GetLocations();
3360 Location first = locations->InAt(0);
3361 Location second = locations->InAt(1);
3362 Location out = locations->Out();
3363
3364 // Create stack space for 2 elements.
3365 // TODO: enhance register allocator to ask for stack temporaries.
3366 __ subl(ESP, Immediate(2 * elem_size));
3367
3368 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003369 const bool is_wide = !is_float;
3370 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3371 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003372
3373 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003374 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003375 __ Bind(&retry);
3376 __ fprem();
3377
3378 // Move FP status to AX.
3379 __ fstsw();
3380
3381 // And see if the argument reduction is complete. This is signaled by the
3382 // C2 FPU flag bit set to 0.
3383 __ andl(EAX, Immediate(kC2ConditionMask));
3384 __ j(kNotEqual, &retry);
3385
3386 // We have settled on the final value. Retrieve it into an XMM register.
3387 // Store FP top of stack to real stack.
3388 if (is_float) {
3389 __ fsts(Address(ESP, 0));
3390 } else {
3391 __ fstl(Address(ESP, 0));
3392 }
3393
3394 // Pop the 2 items from the FP stack.
3395 __ fucompp();
3396
3397 // Load the value from the stack into an XMM register.
3398 DCHECK(out.IsFpuRegister()) << out;
3399 if (is_float) {
3400 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3401 } else {
3402 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3403 }
3404
3405 // And remove the temporary stack space we allocated.
3406 __ addl(ESP, Immediate(2 * elem_size));
3407}
3408
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003409
3410void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3411 DCHECK(instruction->IsDiv() || instruction->IsRem());
3412
3413 LocationSummary* locations = instruction->GetLocations();
3414 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003415 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003416
3417 Register out_register = locations->Out().AsRegister<Register>();
3418 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003419 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003420
3421 DCHECK(imm == 1 || imm == -1);
3422
3423 if (instruction->IsRem()) {
3424 __ xorl(out_register, out_register);
3425 } else {
3426 __ movl(out_register, input_register);
3427 if (imm == -1) {
3428 __ negl(out_register);
3429 }
3430 }
3431}
3432
3433
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003434void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003435 LocationSummary* locations = instruction->GetLocations();
3436
3437 Register out_register = locations->Out().AsRegister<Register>();
3438 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003439 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003440 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3441 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003442
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003443 Register num = locations->GetTemp(0).AsRegister<Register>();
3444
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003445 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003446 __ testl(input_register, input_register);
3447 __ cmovl(kGreaterEqual, num, input_register);
3448 int shift = CTZ(imm);
3449 __ sarl(num, Immediate(shift));
3450
3451 if (imm < 0) {
3452 __ negl(num);
3453 }
3454
3455 __ movl(out_register, num);
3456}
3457
3458void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3459 DCHECK(instruction->IsDiv() || instruction->IsRem());
3460
3461 LocationSummary* locations = instruction->GetLocations();
3462 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3463
3464 Register eax = locations->InAt(0).AsRegister<Register>();
3465 Register out = locations->Out().AsRegister<Register>();
3466 Register num;
3467 Register edx;
3468
3469 if (instruction->IsDiv()) {
3470 edx = locations->GetTemp(0).AsRegister<Register>();
3471 num = locations->GetTemp(1).AsRegister<Register>();
3472 } else {
3473 edx = locations->Out().AsRegister<Register>();
3474 num = locations->GetTemp(0).AsRegister<Register>();
3475 }
3476
3477 DCHECK_EQ(EAX, eax);
3478 DCHECK_EQ(EDX, edx);
3479 if (instruction->IsDiv()) {
3480 DCHECK_EQ(EAX, out);
3481 } else {
3482 DCHECK_EQ(EDX, out);
3483 }
3484
3485 int64_t magic;
3486 int shift;
3487 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3488
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003489 // Save the numerator.
3490 __ movl(num, eax);
3491
3492 // EAX = magic
3493 __ movl(eax, Immediate(magic));
3494
3495 // EDX:EAX = magic * numerator
3496 __ imull(num);
3497
3498 if (imm > 0 && magic < 0) {
3499 // EDX += num
3500 __ addl(edx, num);
3501 } else if (imm < 0 && magic > 0) {
3502 __ subl(edx, num);
3503 }
3504
3505 // Shift if needed.
3506 if (shift != 0) {
3507 __ sarl(edx, Immediate(shift));
3508 }
3509
3510 // EDX += 1 if EDX < 0
3511 __ movl(eax, edx);
3512 __ shrl(edx, Immediate(31));
3513 __ addl(edx, eax);
3514
3515 if (instruction->IsRem()) {
3516 __ movl(eax, num);
3517 __ imull(edx, Immediate(imm));
3518 __ subl(eax, edx);
3519 __ movl(edx, eax);
3520 } else {
3521 __ movl(eax, edx);
3522 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003523}
3524
Calin Juravlebacfec32014-11-14 15:54:36 +00003525void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3526 DCHECK(instruction->IsDiv() || instruction->IsRem());
3527
3528 LocationSummary* locations = instruction->GetLocations();
3529 Location out = locations->Out();
3530 Location first = locations->InAt(0);
3531 Location second = locations->InAt(1);
3532 bool is_div = instruction->IsDiv();
3533
3534 switch (instruction->GetResultType()) {
3535 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003536 DCHECK_EQ(EAX, first.AsRegister<Register>());
3537 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003538
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003539 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003540 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003541
3542 if (imm == 0) {
3543 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3544 } else if (imm == 1 || imm == -1) {
3545 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003546 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003547 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003548 } else {
3549 DCHECK(imm <= -2 || imm >= 2);
3550 GenerateDivRemWithAnyConstant(instruction);
3551 }
3552 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003553 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3554 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003555 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003556
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003557 Register second_reg = second.AsRegister<Register>();
3558 // 0x80000000/-1 triggers an arithmetic exception!
3559 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3560 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003561
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003562 __ cmpl(second_reg, Immediate(-1));
3563 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003564
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003565 // edx:eax <- sign-extended of eax
3566 __ cdq();
3567 // eax = quotient, edx = remainder
3568 __ idivl(second_reg);
3569 __ Bind(slow_path->GetExitLabel());
3570 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003571 break;
3572 }
3573
3574 case Primitive::kPrimLong: {
3575 InvokeRuntimeCallingConvention calling_convention;
3576 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3577 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3578 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3579 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3580 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3581 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3582
3583 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003584 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003585 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003586 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003587 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003588 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003589 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003590 break;
3591 }
3592
3593 default:
3594 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3595 }
3596}
3597
Calin Juravle7c4954d2014-10-28 16:57:40 +00003598void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003599 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003600 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003601 : LocationSummary::kNoCall;
3602 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3603
Calin Juravle7c4954d2014-10-28 16:57:40 +00003604 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003605 case Primitive::kPrimInt: {
3606 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003607 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003608 locations->SetOut(Location::SameAsFirstInput());
3609 // Intel uses edx:eax as the dividend.
3610 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003611 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3612 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3613 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003614 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003615 locations->AddTemp(Location::RequiresRegister());
3616 }
Calin Juravled0d48522014-11-04 16:40:20 +00003617 break;
3618 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003619 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003620 InvokeRuntimeCallingConvention calling_convention;
3621 locations->SetInAt(0, Location::RegisterPairLocation(
3622 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3623 locations->SetInAt(1, Location::RegisterPairLocation(
3624 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3625 // Runtime helper puts the result in EAX, EDX.
3626 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003627 break;
3628 }
3629 case Primitive::kPrimFloat:
3630 case Primitive::kPrimDouble: {
3631 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003632 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3633 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003634 } else if (div->InputAt(1)->IsConstant()) {
3635 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003636 } else {
3637 locations->SetInAt(1, Location::Any());
3638 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003639 locations->SetOut(Location::SameAsFirstInput());
3640 break;
3641 }
3642
3643 default:
3644 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3645 }
3646}
3647
3648void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3649 LocationSummary* locations = div->GetLocations();
3650 Location first = locations->InAt(0);
3651 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003652
3653 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003654 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003655 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003656 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003657 break;
3658 }
3659
3660 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003661 if (second.IsFpuRegister()) {
3662 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3663 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3664 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003665 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003666 __ divss(first.AsFpuRegister<XmmRegister>(),
3667 codegen_->LiteralFloatAddress(
3668 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3669 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3670 } else {
3671 DCHECK(second.IsStackSlot());
3672 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3673 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003674 break;
3675 }
3676
3677 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003678 if (second.IsFpuRegister()) {
3679 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3680 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3681 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003682 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003683 __ divsd(first.AsFpuRegister<XmmRegister>(),
3684 codegen_->LiteralDoubleAddress(
3685 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3686 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3687 } else {
3688 DCHECK(second.IsDoubleStackSlot());
3689 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3690 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003691 break;
3692 }
3693
3694 default:
3695 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3696 }
3697}
3698
Calin Juravlebacfec32014-11-14 15:54:36 +00003699void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003700 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003701
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003702 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003703 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003704 : LocationSummary::kNoCall;
3705 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003706
Calin Juravled2ec87d2014-12-08 14:24:46 +00003707 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003708 case Primitive::kPrimInt: {
3709 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003710 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003711 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003712 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3713 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3714 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003715 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003716 locations->AddTemp(Location::RequiresRegister());
3717 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003718 break;
3719 }
3720 case Primitive::kPrimLong: {
3721 InvokeRuntimeCallingConvention calling_convention;
3722 locations->SetInAt(0, Location::RegisterPairLocation(
3723 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3724 locations->SetInAt(1, Location::RegisterPairLocation(
3725 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3726 // Runtime helper puts the result in EAX, EDX.
3727 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3728 break;
3729 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003730 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003731 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003732 locations->SetInAt(0, Location::Any());
3733 locations->SetInAt(1, Location::Any());
3734 locations->SetOut(Location::RequiresFpuRegister());
3735 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003736 break;
3737 }
3738
3739 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003740 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003741 }
3742}
3743
3744void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3745 Primitive::Type type = rem->GetResultType();
3746 switch (type) {
3747 case Primitive::kPrimInt:
3748 case Primitive::kPrimLong: {
3749 GenerateDivRemIntegral(rem);
3750 break;
3751 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003752 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003753 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003754 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003755 break;
3756 }
3757 default:
3758 LOG(FATAL) << "Unexpected rem type " << type;
3759 }
3760}
3761
Calin Juravled0d48522014-11-04 16:40:20 +00003762void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003763 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003764 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003765 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003766 case Primitive::kPrimByte:
3767 case Primitive::kPrimChar:
3768 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003769 case Primitive::kPrimInt: {
3770 locations->SetInAt(0, Location::Any());
3771 break;
3772 }
3773 case Primitive::kPrimLong: {
3774 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3775 if (!instruction->IsConstant()) {
3776 locations->AddTemp(Location::RequiresRegister());
3777 }
3778 break;
3779 }
3780 default:
3781 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3782 }
Calin Juravled0d48522014-11-04 16:40:20 +00003783}
3784
3785void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003786 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003787 codegen_->AddSlowPath(slow_path);
3788
3789 LocationSummary* locations = instruction->GetLocations();
3790 Location value = locations->InAt(0);
3791
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003792 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003793 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003794 case Primitive::kPrimByte:
3795 case Primitive::kPrimChar:
3796 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003797 case Primitive::kPrimInt: {
3798 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003799 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003800 __ j(kEqual, slow_path->GetEntryLabel());
3801 } else if (value.IsStackSlot()) {
3802 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3803 __ j(kEqual, slow_path->GetEntryLabel());
3804 } else {
3805 DCHECK(value.IsConstant()) << value;
3806 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003807 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003808 }
3809 }
3810 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003811 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003812 case Primitive::kPrimLong: {
3813 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003814 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003815 __ movl(temp, value.AsRegisterPairLow<Register>());
3816 __ orl(temp, value.AsRegisterPairHigh<Register>());
3817 __ j(kEqual, slow_path->GetEntryLabel());
3818 } else {
3819 DCHECK(value.IsConstant()) << value;
3820 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3821 __ jmp(slow_path->GetEntryLabel());
3822 }
3823 }
3824 break;
3825 }
3826 default:
3827 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003828 }
Calin Juravled0d48522014-11-04 16:40:20 +00003829}
3830
Calin Juravle9aec02f2014-11-18 23:06:35 +00003831void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3832 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3833
3834 LocationSummary* locations =
3835 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3836
3837 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003838 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003839 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003840 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003841 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003842 // The shift count needs to be in CL or a constant.
3843 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003844 locations->SetOut(Location::SameAsFirstInput());
3845 break;
3846 }
3847 default:
3848 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3849 }
3850}
3851
3852void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3853 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3854
3855 LocationSummary* locations = op->GetLocations();
3856 Location first = locations->InAt(0);
3857 Location second = locations->InAt(1);
3858 DCHECK(first.Equals(locations->Out()));
3859
3860 switch (op->GetResultType()) {
3861 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003862 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003863 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003864 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003865 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003866 DCHECK_EQ(ECX, second_reg);
3867 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003868 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003869 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003870 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003871 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003872 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003873 }
3874 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003875 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003876 if (shift == 0) {
3877 return;
3878 }
3879 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003880 if (op->IsShl()) {
3881 __ shll(first_reg, imm);
3882 } else if (op->IsShr()) {
3883 __ sarl(first_reg, imm);
3884 } else {
3885 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003886 }
3887 }
3888 break;
3889 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003890 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003891 if (second.IsRegister()) {
3892 Register second_reg = second.AsRegister<Register>();
3893 DCHECK_EQ(ECX, second_reg);
3894 if (op->IsShl()) {
3895 GenerateShlLong(first, second_reg);
3896 } else if (op->IsShr()) {
3897 GenerateShrLong(first, second_reg);
3898 } else {
3899 GenerateUShrLong(first, second_reg);
3900 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003901 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003902 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003903 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003904 // Nothing to do if the shift is 0, as the input is already the output.
3905 if (shift != 0) {
3906 if (op->IsShl()) {
3907 GenerateShlLong(first, shift);
3908 } else if (op->IsShr()) {
3909 GenerateShrLong(first, shift);
3910 } else {
3911 GenerateUShrLong(first, shift);
3912 }
3913 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003914 }
3915 break;
3916 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003917 default:
3918 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3919 }
3920}
3921
Mark P Mendell73945692015-04-29 14:56:17 +00003922void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3923 Register low = loc.AsRegisterPairLow<Register>();
3924 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003925 if (shift == 1) {
3926 // This is just an addition.
3927 __ addl(low, low);
3928 __ adcl(high, high);
3929 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003930 // Shift by 32 is easy. High gets low, and low gets 0.
3931 codegen_->EmitParallelMoves(
3932 loc.ToLow(),
3933 loc.ToHigh(),
3934 Primitive::kPrimInt,
3935 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3936 loc.ToLow(),
3937 Primitive::kPrimInt);
3938 } else if (shift > 32) {
3939 // Low part becomes 0. High part is low part << (shift-32).
3940 __ movl(high, low);
3941 __ shll(high, Immediate(shift - 32));
3942 __ xorl(low, low);
3943 } else {
3944 // Between 1 and 31.
3945 __ shld(high, low, Immediate(shift));
3946 __ shll(low, Immediate(shift));
3947 }
3948}
3949
Calin Juravle9aec02f2014-11-18 23:06:35 +00003950void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003951 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003952 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3953 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3954 __ testl(shifter, Immediate(32));
3955 __ j(kEqual, &done);
3956 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3957 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3958 __ Bind(&done);
3959}
3960
Mark P Mendell73945692015-04-29 14:56:17 +00003961void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3962 Register low = loc.AsRegisterPairLow<Register>();
3963 Register high = loc.AsRegisterPairHigh<Register>();
3964 if (shift == 32) {
3965 // Need to copy the sign.
3966 DCHECK_NE(low, high);
3967 __ movl(low, high);
3968 __ sarl(high, Immediate(31));
3969 } else if (shift > 32) {
3970 DCHECK_NE(low, high);
3971 // High part becomes sign. Low part is shifted by shift - 32.
3972 __ movl(low, high);
3973 __ sarl(high, Immediate(31));
3974 __ sarl(low, Immediate(shift - 32));
3975 } else {
3976 // Between 1 and 31.
3977 __ shrd(low, high, Immediate(shift));
3978 __ sarl(high, Immediate(shift));
3979 }
3980}
3981
Calin Juravle9aec02f2014-11-18 23:06:35 +00003982void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003983 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003984 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3985 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3986 __ testl(shifter, Immediate(32));
3987 __ j(kEqual, &done);
3988 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3989 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3990 __ Bind(&done);
3991}
3992
Mark P Mendell73945692015-04-29 14:56:17 +00003993void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3994 Register low = loc.AsRegisterPairLow<Register>();
3995 Register high = loc.AsRegisterPairHigh<Register>();
3996 if (shift == 32) {
3997 // Shift by 32 is easy. Low gets high, and high gets 0.
3998 codegen_->EmitParallelMoves(
3999 loc.ToHigh(),
4000 loc.ToLow(),
4001 Primitive::kPrimInt,
4002 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4003 loc.ToHigh(),
4004 Primitive::kPrimInt);
4005 } else if (shift > 32) {
4006 // Low part is high >> (shift - 32). High part becomes 0.
4007 __ movl(low, high);
4008 __ shrl(low, Immediate(shift - 32));
4009 __ xorl(high, high);
4010 } else {
4011 // Between 1 and 31.
4012 __ shrd(low, high, Immediate(shift));
4013 __ shrl(high, Immediate(shift));
4014 }
4015}
4016
Calin Juravle9aec02f2014-11-18 23:06:35 +00004017void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004018 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004019 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4020 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4021 __ testl(shifter, Immediate(32));
4022 __ j(kEqual, &done);
4023 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4024 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4025 __ Bind(&done);
4026}
4027
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004028void LocationsBuilderX86::VisitRor(HRor* ror) {
4029 LocationSummary* locations =
4030 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4031
4032 switch (ror->GetResultType()) {
4033 case Primitive::kPrimLong:
4034 // Add the temporary needed.
4035 locations->AddTemp(Location::RequiresRegister());
4036 FALLTHROUGH_INTENDED;
4037 case Primitive::kPrimInt:
4038 locations->SetInAt(0, Location::RequiresRegister());
4039 // The shift count needs to be in CL (unless it is a constant).
4040 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4041 locations->SetOut(Location::SameAsFirstInput());
4042 break;
4043 default:
4044 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4045 UNREACHABLE();
4046 }
4047}
4048
4049void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4050 LocationSummary* locations = ror->GetLocations();
4051 Location first = locations->InAt(0);
4052 Location second = locations->InAt(1);
4053
4054 if (ror->GetResultType() == Primitive::kPrimInt) {
4055 Register first_reg = first.AsRegister<Register>();
4056 if (second.IsRegister()) {
4057 Register second_reg = second.AsRegister<Register>();
4058 __ rorl(first_reg, second_reg);
4059 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004060 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004061 __ rorl(first_reg, imm);
4062 }
4063 return;
4064 }
4065
4066 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4067 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4068 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4069 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4070 if (second.IsRegister()) {
4071 Register second_reg = second.AsRegister<Register>();
4072 DCHECK_EQ(second_reg, ECX);
4073 __ movl(temp_reg, first_reg_hi);
4074 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4075 __ shrd(first_reg_lo, temp_reg, second_reg);
4076 __ movl(temp_reg, first_reg_hi);
4077 __ testl(second_reg, Immediate(32));
4078 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4079 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4080 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004081 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004082 if (shift_amt == 0) {
4083 // Already fine.
4084 return;
4085 }
4086 if (shift_amt == 32) {
4087 // Just swap.
4088 __ movl(temp_reg, first_reg_lo);
4089 __ movl(first_reg_lo, first_reg_hi);
4090 __ movl(first_reg_hi, temp_reg);
4091 return;
4092 }
4093
4094 Immediate imm(shift_amt);
4095 // Save the constents of the low value.
4096 __ movl(temp_reg, first_reg_lo);
4097
4098 // Shift right into low, feeding bits from high.
4099 __ shrd(first_reg_lo, first_reg_hi, imm);
4100
4101 // Shift right into high, feeding bits from the original low.
4102 __ shrd(first_reg_hi, temp_reg, imm);
4103
4104 // Swap if needed.
4105 if (shift_amt > 32) {
4106 __ movl(temp_reg, first_reg_lo);
4107 __ movl(first_reg_lo, first_reg_hi);
4108 __ movl(first_reg_hi, temp_reg);
4109 }
4110 }
4111}
4112
Calin Juravle9aec02f2014-11-18 23:06:35 +00004113void LocationsBuilderX86::VisitShl(HShl* shl) {
4114 HandleShift(shl);
4115}
4116
4117void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4118 HandleShift(shl);
4119}
4120
4121void LocationsBuilderX86::VisitShr(HShr* shr) {
4122 HandleShift(shr);
4123}
4124
4125void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4126 HandleShift(shr);
4127}
4128
4129void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4130 HandleShift(ushr);
4131}
4132
4133void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4134 HandleShift(ushr);
4135}
4136
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004137void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004138 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004139 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004140 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004141 if (instruction->IsStringAlloc()) {
4142 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4143 } else {
4144 InvokeRuntimeCallingConvention calling_convention;
4145 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4146 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4147 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004148}
4149
4150void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004151 // Note: if heap poisoning is enabled, the entry point takes cares
4152 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004153 if (instruction->IsStringAlloc()) {
4154 // String is allocated through StringFactory. Call NewEmptyString entry point.
4155 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004156 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004157 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4158 __ call(Address(temp, code_offset.Int32Value()));
4159 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4160 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004161 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004162 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4163 DCHECK(!codegen_->IsLeafMethod());
4164 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004165}
4166
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004167void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4168 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004169 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004170 locations->SetOut(Location::RegisterLocation(EAX));
4171 InvokeRuntimeCallingConvention calling_convention;
4172 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004173 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004174 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004175}
4176
4177void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4178 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004179 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004180 // Note: if heap poisoning is enabled, the entry point takes cares
4181 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004182 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004183 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004184 DCHECK(!codegen_->IsLeafMethod());
4185}
4186
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004187void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004188 LocationSummary* locations =
4189 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004190 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4191 if (location.IsStackSlot()) {
4192 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4193 } else if (location.IsDoubleStackSlot()) {
4194 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004195 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004196 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004197}
4198
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004199void InstructionCodeGeneratorX86::VisitParameterValue(
4200 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4201}
4202
4203void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4204 LocationSummary* locations =
4205 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4206 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4207}
4208
4209void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004210}
4211
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004212void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4213 LocationSummary* locations =
4214 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4215 locations->SetInAt(0, Location::RequiresRegister());
4216 locations->SetOut(Location::RequiresRegister());
4217}
4218
4219void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4220 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004221 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004222 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004223 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004224 __ movl(locations->Out().AsRegister<Register>(),
4225 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004226 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004227 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004228 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004229 __ movl(locations->Out().AsRegister<Register>(),
4230 Address(locations->InAt(0).AsRegister<Register>(),
4231 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4232 // temp = temp->GetImtEntryAt(method_offset);
4233 __ movl(locations->Out().AsRegister<Register>(),
4234 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004235 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004236}
4237
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004238void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004239 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004240 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004241 locations->SetInAt(0, Location::RequiresRegister());
4242 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004243}
4244
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004245void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4246 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004247 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004248 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004249 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004250 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004251 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004252 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004253 break;
4254
4255 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004256 __ notl(out.AsRegisterPairLow<Register>());
4257 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004258 break;
4259
4260 default:
4261 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4262 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004263}
4264
David Brazdil66d126e2015-04-03 16:02:44 +01004265void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4266 LocationSummary* locations =
4267 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4268 locations->SetInAt(0, Location::RequiresRegister());
4269 locations->SetOut(Location::SameAsFirstInput());
4270}
4271
4272void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004273 LocationSummary* locations = bool_not->GetLocations();
4274 Location in = locations->InAt(0);
4275 Location out = locations->Out();
4276 DCHECK(in.Equals(out));
4277 __ xorl(out.AsRegister<Register>(), Immediate(1));
4278}
4279
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004280void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004281 LocationSummary* locations =
4282 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004283 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004284 case Primitive::kPrimBoolean:
4285 case Primitive::kPrimByte:
4286 case Primitive::kPrimShort:
4287 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004288 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004289 case Primitive::kPrimLong: {
4290 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004291 locations->SetInAt(1, Location::Any());
4292 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4293 break;
4294 }
4295 case Primitive::kPrimFloat:
4296 case Primitive::kPrimDouble: {
4297 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004298 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4299 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4300 } else if (compare->InputAt(1)->IsConstant()) {
4301 locations->SetInAt(1, Location::RequiresFpuRegister());
4302 } else {
4303 locations->SetInAt(1, Location::Any());
4304 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004305 locations->SetOut(Location::RequiresRegister());
4306 break;
4307 }
4308 default:
4309 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4310 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004311}
4312
4313void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004314 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004315 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004316 Location left = locations->InAt(0);
4317 Location right = locations->InAt(1);
4318
Mark Mendell0c9497d2015-08-21 09:30:05 -04004319 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004320 Condition less_cond = kLess;
4321
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004322 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004323 case Primitive::kPrimBoolean:
4324 case Primitive::kPrimByte:
4325 case Primitive::kPrimShort:
4326 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004327 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004328 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004329 break;
4330 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004331 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004332 Register left_low = left.AsRegisterPairLow<Register>();
4333 Register left_high = left.AsRegisterPairHigh<Register>();
4334 int32_t val_low = 0;
4335 int32_t val_high = 0;
4336 bool right_is_const = false;
4337
4338 if (right.IsConstant()) {
4339 DCHECK(right.GetConstant()->IsLongConstant());
4340 right_is_const = true;
4341 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4342 val_low = Low32Bits(val);
4343 val_high = High32Bits(val);
4344 }
4345
Calin Juravleddb7df22014-11-25 20:56:51 +00004346 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004347 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004348 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004349 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004350 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004351 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004352 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004353 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004354 __ j(kLess, &less); // Signed compare.
4355 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004356 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004357 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004358 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004359 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004360 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004361 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004362 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004363 }
Aart Bika19616e2016-02-01 18:57:58 -08004364 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004365 break;
4366 }
4367 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004368 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004369 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004370 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004371 break;
4372 }
4373 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004374 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004375 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004376 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004377 break;
4378 }
4379 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004380 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004381 }
Aart Bika19616e2016-02-01 18:57:58 -08004382
Calin Juravleddb7df22014-11-25 20:56:51 +00004383 __ movl(out, Immediate(0));
4384 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004385 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004386
4387 __ Bind(&greater);
4388 __ movl(out, Immediate(1));
4389 __ jmp(&done);
4390
4391 __ Bind(&less);
4392 __ movl(out, Immediate(-1));
4393
4394 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004395}
4396
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004397void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004398 LocationSummary* locations =
4399 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004400 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004401 locations->SetInAt(i, Location::Any());
4402 }
4403 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004404}
4405
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004406void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004407 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004408}
4409
Roland Levillain7c1559a2015-12-15 10:55:36 +00004410void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004411 /*
4412 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4413 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4414 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4415 */
4416 switch (kind) {
4417 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004418 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004419 break;
4420 }
4421 case MemBarrierKind::kAnyStore:
4422 case MemBarrierKind::kLoadAny:
4423 case MemBarrierKind::kStoreStore: {
4424 // nop
4425 break;
4426 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004427 case MemBarrierKind::kNTStoreStore:
4428 // Non-Temporal Store/Store needs an explicit fence.
4429 MemoryFence(/* non-temporal */ true);
4430 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004431 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004432}
4433
Vladimir Markodc151b22015-10-15 18:02:30 +01004434HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4435 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004436 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004437 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4438
4439 // We disable pc-relative load when there is an irreducible loop, as the optimization
4440 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004441 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4442 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004443 if (GetGraph()->HasIrreducibleLoops() &&
4444 (dispatch_info.method_load_kind ==
4445 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4446 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4447 }
4448 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004449 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4450 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4451 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4452 // (Though the direct CALL ptr16:32 is available for consideration).
4453 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004454 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004455 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004456 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004457 0u
4458 };
4459 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004460 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004461 }
4462}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004463
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004464Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4465 Register temp) {
4466 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004467 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004468 if (!invoke->GetLocations()->Intrinsified()) {
4469 return location.AsRegister<Register>();
4470 }
4471 // For intrinsics we allow any location, so it may be on the stack.
4472 if (!location.IsRegister()) {
4473 __ movl(temp, Address(ESP, location.GetStackIndex()));
4474 return temp;
4475 }
4476 // For register locations, check if the register was saved. If so, get it from the stack.
4477 // Note: There is a chance that the register was saved but not overwritten, so we could
4478 // save one load. However, since this is just an intrinsic slow path we prefer this
4479 // simple and more robust approach rather that trying to determine if that's the case.
4480 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004481 if (slow_path != nullptr) {
4482 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4483 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4484 __ movl(temp, Address(ESP, stack_offset));
4485 return temp;
4486 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004487 }
4488 return location.AsRegister<Register>();
4489}
4490
Serguei Katkov288c7a82016-05-16 11:53:15 +06004491Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4492 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004493 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4494 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004495 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004496 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004497 uint32_t offset =
4498 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4499 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004500 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004501 }
Vladimir Marko58155012015-08-19 12:49:41 +00004502 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004503 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004504 break;
4505 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4506 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4507 break;
4508 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004509 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004510 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4511 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004512 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4513 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004514 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4515 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4516 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004517 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004518 // Bind a new fixup label at the end of the "movl" insn.
4519 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004520 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004521 break;
4522 }
Vladimir Marko58155012015-08-19 12:49:41 +00004523 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004524 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004525 Register method_reg;
4526 Register reg = temp.AsRegister<Register>();
4527 if (current_method.IsRegister()) {
4528 method_reg = current_method.AsRegister<Register>();
4529 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004530 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004531 DCHECK(!current_method.IsValid());
4532 method_reg = reg;
4533 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4534 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004535 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004536 __ movl(reg, Address(method_reg,
4537 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004538 // temp = temp[index_in_cache];
4539 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4540 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004541 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4542 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004543 }
Vladimir Marko58155012015-08-19 12:49:41 +00004544 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004545 return callee_method;
4546}
4547
4548void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4549 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004550
4551 switch (invoke->GetCodePtrLocation()) {
4552 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4553 __ call(GetFrameEntryLabel());
4554 break;
4555 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004556 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4557 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004558 Label* label = &relative_call_patches_.back().label;
4559 __ call(label); // Bind to the patch label, override at link time.
4560 __ Bind(label); // Bind the label at the end of the "call" insn.
4561 break;
4562 }
4563 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4564 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004565 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4566 LOG(FATAL) << "Unsupported";
4567 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004568 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4569 // (callee_method + offset_of_quick_compiled_code)()
4570 __ call(Address(callee_method.AsRegister<Register>(),
4571 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004572 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004573 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004574 }
4575
4576 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004577}
4578
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004579void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4580 Register temp = temp_in.AsRegister<Register>();
4581 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4582 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004583
4584 // Use the calling convention instead of the location of the receiver, as
4585 // intrinsics may have put the receiver in a different register. In the intrinsics
4586 // slow path, the arguments have been moved to the right place, so here we are
4587 // guaranteed that the receiver is the first register of the calling convention.
4588 InvokeDexCallingConvention calling_convention;
4589 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004590 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004591 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004592 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004593 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004594 // Instead of simply (possibly) unpoisoning `temp` here, we should
4595 // emit a read barrier for the previous class reference load.
4596 // However this is not required in practice, as this is an
4597 // intermediate/temporary reference and because the current
4598 // concurrent copying collector keeps the from-space memory
4599 // intact/accessible until the end of the marking phase (the
4600 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004601 __ MaybeUnpoisonHeapReference(temp);
4602 // temp = temp->GetMethodAt(method_offset);
4603 __ movl(temp, Address(temp, method_offset));
4604 // call temp->GetEntryPoint();
4605 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004606 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004607}
4608
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004609void CodeGeneratorX86::RecordSimplePatch() {
4610 if (GetCompilerOptions().GetIncludePatchInformation()) {
4611 simple_patches_.emplace_back();
4612 __ Bind(&simple_patches_.back());
4613 }
4614}
4615
Vladimir Markoaad75c62016-10-03 08:46:48 +00004616void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4617 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004618 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4619 __ Bind(&string_patches_.back().label);
4620}
4621
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004622void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4623 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4624 __ Bind(&type_patches_.back().label);
4625}
4626
Vladimir Markoaad75c62016-10-03 08:46:48 +00004627Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4628 DCHECK(!GetCompilerOptions().IsBootImage());
4629 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4630 return &string_patches_.back().label;
4631}
4632
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004633Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4634 uint32_t element_offset) {
4635 // Add the patch entry and bind its label at the end of the instruction.
4636 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4637 return &pc_relative_dex_cache_patches_.back().label;
4638}
4639
Vladimir Markoaad75c62016-10-03 08:46:48 +00004640// The label points to the end of the "movl" or another instruction but the literal offset
4641// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4642constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4643
4644template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4645inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
4646 const ArenaDeque<PatchInfo<Label>>& infos,
4647 ArenaVector<LinkerPatch>* linker_patches) {
4648 for (const PatchInfo<Label>& info : infos) {
4649 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4650 linker_patches->push_back(
4651 Factory(literal_offset, &info.dex_file, GetMethodAddressOffset(), info.index));
4652 }
4653}
4654
Vladimir Marko58155012015-08-19 12:49:41 +00004655void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4656 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004657 size_t size =
4658 method_patches_.size() +
4659 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004660 pc_relative_dex_cache_patches_.size() +
4661 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004662 string_patches_.size() +
4663 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004664 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004665 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004666 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004667 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004668 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004669 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004670 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004671 linker_patches->push_back(
4672 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004673 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004674 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4675 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004676 for (const Label& label : simple_patches_) {
4677 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4678 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4679 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004680 if (!GetCompilerOptions().IsBootImage()) {
4681 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4682 } else if (GetCompilerOptions().GetCompilePic()) {
4683 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004684 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004685 for (const PatchInfo<Label>& info : string_patches_) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004686 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004687 linker_patches->push_back(
4688 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004689 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004690 }
4691 if (GetCompilerOptions().GetCompilePic()) {
4692 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
4693 } else {
4694 for (const PatchInfo<Label>& info : type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004695 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004696 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004697 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004698 }
Vladimir Marko58155012015-08-19 12:49:41 +00004699}
4700
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004701void CodeGeneratorX86::MarkGCCard(Register temp,
4702 Register card,
4703 Register object,
4704 Register value,
4705 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004706 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004707 if (value_can_be_null) {
4708 __ testl(value, value);
4709 __ j(kEqual, &is_null);
4710 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004711 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004712 __ movl(temp, object);
4713 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004714 __ movb(Address(temp, card, TIMES_1, 0),
4715 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004716 if (value_can_be_null) {
4717 __ Bind(&is_null);
4718 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004719}
4720
Calin Juravle52c48962014-12-16 17:02:57 +00004721void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4722 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004723
4724 bool object_field_get_with_read_barrier =
4725 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004726 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004727 new (GetGraph()->GetArena()) LocationSummary(instruction,
4728 kEmitCompilerReadBarrier ?
4729 LocationSummary::kCallOnSlowPath :
4730 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004731 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004732 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004733 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004734 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004735
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004736 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4737 locations->SetOut(Location::RequiresFpuRegister());
4738 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004739 // The output overlaps in case of long: we don't want the low move
4740 // to overwrite the object's location. Likewise, in the case of
4741 // an object field get with read barriers enabled, we do not want
4742 // the move to overwrite the object's location, as we need it to emit
4743 // the read barrier.
4744 locations->SetOut(
4745 Location::RequiresRegister(),
4746 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4747 Location::kOutputOverlap :
4748 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004749 }
Calin Juravle52c48962014-12-16 17:02:57 +00004750
4751 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4752 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004753 // So we use an XMM register as a temp to achieve atomicity (first
4754 // load the temp into the XMM and then copy the XMM into the
4755 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004756 locations->AddTemp(Location::RequiresFpuRegister());
4757 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004758}
4759
Calin Juravle52c48962014-12-16 17:02:57 +00004760void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4761 const FieldInfo& field_info) {
4762 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004763
Calin Juravle52c48962014-12-16 17:02:57 +00004764 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004765 Location base_loc = locations->InAt(0);
4766 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004767 Location out = locations->Out();
4768 bool is_volatile = field_info.IsVolatile();
4769 Primitive::Type field_type = field_info.GetFieldType();
4770 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4771
4772 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004773 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004774 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004775 break;
4776 }
4777
4778 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004779 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004780 break;
4781 }
4782
4783 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004784 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004785 break;
4786 }
4787
4788 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004789 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004790 break;
4791 }
4792
4793 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004794 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004795 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004796
4797 case Primitive::kPrimNot: {
4798 // /* HeapReference<Object> */ out = *(base + offset)
4799 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004800 // Note that a potential implicit null check is handled in this
4801 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4802 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004803 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004804 if (is_volatile) {
4805 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4806 }
4807 } else {
4808 __ movl(out.AsRegister<Register>(), Address(base, offset));
4809 codegen_->MaybeRecordImplicitNullCheck(instruction);
4810 if (is_volatile) {
4811 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4812 }
4813 // If read barriers are enabled, emit read barriers other than
4814 // Baker's using a slow path (and also unpoison the loaded
4815 // reference, if heap poisoning is enabled).
4816 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4817 }
4818 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004819 }
4820
4821 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004822 if (is_volatile) {
4823 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4824 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004825 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004826 __ movd(out.AsRegisterPairLow<Register>(), temp);
4827 __ psrlq(temp, Immediate(32));
4828 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4829 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004830 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004831 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004832 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004833 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4834 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004835 break;
4836 }
4837
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004838 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004839 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004840 break;
4841 }
4842
4843 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004844 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004845 break;
4846 }
4847
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004848 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004849 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004850 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004851 }
Calin Juravle52c48962014-12-16 17:02:57 +00004852
Roland Levillain7c1559a2015-12-15 10:55:36 +00004853 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4854 // Potential implicit null checks, in the case of reference or
4855 // long fields, are handled in the previous switch statement.
4856 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004857 codegen_->MaybeRecordImplicitNullCheck(instruction);
4858 }
4859
Calin Juravle52c48962014-12-16 17:02:57 +00004860 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004861 if (field_type == Primitive::kPrimNot) {
4862 // Memory barriers, in the case of references, are also handled
4863 // in the previous switch statement.
4864 } else {
4865 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4866 }
Roland Levillain4d027112015-07-01 15:41:14 +01004867 }
Calin Juravle52c48962014-12-16 17:02:57 +00004868}
4869
4870void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4871 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4872
4873 LocationSummary* locations =
4874 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4875 locations->SetInAt(0, Location::RequiresRegister());
4876 bool is_volatile = field_info.IsVolatile();
4877 Primitive::Type field_type = field_info.GetFieldType();
4878 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4879 || (field_type == Primitive::kPrimByte);
4880
4881 // The register allocator does not support multiple
4882 // inputs that die at entry with one in a specific register.
4883 if (is_byte_type) {
4884 // Ensure the value is in a byte register.
4885 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004886 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004887 if (is_volatile && field_type == Primitive::kPrimDouble) {
4888 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4889 locations->SetInAt(1, Location::RequiresFpuRegister());
4890 } else {
4891 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4892 }
4893 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4894 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004895 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004896
Calin Juravle52c48962014-12-16 17:02:57 +00004897 // 64bits value can be atomically written to an address with movsd and an XMM register.
4898 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4899 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4900 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4901 // isolated cases when we need this it isn't worth adding the extra complexity.
4902 locations->AddTemp(Location::RequiresFpuRegister());
4903 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004904 } else {
4905 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4906
4907 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4908 // Temporary registers for the write barrier.
4909 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4910 // Ensure the card is in a byte register.
4911 locations->AddTemp(Location::RegisterLocation(ECX));
4912 }
Calin Juravle52c48962014-12-16 17:02:57 +00004913 }
4914}
4915
4916void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004917 const FieldInfo& field_info,
4918 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004919 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4920
4921 LocationSummary* locations = instruction->GetLocations();
4922 Register base = locations->InAt(0).AsRegister<Register>();
4923 Location value = locations->InAt(1);
4924 bool is_volatile = field_info.IsVolatile();
4925 Primitive::Type field_type = field_info.GetFieldType();
4926 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004927 bool needs_write_barrier =
4928 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004929
4930 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004931 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004932 }
4933
Mark Mendell81489372015-11-04 11:30:41 -05004934 bool maybe_record_implicit_null_check_done = false;
4935
Calin Juravle52c48962014-12-16 17:02:57 +00004936 switch (field_type) {
4937 case Primitive::kPrimBoolean:
4938 case Primitive::kPrimByte: {
4939 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4940 break;
4941 }
4942
4943 case Primitive::kPrimShort:
4944 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004945 if (value.IsConstant()) {
4946 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4947 __ movw(Address(base, offset), Immediate(v));
4948 } else {
4949 __ movw(Address(base, offset), value.AsRegister<Register>());
4950 }
Calin Juravle52c48962014-12-16 17:02:57 +00004951 break;
4952 }
4953
4954 case Primitive::kPrimInt:
4955 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004956 if (kPoisonHeapReferences && needs_write_barrier) {
4957 // Note that in the case where `value` is a null reference,
4958 // we do not enter this block, as the reference does not
4959 // need poisoning.
4960 DCHECK_EQ(field_type, Primitive::kPrimNot);
4961 Register temp = locations->GetTemp(0).AsRegister<Register>();
4962 __ movl(temp, value.AsRegister<Register>());
4963 __ PoisonHeapReference(temp);
4964 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004965 } else if (value.IsConstant()) {
4966 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4967 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004968 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004969 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004970 __ movl(Address(base, offset), value.AsRegister<Register>());
4971 }
Calin Juravle52c48962014-12-16 17:02:57 +00004972 break;
4973 }
4974
4975 case Primitive::kPrimLong: {
4976 if (is_volatile) {
4977 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4978 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4979 __ movd(temp1, value.AsRegisterPairLow<Register>());
4980 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4981 __ punpckldq(temp1, temp2);
4982 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004983 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004984 } else if (value.IsConstant()) {
4985 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4986 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4987 codegen_->MaybeRecordImplicitNullCheck(instruction);
4988 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004989 } else {
4990 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004991 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004992 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4993 }
Mark Mendell81489372015-11-04 11:30:41 -05004994 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004995 break;
4996 }
4997
4998 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004999 if (value.IsConstant()) {
5000 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5001 __ movl(Address(base, offset), Immediate(v));
5002 } else {
5003 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5004 }
Calin Juravle52c48962014-12-16 17:02:57 +00005005 break;
5006 }
5007
5008 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005009 if (value.IsConstant()) {
5010 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5011 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5012 codegen_->MaybeRecordImplicitNullCheck(instruction);
5013 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5014 maybe_record_implicit_null_check_done = true;
5015 } else {
5016 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5017 }
Calin Juravle52c48962014-12-16 17:02:57 +00005018 break;
5019 }
5020
5021 case Primitive::kPrimVoid:
5022 LOG(FATAL) << "Unreachable type " << field_type;
5023 UNREACHABLE();
5024 }
5025
Mark Mendell81489372015-11-04 11:30:41 -05005026 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005027 codegen_->MaybeRecordImplicitNullCheck(instruction);
5028 }
5029
Roland Levillain4d027112015-07-01 15:41:14 +01005030 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005031 Register temp = locations->GetTemp(0).AsRegister<Register>();
5032 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005033 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005034 }
5035
Calin Juravle52c48962014-12-16 17:02:57 +00005036 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005037 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005038 }
5039}
5040
5041void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5042 HandleFieldGet(instruction, instruction->GetFieldInfo());
5043}
5044
5045void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5046 HandleFieldGet(instruction, instruction->GetFieldInfo());
5047}
5048
5049void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5050 HandleFieldSet(instruction, instruction->GetFieldInfo());
5051}
5052
5053void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005054 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005055}
5056
5057void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5058 HandleFieldSet(instruction, instruction->GetFieldInfo());
5059}
5060
5061void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005062 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005063}
5064
5065void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5066 HandleFieldGet(instruction, instruction->GetFieldInfo());
5067}
5068
5069void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5070 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005071}
5072
Calin Juravlee460d1d2015-09-29 04:52:17 +01005073void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5074 HUnresolvedInstanceFieldGet* instruction) {
5075 FieldAccessCallingConventionX86 calling_convention;
5076 codegen_->CreateUnresolvedFieldLocationSummary(
5077 instruction, instruction->GetFieldType(), calling_convention);
5078}
5079
5080void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5081 HUnresolvedInstanceFieldGet* instruction) {
5082 FieldAccessCallingConventionX86 calling_convention;
5083 codegen_->GenerateUnresolvedFieldAccess(instruction,
5084 instruction->GetFieldType(),
5085 instruction->GetFieldIndex(),
5086 instruction->GetDexPc(),
5087 calling_convention);
5088}
5089
5090void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5091 HUnresolvedInstanceFieldSet* instruction) {
5092 FieldAccessCallingConventionX86 calling_convention;
5093 codegen_->CreateUnresolvedFieldLocationSummary(
5094 instruction, instruction->GetFieldType(), calling_convention);
5095}
5096
5097void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5098 HUnresolvedInstanceFieldSet* instruction) {
5099 FieldAccessCallingConventionX86 calling_convention;
5100 codegen_->GenerateUnresolvedFieldAccess(instruction,
5101 instruction->GetFieldType(),
5102 instruction->GetFieldIndex(),
5103 instruction->GetDexPc(),
5104 calling_convention);
5105}
5106
5107void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5108 HUnresolvedStaticFieldGet* instruction) {
5109 FieldAccessCallingConventionX86 calling_convention;
5110 codegen_->CreateUnresolvedFieldLocationSummary(
5111 instruction, instruction->GetFieldType(), calling_convention);
5112}
5113
5114void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5115 HUnresolvedStaticFieldGet* instruction) {
5116 FieldAccessCallingConventionX86 calling_convention;
5117 codegen_->GenerateUnresolvedFieldAccess(instruction,
5118 instruction->GetFieldType(),
5119 instruction->GetFieldIndex(),
5120 instruction->GetDexPc(),
5121 calling_convention);
5122}
5123
5124void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5125 HUnresolvedStaticFieldSet* instruction) {
5126 FieldAccessCallingConventionX86 calling_convention;
5127 codegen_->CreateUnresolvedFieldLocationSummary(
5128 instruction, instruction->GetFieldType(), calling_convention);
5129}
5130
5131void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5132 HUnresolvedStaticFieldSet* instruction) {
5133 FieldAccessCallingConventionX86 calling_convention;
5134 codegen_->GenerateUnresolvedFieldAccess(instruction,
5135 instruction->GetFieldType(),
5136 instruction->GetFieldIndex(),
5137 instruction->GetDexPc(),
5138 calling_convention);
5139}
5140
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005141void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005142 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5143 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5144 ? Location::RequiresRegister()
5145 : Location::Any();
5146 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005147}
5148
Calin Juravle2ae48182016-03-16 14:05:09 +00005149void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5150 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005151 return;
5152 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005153 LocationSummary* locations = instruction->GetLocations();
5154 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005155
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005156 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005157 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005158}
5159
Calin Juravle2ae48182016-03-16 14:05:09 +00005160void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005161 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005162 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005163
5164 LocationSummary* locations = instruction->GetLocations();
5165 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005166
5167 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005168 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005169 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005170 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005171 } else {
5172 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005173 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005174 __ jmp(slow_path->GetEntryLabel());
5175 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005176 }
5177 __ j(kEqual, slow_path->GetEntryLabel());
5178}
5179
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005180void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005181 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005182}
5183
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005184void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005185 bool object_array_get_with_read_barrier =
5186 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005187 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005188 new (GetGraph()->GetArena()) LocationSummary(instruction,
5189 object_array_get_with_read_barrier ?
5190 LocationSummary::kCallOnSlowPath :
5191 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005192 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005193 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005194 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005195 locations->SetInAt(0, Location::RequiresRegister());
5196 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005197 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5198 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5199 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005200 // The output overlaps in case of long: we don't want the low move
5201 // to overwrite the array's location. Likewise, in the case of an
5202 // object array get with read barriers enabled, we do not want the
5203 // move to overwrite the array's location, as we need it to emit
5204 // the read barrier.
5205 locations->SetOut(
5206 Location::RequiresRegister(),
5207 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5208 Location::kOutputOverlap :
5209 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005210 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005211}
5212
5213void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5214 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005215 Location obj_loc = locations->InAt(0);
5216 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005217 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005218 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005219 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005220
Calin Juravle77520bc2015-01-12 18:45:46 +00005221 Primitive::Type type = instruction->GetType();
5222 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005223 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005224 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005225 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005226 break;
5227 }
5228
5229 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005230 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005231 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005232 break;
5233 }
5234
5235 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005236 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005237 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005238 break;
5239 }
5240
5241 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005242 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005243 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5244 // Branch cases into compressed and uncompressed for each index's type.
5245 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5246 NearLabel done, not_compressed;
5247 __ cmpl(Address(obj, count_offset), Immediate(0));
5248 codegen_->MaybeRecordImplicitNullCheck(instruction);
5249 __ j(kGreaterEqual, &not_compressed);
5250 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5251 __ jmp(&done);
5252 __ Bind(&not_compressed);
5253 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5254 __ Bind(&done);
5255 } else {
5256 // Common case for charAt of array of char or when string compression's
5257 // feature is turned off.
5258 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5259 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 break;
5261 }
5262
Roland Levillain7c1559a2015-12-15 10:55:36 +00005263 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005264 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005265 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005266 break;
5267 }
5268
Roland Levillain7c1559a2015-12-15 10:55:36 +00005269 case Primitive::kPrimNot: {
5270 static_assert(
5271 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5272 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005273 // /* HeapReference<Object> */ out =
5274 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5275 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005276 // Note that a potential implicit null check is handled in this
5277 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5278 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005279 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005280 } else {
5281 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005282 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5283 codegen_->MaybeRecordImplicitNullCheck(instruction);
5284 // If read barriers are enabled, emit read barriers other than
5285 // Baker's using a slow path (and also unpoison the loaded
5286 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005287 if (index.IsConstant()) {
5288 uint32_t offset =
5289 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005290 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5291 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005292 codegen_->MaybeGenerateReadBarrierSlow(
5293 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5294 }
5295 }
5296 break;
5297 }
5298
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005300 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005301 __ movl(out_loc.AsRegisterPairLow<Register>(),
5302 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5303 codegen_->MaybeRecordImplicitNullCheck(instruction);
5304 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5305 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005306 break;
5307 }
5308
Mark Mendell7c8d0092015-01-26 11:21:33 -05005309 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005310 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005311 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005312 break;
5313 }
5314
5315 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005316 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005317 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005318 break;
5319 }
5320
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005321 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005322 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005323 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005324 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005325
Roland Levillain7c1559a2015-12-15 10:55:36 +00005326 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5327 // Potential implicit null checks, in the case of reference or
5328 // long arrays, are handled in the previous switch statement.
5329 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005330 codegen_->MaybeRecordImplicitNullCheck(instruction);
5331 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005332}
5333
5334void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005335 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005336
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005337 bool needs_write_barrier =
5338 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005339 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005340
Nicolas Geoffray39468442014-09-02 15:17:15 +01005341 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5342 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005343 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005344 LocationSummary::kCallOnSlowPath :
5345 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005346
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005347 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5348 || (value_type == Primitive::kPrimByte);
5349 // We need the inputs to be different than the output in case of long operation.
5350 // In case of a byte operation, the register allocator does not support multiple
5351 // inputs that die at entry with one in a specific register.
5352 locations->SetInAt(0, Location::RequiresRegister());
5353 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5354 if (is_byte_type) {
5355 // Ensure the value is in a byte register.
5356 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5357 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005358 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005359 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005360 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5361 }
5362 if (needs_write_barrier) {
5363 // Temporary registers for the write barrier.
5364 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5365 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005366 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005367 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005368}
5369
5370void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5371 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005372 Location array_loc = locations->InAt(0);
5373 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005375 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005376 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005377 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5378 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5379 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005380 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005381 bool needs_write_barrier =
5382 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005383
5384 switch (value_type) {
5385 case Primitive::kPrimBoolean:
5386 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005387 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005388 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005389 if (value.IsRegister()) {
5390 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005391 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005392 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005393 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005394 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005395 break;
5396 }
5397
5398 case Primitive::kPrimShort:
5399 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005400 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005401 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005402 if (value.IsRegister()) {
5403 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005404 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005405 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005406 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005407 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005408 break;
5409 }
5410
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005411 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005412 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005413 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005414
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005415 if (!value.IsRegister()) {
5416 // Just setting null.
5417 DCHECK(instruction->InputAt(2)->IsNullConstant());
5418 DCHECK(value.IsConstant()) << value;
5419 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005420 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005421 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005422 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005423 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005424 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005425
5426 DCHECK(needs_write_barrier);
5427 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005428 // We cannot use a NearLabel for `done`, as its range may be too
5429 // short when Baker read barriers are enabled.
5430 Label done;
5431 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005432 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005433 Location temp_loc = locations->GetTemp(0);
5434 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005435 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005436 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5437 codegen_->AddSlowPath(slow_path);
5438 if (instruction->GetValueCanBeNull()) {
5439 __ testl(register_value, register_value);
5440 __ j(kNotEqual, &not_null);
5441 __ movl(address, Immediate(0));
5442 codegen_->MaybeRecordImplicitNullCheck(instruction);
5443 __ jmp(&done);
5444 __ Bind(&not_null);
5445 }
5446
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005447 // Note that when Baker read barriers are enabled, the type
5448 // checks are performed without read barriers. This is fine,
5449 // even in the case where a class object is in the from-space
5450 // after the flip, as a comparison involving such a type would
5451 // not produce a false positive; it may of course produce a
5452 // false negative, in which case we would take the ArraySet
5453 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005454
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005455 // /* HeapReference<Class> */ temp = array->klass_
5456 __ movl(temp, Address(array, class_offset));
5457 codegen_->MaybeRecordImplicitNullCheck(instruction);
5458 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005459
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005460 // /* HeapReference<Class> */ temp = temp->component_type_
5461 __ movl(temp, Address(temp, component_offset));
5462 // If heap poisoning is enabled, no need to unpoison `temp`
5463 // nor the object reference in `register_value->klass`, as
5464 // we are comparing two poisoned references.
5465 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005466
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005467 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5468 __ j(kEqual, &do_put);
5469 // If heap poisoning is enabled, the `temp` reference has
5470 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005471 __ MaybeUnpoisonHeapReference(temp);
5472
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005473 // If heap poisoning is enabled, no need to unpoison the
5474 // heap reference loaded below, as it is only used for a
5475 // comparison with null.
5476 __ cmpl(Address(temp, super_offset), Immediate(0));
5477 __ j(kNotEqual, slow_path->GetEntryLabel());
5478 __ Bind(&do_put);
5479 } else {
5480 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005481 }
5482 }
5483
5484 if (kPoisonHeapReferences) {
5485 __ movl(temp, register_value);
5486 __ PoisonHeapReference(temp);
5487 __ movl(address, temp);
5488 } else {
5489 __ movl(address, register_value);
5490 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005491 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005492 codegen_->MaybeRecordImplicitNullCheck(instruction);
5493 }
5494
5495 Register card = locations->GetTemp(1).AsRegister<Register>();
5496 codegen_->MarkGCCard(
5497 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5498 __ Bind(&done);
5499
5500 if (slow_path != nullptr) {
5501 __ Bind(slow_path->GetExitLabel());
5502 }
5503
5504 break;
5505 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005506
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005507 case Primitive::kPrimInt: {
5508 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005509 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005510 if (value.IsRegister()) {
5511 __ movl(address, value.AsRegister<Register>());
5512 } else {
5513 DCHECK(value.IsConstant()) << value;
5514 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5515 __ movl(address, Immediate(v));
5516 }
5517 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005518 break;
5519 }
5520
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005521 case Primitive::kPrimLong: {
5522 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005523 if (value.IsRegisterPair()) {
5524 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5525 value.AsRegisterPairLow<Register>());
5526 codegen_->MaybeRecordImplicitNullCheck(instruction);
5527 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5528 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005529 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005530 DCHECK(value.IsConstant());
5531 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5532 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5533 Immediate(Low32Bits(val)));
5534 codegen_->MaybeRecordImplicitNullCheck(instruction);
5535 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5536 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005537 }
5538 break;
5539 }
5540
Mark Mendell7c8d0092015-01-26 11:21:33 -05005541 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005542 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005543 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005544 if (value.IsFpuRegister()) {
5545 __ movss(address, value.AsFpuRegister<XmmRegister>());
5546 } else {
5547 DCHECK(value.IsConstant());
5548 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5549 __ movl(address, Immediate(v));
5550 }
5551 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005552 break;
5553 }
5554
5555 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005556 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005557 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005558 if (value.IsFpuRegister()) {
5559 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5560 } else {
5561 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005562 Address address_hi =
5563 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005564 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5565 __ movl(address, Immediate(Low32Bits(v)));
5566 codegen_->MaybeRecordImplicitNullCheck(instruction);
5567 __ movl(address_hi, Immediate(High32Bits(v)));
5568 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005569 break;
5570 }
5571
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005572 case Primitive::kPrimVoid:
5573 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005574 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005575 }
5576}
5577
5578void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5579 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005580 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005581 if (!instruction->IsEmittedAtUseSite()) {
5582 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5583 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005584}
5585
5586void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005587 if (instruction->IsEmittedAtUseSite()) {
5588 return;
5589 }
5590
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005591 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005592 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005593 Register obj = locations->InAt(0).AsRegister<Register>();
5594 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005595 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005596 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005597 // Mask out most significant bit in case the array is String's array of char.
5598 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
5599 __ andl(out, Immediate(INT32_MAX));
5600 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005601}
5602
5603void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005604 RegisterSet caller_saves = RegisterSet::Empty();
5605 InvokeRuntimeCallingConvention calling_convention;
5606 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5607 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5608 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005609 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005610 HInstruction* length = instruction->InputAt(1);
5611 if (!length->IsEmittedAtUseSite()) {
5612 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5613 }
jessicahandojo4877b792016-09-08 19:49:13 -07005614 // Need register to see array's length.
5615 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5616 locations->AddTemp(Location::RequiresRegister());
5617 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005618}
5619
5620void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005621 const bool is_string_compressed_char_at =
5622 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005623 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005624 Location index_loc = locations->InAt(0);
5625 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005626 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005627 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005628
Mark Mendell99dbd682015-04-22 16:18:52 -04005629 if (length_loc.IsConstant()) {
5630 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5631 if (index_loc.IsConstant()) {
5632 // BCE will remove the bounds check if we are guarenteed to pass.
5633 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5634 if (index < 0 || index >= length) {
5635 codegen_->AddSlowPath(slow_path);
5636 __ jmp(slow_path->GetEntryLabel());
5637 } else {
5638 // Some optimization after BCE may have generated this, and we should not
5639 // generate a bounds check if it is a valid range.
5640 }
5641 return;
5642 }
5643
5644 // We have to reverse the jump condition because the length is the constant.
5645 Register index_reg = index_loc.AsRegister<Register>();
5646 __ cmpl(index_reg, Immediate(length));
5647 codegen_->AddSlowPath(slow_path);
5648 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005649 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005650 HInstruction* array_length = instruction->InputAt(1);
5651 if (array_length->IsEmittedAtUseSite()) {
5652 // Address the length field in the array.
5653 DCHECK(array_length->IsArrayLength());
5654 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5655 Location array_loc = array_length->GetLocations()->InAt(0);
5656 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005657 if (is_string_compressed_char_at) {
5658 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5659 __ movl(length_reg, array_len);
5660 codegen_->MaybeRecordImplicitNullCheck(array_length);
5661 __ andl(length_reg, Immediate(INT32_MAX));
5662 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005663 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005664 // Checking bounds for general case:
5665 // Array of char or string's array with feature compression off.
5666 if (index_loc.IsConstant()) {
5667 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5668 __ cmpl(array_len, Immediate(value));
5669 } else {
5670 __ cmpl(array_len, index_loc.AsRegister<Register>());
5671 }
5672 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005673 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005674 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005675 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005676 }
5677 codegen_->AddSlowPath(slow_path);
5678 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005679 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005680}
5681
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005682void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005683 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005684}
5685
5686void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005687 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5688}
5689
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005690void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005691 LocationSummary* locations =
5692 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005693 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005694}
5695
5696void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005697 HBasicBlock* block = instruction->GetBlock();
5698 if (block->GetLoopInformation() != nullptr) {
5699 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5700 // The back edge will generate the suspend check.
5701 return;
5702 }
5703 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5704 // The goto will generate the suspend check.
5705 return;
5706 }
5707 GenerateSuspendCheck(instruction, nullptr);
5708}
5709
5710void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5711 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005712 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005713 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5714 if (slow_path == nullptr) {
5715 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5716 instruction->SetSlowPath(slow_path);
5717 codegen_->AddSlowPath(slow_path);
5718 if (successor != nullptr) {
5719 DCHECK(successor->IsLoopHeader());
5720 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5721 }
5722 } else {
5723 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5724 }
5725
Andreas Gampe542451c2016-07-26 09:02:02 -07005726 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005727 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005728 if (successor == nullptr) {
5729 __ j(kNotEqual, slow_path->GetEntryLabel());
5730 __ Bind(slow_path->GetReturnLabel());
5731 } else {
5732 __ j(kEqual, codegen_->GetLabelOf(successor));
5733 __ jmp(slow_path->GetEntryLabel());
5734 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005735}
5736
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005737X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5738 return codegen_->GetAssembler();
5739}
5740
Mark Mendell7c8d0092015-01-26 11:21:33 -05005741void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005742 ScratchRegisterScope ensure_scratch(
5743 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5744 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5745 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5746 __ movl(temp_reg, Address(ESP, src + stack_offset));
5747 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005748}
5749
5750void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005751 ScratchRegisterScope ensure_scratch(
5752 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5753 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5754 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5755 __ movl(temp_reg, Address(ESP, src + stack_offset));
5756 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5757 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5758 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005759}
5760
5761void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005762 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005763 Location source = move->GetSource();
5764 Location destination = move->GetDestination();
5765
5766 if (source.IsRegister()) {
5767 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005768 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005769 } else if (destination.IsFpuRegister()) {
5770 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005771 } else {
5772 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005773 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005774 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005775 } else if (source.IsRegisterPair()) {
5776 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5777 // Create stack space for 2 elements.
5778 __ subl(ESP, Immediate(2 * elem_size));
5779 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5780 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5781 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5782 // And remove the temporary stack space we allocated.
5783 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005784 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005785 if (destination.IsRegister()) {
5786 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5787 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005788 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005789 } else if (destination.IsRegisterPair()) {
5790 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5791 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5792 __ psrlq(src_reg, Immediate(32));
5793 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005794 } else if (destination.IsStackSlot()) {
5795 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5796 } else {
5797 DCHECK(destination.IsDoubleStackSlot());
5798 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5799 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005800 } else if (source.IsStackSlot()) {
5801 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005802 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005803 } else if (destination.IsFpuRegister()) {
5804 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005805 } else {
5806 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005807 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5808 }
5809 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005810 if (destination.IsRegisterPair()) {
5811 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5812 __ movl(destination.AsRegisterPairHigh<Register>(),
5813 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5814 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005815 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5816 } else {
5817 DCHECK(destination.IsDoubleStackSlot()) << destination;
5818 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005819 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005820 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005821 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005822 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005823 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005824 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005825 if (value == 0) {
5826 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5827 } else {
5828 __ movl(destination.AsRegister<Register>(), Immediate(value));
5829 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005830 } else {
5831 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005832 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005833 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005834 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005835 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005836 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005837 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005838 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005839 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5840 if (value == 0) {
5841 // Easy handling of 0.0.
5842 __ xorps(dest, dest);
5843 } else {
5844 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005845 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5846 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5847 __ movl(temp, Immediate(value));
5848 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005849 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005850 } else {
5851 DCHECK(destination.IsStackSlot()) << destination;
5852 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5853 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005854 } else if (constant->IsLongConstant()) {
5855 int64_t value = constant->AsLongConstant()->GetValue();
5856 int32_t low_value = Low32Bits(value);
5857 int32_t high_value = High32Bits(value);
5858 Immediate low(low_value);
5859 Immediate high(high_value);
5860 if (destination.IsDoubleStackSlot()) {
5861 __ movl(Address(ESP, destination.GetStackIndex()), low);
5862 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5863 } else {
5864 __ movl(destination.AsRegisterPairLow<Register>(), low);
5865 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5866 }
5867 } else {
5868 DCHECK(constant->IsDoubleConstant());
5869 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005870 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005871 int32_t low_value = Low32Bits(value);
5872 int32_t high_value = High32Bits(value);
5873 Immediate low(low_value);
5874 Immediate high(high_value);
5875 if (destination.IsFpuRegister()) {
5876 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5877 if (value == 0) {
5878 // Easy handling of 0.0.
5879 __ xorpd(dest, dest);
5880 } else {
5881 __ pushl(high);
5882 __ pushl(low);
5883 __ movsd(dest, Address(ESP, 0));
5884 __ addl(ESP, Immediate(8));
5885 }
5886 } else {
5887 DCHECK(destination.IsDoubleStackSlot()) << destination;
5888 __ movl(Address(ESP, destination.GetStackIndex()), low);
5889 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5890 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005891 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005892 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005893 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005894 }
5895}
5896
Mark Mendella5c19ce2015-04-01 12:51:05 -04005897void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005898 Register suggested_scratch = reg == EAX ? EBX : EAX;
5899 ScratchRegisterScope ensure_scratch(
5900 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5901
5902 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5903 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5904 __ movl(Address(ESP, mem + stack_offset), reg);
5905 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005906}
5907
Mark Mendell7c8d0092015-01-26 11:21:33 -05005908void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005909 ScratchRegisterScope ensure_scratch(
5910 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5911
5912 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5913 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5914 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5915 __ movss(Address(ESP, mem + stack_offset), reg);
5916 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005917}
5918
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005919void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005920 ScratchRegisterScope ensure_scratch1(
5921 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005922
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005923 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5924 ScratchRegisterScope ensure_scratch2(
5925 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005926
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005927 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5928 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5929 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5930 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5931 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5932 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005933}
5934
5935void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005936 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005937 Location source = move->GetSource();
5938 Location destination = move->GetDestination();
5939
5940 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005941 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5942 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5943 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5944 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5945 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005946 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005947 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005948 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005949 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005950 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5951 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005952 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5953 // Use XOR Swap algorithm to avoid a temporary.
5954 DCHECK_NE(source.reg(), destination.reg());
5955 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5956 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5957 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5958 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5959 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5960 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5961 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005962 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5963 // Take advantage of the 16 bytes in the XMM register.
5964 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5965 Address stack(ESP, destination.GetStackIndex());
5966 // Load the double into the high doubleword.
5967 __ movhpd(reg, stack);
5968
5969 // Store the low double into the destination.
5970 __ movsd(stack, reg);
5971
5972 // Move the high double to the low double.
5973 __ psrldq(reg, Immediate(8));
5974 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5975 // Take advantage of the 16 bytes in the XMM register.
5976 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5977 Address stack(ESP, source.GetStackIndex());
5978 // Load the double into the high doubleword.
5979 __ movhpd(reg, stack);
5980
5981 // Store the low double into the destination.
5982 __ movsd(stack, reg);
5983
5984 // Move the high double to the low double.
5985 __ psrldq(reg, Immediate(8));
5986 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5987 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5988 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005989 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005990 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005991 }
5992}
5993
5994void ParallelMoveResolverX86::SpillScratch(int reg) {
5995 __ pushl(static_cast<Register>(reg));
5996}
5997
5998void ParallelMoveResolverX86::RestoreScratch(int reg) {
5999 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006000}
6001
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006002HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6003 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006004 switch (desired_class_load_kind) {
6005 case HLoadClass::LoadKind::kReferrersClass:
6006 break;
6007 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6008 DCHECK(!GetCompilerOptions().GetCompilePic());
6009 break;
6010 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6011 DCHECK(GetCompilerOptions().GetCompilePic());
6012 FALLTHROUGH_INTENDED;
6013 case HLoadClass::LoadKind::kDexCachePcRelative:
6014 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
6015 // We disable pc-relative load when there is an irreducible loop, as the optimization
6016 // is incompatible with it.
6017 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6018 // with irreducible loops.
6019 if (GetGraph()->HasIrreducibleLoops()) {
6020 return HLoadClass::LoadKind::kDexCacheViaMethod;
6021 }
6022 break;
6023 case HLoadClass::LoadKind::kBootImageAddress:
6024 break;
6025 case HLoadClass::LoadKind::kDexCacheAddress:
6026 DCHECK(Runtime::Current()->UseJitCompilation());
6027 break;
6028 case HLoadClass::LoadKind::kDexCacheViaMethod:
6029 break;
6030 }
6031 return desired_class_load_kind;
6032}
6033
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006034void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006035 if (cls->NeedsAccessCheck()) {
6036 InvokeRuntimeCallingConvention calling_convention;
6037 CodeGenerator::CreateLoadClassLocationSummary(
6038 cls,
6039 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6040 Location::RegisterLocation(EAX),
6041 /* code_generator_supports_read_barrier */ true);
6042 return;
6043 }
6044
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006045 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6046 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006047 ? LocationSummary::kCallOnSlowPath
6048 : LocationSummary::kNoCall;
6049 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006050 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006051 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006052 }
6053
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006054 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6055 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6056 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6057 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6058 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6059 locations->SetInAt(0, Location::RequiresRegister());
6060 }
6061 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006062}
6063
6064void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006065 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006066 if (cls->NeedsAccessCheck()) {
6067 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01006068 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006069 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006070 return;
6071 }
6072
Roland Levillain0d5a2812015-11-13 10:07:31 +00006073 Location out_loc = locations->Out();
6074 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006075
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006076 bool generate_null_check = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006077 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006078 switch (cls->GetLoadKind()) {
6079 case HLoadClass::LoadKind::kReferrersClass: {
6080 DCHECK(!cls->CanCallRuntime());
6081 DCHECK(!cls->MustGenerateClinitCheck());
6082 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6083 Register current_method = locations->InAt(0).AsRegister<Register>();
6084 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006085 cls,
6086 out_loc,
6087 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006088 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006089 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006090 break;
6091 }
6092 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006093 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006094 __ movl(out, Immediate(/* placeholder */ 0));
6095 codegen_->RecordTypePatch(cls);
6096 break;
6097 }
6098 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006099 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006100 Register method_address = locations->InAt(0).AsRegister<Register>();
6101 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6102 codegen_->RecordTypePatch(cls);
6103 break;
6104 }
6105 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006106 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006107 DCHECK_NE(cls->GetAddress(), 0u);
6108 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6109 __ movl(out, Immediate(address));
6110 codegen_->RecordSimplePatch();
6111 break;
6112 }
6113 case HLoadClass::LoadKind::kDexCacheAddress: {
6114 DCHECK_NE(cls->GetAddress(), 0u);
6115 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6116 // /* GcRoot<mirror::Class> */ out = *address
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006117 GenerateGcRootFieldLoad(cls,
6118 out_loc,
6119 Address::Absolute(address),
Roland Levillain00468f32016-10-27 18:02:48 +01006120 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006121 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006122 generate_null_check = !cls->IsInDexCache();
6123 break;
6124 }
6125 case HLoadClass::LoadKind::kDexCachePcRelative: {
6126 Register base_reg = locations->InAt(0).AsRegister<Register>();
6127 uint32_t offset = cls->GetDexCacheElementOffset();
6128 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6129 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006130 GenerateGcRootFieldLoad(cls,
6131 out_loc,
6132 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
6133 fixup_label,
6134 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006135 generate_null_check = !cls->IsInDexCache();
6136 break;
6137 }
6138 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6139 // /* GcRoot<mirror::Class>[] */ out =
6140 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6141 Register current_method = locations->InAt(0).AsRegister<Register>();
6142 __ movl(out, Address(current_method,
6143 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6144 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006145 GenerateGcRootFieldLoad(cls,
6146 out_loc,
6147 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
Roland Levillain00468f32016-10-27 18:02:48 +01006148 /* fixup_label */ nullptr,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006149 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006150 generate_null_check = !cls->IsInDexCache();
6151 break;
6152 }
6153 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006154
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006155 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6156 DCHECK(cls->CanCallRuntime());
6157 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6158 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6159 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006160
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006161 if (generate_null_check) {
6162 __ testl(out, out);
6163 __ j(kEqual, slow_path->GetEntryLabel());
6164 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006165
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006166 if (cls->MustGenerateClinitCheck()) {
6167 GenerateClassInitializationCheck(slow_path, out);
6168 } else {
6169 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006170 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006171 }
6172}
6173
6174void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6175 LocationSummary* locations =
6176 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6177 locations->SetInAt(0, Location::RequiresRegister());
6178 if (check->HasUses()) {
6179 locations->SetOut(Location::SameAsFirstInput());
6180 }
6181}
6182
6183void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006184 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006185 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006186 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006187 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006188 GenerateClassInitializationCheck(slow_path,
6189 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006190}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006191
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006192void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006193 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006194 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6195 Immediate(mirror::Class::kStatusInitialized));
6196 __ j(kLess, slow_path->GetEntryLabel());
6197 __ Bind(slow_path->GetExitLabel());
6198 // No need for memory fence, thanks to the X86 memory model.
6199}
6200
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006201HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6202 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006203 switch (desired_string_load_kind) {
6204 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6205 DCHECK(!GetCompilerOptions().GetCompilePic());
6206 break;
6207 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6208 DCHECK(GetCompilerOptions().GetCompilePic());
6209 FALLTHROUGH_INTENDED;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006210 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006211 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006212 // We disable pc-relative load when there is an irreducible loop, as the optimization
6213 // is incompatible with it.
6214 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6215 // with irreducible loops.
6216 if (GetGraph()->HasIrreducibleLoops()) {
6217 return HLoadString::LoadKind::kDexCacheViaMethod;
6218 }
6219 break;
6220 case HLoadString::LoadKind::kBootImageAddress:
6221 break;
6222 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006223 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006224 break;
6225 case HLoadString::LoadKind::kDexCacheViaMethod:
6226 break;
Nicolas Geoffrayac3ebc32016-10-05 13:13:50 +01006227 case HLoadString::LoadKind::kJitTableAddress:
6228 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray997d1212016-11-09 10:36:29 +00006229 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006230 }
6231 return desired_string_load_kind;
6232}
6233
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006234void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006235 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00006236 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
6237 ? LocationSummary::kCallOnMainOnly
6238 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006239 : LocationSummary::kNoCall;
6240 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006241 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006242 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006243 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006244 locations->SetInAt(0, Location::RequiresRegister());
6245 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006246 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6247 locations->SetOut(Location::RegisterLocation(EAX));
6248 } else {
6249 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006250 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6251 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6252 // Rely on the pResolveString and/or marking to save everything.
6253 RegisterSet caller_saves = RegisterSet::Empty();
6254 InvokeRuntimeCallingConvention calling_convention;
6255 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6256 locations->SetCustomSlowPathCallerSaves(caller_saves);
6257 } else {
6258 // For non-Baker read barrier we have a temp-clobbering call.
6259 }
6260 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006261 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006262}
6263
Nicolas Geoffray997d1212016-11-09 10:36:29 +00006264Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file, uint32_t dex_index) {
6265 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index), /* placeholder */ 0u);
6266 // Add a patch entry and return the label.
6267 jit_string_patches_.emplace_back(dex_file, dex_index);
6268 PatchInfo<Label>* info = &jit_string_patches_.back();
6269 return &info->label;
6270}
6271
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006272void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006273 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006274 Location out_loc = locations->Out();
6275 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006276
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006277 switch (load->GetLoadKind()) {
6278 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006279 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006280 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006281 return; // No dex cache slow path.
6282 }
6283 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006284 Register method_address = locations->InAt(0).AsRegister<Register>();
6285 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006286 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006287 return; // No dex cache slow path.
6288 }
6289 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006290 DCHECK_NE(load->GetAddress(), 0u);
6291 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6292 __ movl(out, Immediate(address));
6293 codegen_->RecordSimplePatch();
6294 return; // No dex cache slow path.
6295 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006296 case HLoadString::LoadKind::kBssEntry: {
6297 Register method_address = locations->InAt(0).AsRegister<Register>();
6298 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6299 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray997d1212016-11-09 10:36:29 +00006300 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Roland Levillain00468f32016-10-27 18:02:48 +01006301 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006302 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6303 codegen_->AddSlowPath(slow_path);
6304 __ testl(out, out);
6305 __ j(kEqual, slow_path->GetEntryLabel());
6306 __ Bind(slow_path->GetExitLabel());
6307 return;
6308 }
Nicolas Geoffray997d1212016-11-09 10:36:29 +00006309 case HLoadString::LoadKind::kJitTableAddress: {
6310 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6311 Label* fixup_label = codegen_->NewJitRootStringPatch(
6312 load->GetDexFile(), load->GetStringIndex());
6313 // /* GcRoot<mirror::String> */ out = *address
6314 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kEmitCompilerReadBarrier);
6315 return;
6316 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006317 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006318 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006319 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006320
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006321 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006322 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006323 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006324 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex()));
6325 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6326 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006327}
6328
David Brazdilcb1c0552015-08-04 16:22:25 +01006329static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006330 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006331}
6332
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006333void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6334 LocationSummary* locations =
6335 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6336 locations->SetOut(Location::RequiresRegister());
6337}
6338
6339void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006340 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6341}
6342
6343void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6344 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6345}
6346
6347void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6348 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006349}
6350
6351void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6352 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006353 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006354 InvokeRuntimeCallingConvention calling_convention;
6355 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6356}
6357
6358void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006359 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006360 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006361}
6362
Roland Levillain7c1559a2015-12-15 10:55:36 +00006363static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6364 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006365 !kUseBakerReadBarrier &&
6366 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006367 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6368 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6369}
6370
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006371void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006372 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006373 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006374 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006375 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006376 case TypeCheckKind::kExactCheck:
6377 case TypeCheckKind::kAbstractClassCheck:
6378 case TypeCheckKind::kClassHierarchyCheck:
6379 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006380 call_kind =
6381 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006382 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006383 break;
6384 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006385 case TypeCheckKind::kUnresolvedCheck:
6386 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006387 call_kind = LocationSummary::kCallOnSlowPath;
6388 break;
6389 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006390
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006391 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006392 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006393 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006394 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006395 locations->SetInAt(0, Location::RequiresRegister());
6396 locations->SetInAt(1, Location::Any());
6397 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6398 locations->SetOut(Location::RequiresRegister());
6399 // When read barriers are enabled, we need a temporary register for
6400 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006401 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006402 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006403 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006404}
6405
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006406void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006407 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006408 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006409 Location obj_loc = locations->InAt(0);
6410 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006411 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412 Location out_loc = locations->Out();
6413 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006414 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006415 locations->GetTemp(0) :
6416 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006417 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006418 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6419 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6420 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006421 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006422 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006423
6424 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006425 // Avoid null check if we know obj is not null.
6426 if (instruction->MustDoNullCheck()) {
6427 __ testl(obj, obj);
6428 __ j(kEqual, &zero);
6429 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006430
Roland Levillain0d5a2812015-11-13 10:07:31 +00006431 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006432 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006433
Roland Levillain7c1559a2015-12-15 10:55:36 +00006434 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006435 case TypeCheckKind::kExactCheck: {
6436 if (cls.IsRegister()) {
6437 __ cmpl(out, cls.AsRegister<Register>());
6438 } else {
6439 DCHECK(cls.IsStackSlot()) << cls;
6440 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6441 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006442
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006443 // Classes must be equal for the instanceof to succeed.
6444 __ j(kNotEqual, &zero);
6445 __ movl(out, Immediate(1));
6446 __ jmp(&done);
6447 break;
6448 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006450 case TypeCheckKind::kAbstractClassCheck: {
6451 // If the class is abstract, we eagerly fetch the super class of the
6452 // object to avoid doing a comparison we know will fail.
6453 NearLabel loop;
6454 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006455 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006456 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006457 __ testl(out, out);
6458 // If `out` is null, we use it for the result, and jump to `done`.
6459 __ j(kEqual, &done);
6460 if (cls.IsRegister()) {
6461 __ cmpl(out, cls.AsRegister<Register>());
6462 } else {
6463 DCHECK(cls.IsStackSlot()) << cls;
6464 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6465 }
6466 __ j(kNotEqual, &loop);
6467 __ movl(out, Immediate(1));
6468 if (zero.IsLinked()) {
6469 __ jmp(&done);
6470 }
6471 break;
6472 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006473
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006474 case TypeCheckKind::kClassHierarchyCheck: {
6475 // Walk over the class hierarchy to find a match.
6476 NearLabel loop, success;
6477 __ Bind(&loop);
6478 if (cls.IsRegister()) {
6479 __ cmpl(out, cls.AsRegister<Register>());
6480 } else {
6481 DCHECK(cls.IsStackSlot()) << cls;
6482 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6483 }
6484 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006485 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006486 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006487 __ testl(out, out);
6488 __ j(kNotEqual, &loop);
6489 // If `out` is null, we use it for the result, and jump to `done`.
6490 __ jmp(&done);
6491 __ Bind(&success);
6492 __ movl(out, Immediate(1));
6493 if (zero.IsLinked()) {
6494 __ jmp(&done);
6495 }
6496 break;
6497 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006498
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006499 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006500 // Do an exact check.
6501 NearLabel exact_check;
6502 if (cls.IsRegister()) {
6503 __ cmpl(out, cls.AsRegister<Register>());
6504 } else {
6505 DCHECK(cls.IsStackSlot()) << cls;
6506 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6507 }
6508 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006509 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006510 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006511 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006512 __ testl(out, out);
6513 // If `out` is null, we use it for the result, and jump to `done`.
6514 __ j(kEqual, &done);
6515 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6516 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006517 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006518 __ movl(out, Immediate(1));
6519 __ jmp(&done);
6520 break;
6521 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006522
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006523 case TypeCheckKind::kArrayCheck: {
6524 if (cls.IsRegister()) {
6525 __ cmpl(out, cls.AsRegister<Register>());
6526 } else {
6527 DCHECK(cls.IsStackSlot()) << cls;
6528 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6529 }
6530 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006531 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6532 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006533 codegen_->AddSlowPath(slow_path);
6534 __ j(kNotEqual, slow_path->GetEntryLabel());
6535 __ movl(out, Immediate(1));
6536 if (zero.IsLinked()) {
6537 __ jmp(&done);
6538 }
6539 break;
6540 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006541
Calin Juravle98893e12015-10-02 21:05:03 +01006542 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006543 case TypeCheckKind::kInterfaceCheck: {
6544 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006545 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006546 // cases.
6547 //
6548 // We cannot directly call the InstanceofNonTrivial runtime
6549 // entry point without resorting to a type checking slow path
6550 // here (i.e. by calling InvokeRuntime directly), as it would
6551 // require to assign fixed registers for the inputs of this
6552 // HInstanceOf instruction (following the runtime calling
6553 // convention), which might be cluttered by the potential first
6554 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006555 //
6556 // TODO: Introduce a new runtime entry point taking the object
6557 // to test (instead of its class) as argument, and let it deal
6558 // with the read barrier issues. This will let us refactor this
6559 // case of the `switch` code as it was previously (with a direct
6560 // call to the runtime not using a type checking slow path).
6561 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006562 DCHECK(locations->OnlyCallsOnSlowPath());
6563 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6564 /* is_fatal */ false);
6565 codegen_->AddSlowPath(slow_path);
6566 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006567 if (zero.IsLinked()) {
6568 __ jmp(&done);
6569 }
6570 break;
6571 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006572 }
6573
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006574 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006575 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006576 __ xorl(out, out);
6577 }
6578
6579 if (done.IsLinked()) {
6580 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006581 }
6582
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006583 if (slow_path != nullptr) {
6584 __ Bind(slow_path->GetExitLabel());
6585 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006586}
6587
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006588void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006589 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6590 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006591 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6592 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006593 case TypeCheckKind::kExactCheck:
6594 case TypeCheckKind::kAbstractClassCheck:
6595 case TypeCheckKind::kClassHierarchyCheck:
6596 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006597 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6598 LocationSummary::kCallOnSlowPath :
6599 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006600 break;
6601 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006602 case TypeCheckKind::kUnresolvedCheck:
6603 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006604 call_kind = LocationSummary::kCallOnSlowPath;
6605 break;
6606 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006607 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6608 locations->SetInAt(0, Location::RequiresRegister());
6609 locations->SetInAt(1, Location::Any());
6610 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6611 locations->AddTemp(Location::RequiresRegister());
6612 // When read barriers are enabled, we need an additional temporary
6613 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006614 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006615 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006616 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006617}
6618
6619void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006620 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006621 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006622 Location obj_loc = locations->InAt(0);
6623 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006624 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006625 Location temp_loc = locations->GetTemp(0);
6626 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006627 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006628 locations->GetTemp(1) :
6629 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006630 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6631 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6632 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6633 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006634
Roland Levillain0d5a2812015-11-13 10:07:31 +00006635 bool is_type_check_slow_path_fatal =
6636 (type_check_kind == TypeCheckKind::kExactCheck ||
6637 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6638 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6639 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6640 !instruction->CanThrowIntoCatchBlock();
6641 SlowPathCode* type_check_slow_path =
6642 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6643 is_type_check_slow_path_fatal);
6644 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006645
Roland Levillain0d5a2812015-11-13 10:07:31 +00006646 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006647 // Avoid null check if we know obj is not null.
6648 if (instruction->MustDoNullCheck()) {
6649 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006650 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006651 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006652
Roland Levillain0d5a2812015-11-13 10:07:31 +00006653 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006654 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006655
Roland Levillain0d5a2812015-11-13 10:07:31 +00006656 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006657 case TypeCheckKind::kExactCheck:
6658 case TypeCheckKind::kArrayCheck: {
6659 if (cls.IsRegister()) {
6660 __ cmpl(temp, cls.AsRegister<Register>());
6661 } else {
6662 DCHECK(cls.IsStackSlot()) << cls;
6663 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6664 }
6665 // Jump to slow path for throwing the exception or doing a
6666 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006667 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006668 break;
6669 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006670
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006671 case TypeCheckKind::kAbstractClassCheck: {
6672 // If the class is abstract, we eagerly fetch the super class of the
6673 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006674 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006675 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006676 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006677 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006678
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006679 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6680 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006681 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006682 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006683
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006684 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006685 if (cls.IsRegister()) {
6686 __ cmpl(temp, cls.AsRegister<Register>());
6687 } else {
6688 DCHECK(cls.IsStackSlot()) << cls;
6689 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6690 }
6691 __ j(kNotEqual, &loop);
6692 break;
6693 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006694
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006695 case TypeCheckKind::kClassHierarchyCheck: {
6696 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006697 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006698 __ Bind(&loop);
6699 if (cls.IsRegister()) {
6700 __ cmpl(temp, cls.AsRegister<Register>());
6701 } else {
6702 DCHECK(cls.IsStackSlot()) << cls;
6703 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6704 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006705 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006706
Roland Levillain0d5a2812015-11-13 10:07:31 +00006707 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006708 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006709
6710 // If the class reference currently in `temp` is not null, jump
6711 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006712 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006713 __ j(kNotZero, &loop);
6714 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006715 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006716 break;
6717 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006718
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006719 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006720 // Do an exact check.
6721 if (cls.IsRegister()) {
6722 __ cmpl(temp, cls.AsRegister<Register>());
6723 } else {
6724 DCHECK(cls.IsStackSlot()) << cls;
6725 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6726 }
6727 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006728
6729 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006730 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006731 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006732
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006733 // If the component type is null (i.e. the object not an array), jump to the slow path to
6734 // throw the exception. Otherwise proceed with the check.
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
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006738 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006739 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006740 break;
6741 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006742
Calin Juravle98893e12015-10-02 21:05:03 +01006743 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006744 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006745 // We always go into the type check slow path for the unresolved
6746 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006747 //
6748 // We cannot directly call the CheckCast runtime entry point
6749 // without resorting to a type checking slow path here (i.e. by
6750 // calling InvokeRuntime directly), as it would require to
6751 // assign fixed registers for the inputs of this HInstanceOf
6752 // instruction (following the runtime calling convention), which
6753 // might be cluttered by the potential first read barrier
6754 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006755 //
6756 // TODO: Introduce a new runtime entry point taking the object
6757 // to test (instead of its class) as argument, and let it deal
6758 // with the read barrier issues. This will let us refactor this
6759 // case of the `switch` code as it was previously (with a direct
6760 // call to the runtime not using a type checking slow path).
6761 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006762 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006763 break;
6764 }
6765 __ Bind(&done);
6766
Roland Levillain0d5a2812015-11-13 10:07:31 +00006767 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006768}
6769
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006770void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6771 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006772 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006773 InvokeRuntimeCallingConvention calling_convention;
6774 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6775}
6776
6777void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006778 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6779 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006780 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006781 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006782 if (instruction->IsEnter()) {
6783 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6784 } else {
6785 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6786 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006787}
6788
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006789void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6790void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6791void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6792
6793void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6794 LocationSummary* locations =
6795 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6796 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6797 || instruction->GetResultType() == Primitive::kPrimLong);
6798 locations->SetInAt(0, Location::RequiresRegister());
6799 locations->SetInAt(1, Location::Any());
6800 locations->SetOut(Location::SameAsFirstInput());
6801}
6802
6803void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6804 HandleBitwiseOperation(instruction);
6805}
6806
6807void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6808 HandleBitwiseOperation(instruction);
6809}
6810
6811void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6812 HandleBitwiseOperation(instruction);
6813}
6814
6815void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6816 LocationSummary* locations = instruction->GetLocations();
6817 Location first = locations->InAt(0);
6818 Location second = locations->InAt(1);
6819 DCHECK(first.Equals(locations->Out()));
6820
6821 if (instruction->GetResultType() == Primitive::kPrimInt) {
6822 if (second.IsRegister()) {
6823 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006824 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006825 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006826 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006827 } else {
6828 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006829 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006830 }
6831 } else if (second.IsConstant()) {
6832 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006833 __ andl(first.AsRegister<Register>(),
6834 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006835 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006836 __ orl(first.AsRegister<Register>(),
6837 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006838 } else {
6839 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006840 __ xorl(first.AsRegister<Register>(),
6841 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006842 }
6843 } else {
6844 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006845 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006846 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006847 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006848 } else {
6849 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006850 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006851 }
6852 }
6853 } else {
6854 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6855 if (second.IsRegisterPair()) {
6856 if (instruction->IsAnd()) {
6857 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6858 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6859 } else if (instruction->IsOr()) {
6860 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6861 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6862 } else {
6863 DCHECK(instruction->IsXor());
6864 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6865 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6866 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006867 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006868 if (instruction->IsAnd()) {
6869 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6870 __ andl(first.AsRegisterPairHigh<Register>(),
6871 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6872 } else if (instruction->IsOr()) {
6873 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6874 __ orl(first.AsRegisterPairHigh<Register>(),
6875 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6876 } else {
6877 DCHECK(instruction->IsXor());
6878 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6879 __ xorl(first.AsRegisterPairHigh<Register>(),
6880 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6881 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006882 } else {
6883 DCHECK(second.IsConstant()) << second;
6884 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006885 int32_t low_value = Low32Bits(value);
6886 int32_t high_value = High32Bits(value);
6887 Immediate low(low_value);
6888 Immediate high(high_value);
6889 Register first_low = first.AsRegisterPairLow<Register>();
6890 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006891 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006892 if (low_value == 0) {
6893 __ xorl(first_low, first_low);
6894 } else if (low_value != -1) {
6895 __ andl(first_low, low);
6896 }
6897 if (high_value == 0) {
6898 __ xorl(first_high, first_high);
6899 } else if (high_value != -1) {
6900 __ andl(first_high, high);
6901 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006902 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006903 if (low_value != 0) {
6904 __ orl(first_low, low);
6905 }
6906 if (high_value != 0) {
6907 __ orl(first_high, high);
6908 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006909 } else {
6910 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006911 if (low_value != 0) {
6912 __ xorl(first_low, low);
6913 }
6914 if (high_value != 0) {
6915 __ xorl(first_high, high);
6916 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006917 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006918 }
6919 }
6920}
6921
Roland Levillain7c1559a2015-12-15 10:55:36 +00006922void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6923 Location out,
6924 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006925 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006926 Register out_reg = out.AsRegister<Register>();
6927 if (kEmitCompilerReadBarrier) {
6928 if (kUseBakerReadBarrier) {
6929 // Load with fast path based Baker's read barrier.
6930 // /* HeapReference<Object> */ out = *(out + offset)
6931 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006932 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006933 } else {
6934 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006935 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006936 // in the following move operation, as we will need it for the
6937 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006938 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006939 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006940 // /* HeapReference<Object> */ out = *(out + offset)
6941 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006942 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006943 }
6944 } else {
6945 // Plain load with no read barrier.
6946 // /* HeapReference<Object> */ out = *(out + offset)
6947 __ movl(out_reg, Address(out_reg, offset));
6948 __ MaybeUnpoisonHeapReference(out_reg);
6949 }
6950}
6951
6952void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6953 Location out,
6954 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006955 uint32_t offset) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006956 Register out_reg = out.AsRegister<Register>();
6957 Register obj_reg = obj.AsRegister<Register>();
6958 if (kEmitCompilerReadBarrier) {
6959 if (kUseBakerReadBarrier) {
6960 // Load with fast path based Baker's read barrier.
6961 // /* HeapReference<Object> */ out = *(obj + offset)
6962 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006963 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006964 } else {
6965 // Load with slow path based read barrier.
6966 // /* HeapReference<Object> */ out = *(obj + offset)
6967 __ movl(out_reg, Address(obj_reg, offset));
6968 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6969 }
6970 } else {
6971 // Plain load with no read barrier.
6972 // /* HeapReference<Object> */ out = *(obj + offset)
6973 __ movl(out_reg, Address(obj_reg, offset));
6974 __ MaybeUnpoisonHeapReference(out_reg);
6975 }
6976}
6977
6978void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6979 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006980 const Address& address,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006981 Label* fixup_label,
6982 bool requires_read_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006983 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006984 if (requires_read_barrier) {
6985 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006986 if (kUseBakerReadBarrier) {
6987 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6988 // Baker's read barrier are used:
6989 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006990 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006991 // if (Thread::Current()->GetIsGcMarking()) {
6992 // root = ReadBarrier::Mark(root)
6993 // }
6994
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006995 // /* GcRoot<mirror::Object> */ root = *address
6996 __ movl(root_reg, address);
6997 if (fixup_label != nullptr) {
6998 __ Bind(fixup_label);
6999 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007000 static_assert(
7001 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7002 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7003 "have different sizes.");
7004 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7005 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7006 "have different sizes.");
7007
Vladimir Marko953437b2016-08-24 08:30:46 +00007008 // Slow path marking the GC root `root`.
7009 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007010 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007011 codegen_->AddSlowPath(slow_path);
7012
Andreas Gampe542451c2016-07-26 09:02:02 -07007013 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00007014 Immediate(0));
7015 __ j(kNotEqual, slow_path->GetEntryLabel());
7016 __ Bind(slow_path->GetExitLabel());
7017 } else {
7018 // GC root loaded through a slow path for read barriers other
7019 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007020 // /* GcRoot<mirror::Object>* */ root = address
7021 __ leal(root_reg, address);
7022 if (fixup_label != nullptr) {
7023 __ Bind(fixup_label);
7024 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007025 // /* mirror::Object* */ root = root->Read()
7026 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7027 }
7028 } else {
7029 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007030 // /* GcRoot<mirror::Object> */ root = *address
7031 __ movl(root_reg, address);
7032 if (fixup_label != nullptr) {
7033 __ Bind(fixup_label);
7034 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007035 // Note that GC roots are not affected by heap poisoning, thus we
7036 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007037 }
7038}
7039
7040void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7041 Location ref,
7042 Register obj,
7043 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007044 bool needs_null_check) {
7045 DCHECK(kEmitCompilerReadBarrier);
7046 DCHECK(kUseBakerReadBarrier);
7047
7048 // /* HeapReference<Object> */ ref = *(obj + offset)
7049 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007050 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007051}
7052
7053void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7054 Location ref,
7055 Register obj,
7056 uint32_t data_offset,
7057 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007058 bool needs_null_check) {
7059 DCHECK(kEmitCompilerReadBarrier);
7060 DCHECK(kUseBakerReadBarrier);
7061
Roland Levillain3d312422016-06-23 13:53:42 +01007062 static_assert(
7063 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7064 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007065 // /* HeapReference<Object> */ ref =
7066 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007067 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007068 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007069}
7070
7071void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7072 Location ref,
7073 Register obj,
7074 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007075 bool needs_null_check,
7076 bool always_update_field,
7077 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007078 DCHECK(kEmitCompilerReadBarrier);
7079 DCHECK(kUseBakerReadBarrier);
7080
7081 // In slow path based read barriers, the read barrier call is
7082 // inserted after the original load. However, in fast path based
7083 // Baker's read barriers, we need to perform the load of
7084 // mirror::Object::monitor_ *before* the original reference load.
7085 // This load-load ordering is required by the read barrier.
7086 // The fast path/slow path (for Baker's algorithm) should look like:
7087 //
7088 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7089 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7090 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007091 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007092 // if (is_gray) {
7093 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7094 // }
7095 //
7096 // Note: the original implementation in ReadBarrier::Barrier is
7097 // slightly more complex as:
7098 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007099 // the high-bits of rb_state, which are expected to be all zeroes
7100 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7101 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007102 // - it performs additional checks that we do not do here for
7103 // performance reasons.
7104
7105 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007106 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7107
Vladimir Marko953437b2016-08-24 08:30:46 +00007108 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007109 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7110 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007111 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7112 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7113 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7114
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007115 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007116 // ref = ReadBarrier::Mark(ref);
7117 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7118 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007119 if (needs_null_check) {
7120 MaybeRecordImplicitNullCheck(instruction);
7121 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007122
7123 // Load fence to prevent load-load reordering.
7124 // Note that this is a no-op, thanks to the x86 memory model.
7125 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7126
7127 // The actual reference load.
7128 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007129 __ movl(ref_reg, src); // Flags are unaffected.
7130
7131 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7132 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007133 SlowPathCode* slow_path;
7134 if (always_update_field) {
7135 DCHECK(temp != nullptr);
7136 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7137 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7138 } else {
7139 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7140 instruction, ref, /* unpoison_ref_before_marking */ true);
7141 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007142 AddSlowPath(slow_path);
7143
7144 // We have done the "if" of the gray bit check above, now branch based on the flags.
7145 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007146
7147 // Object* ref = ref_addr->AsMirrorPtr()
7148 __ MaybeUnpoisonHeapReference(ref_reg);
7149
Roland Levillain7c1559a2015-12-15 10:55:36 +00007150 __ Bind(slow_path->GetExitLabel());
7151}
7152
7153void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7154 Location out,
7155 Location ref,
7156 Location obj,
7157 uint32_t offset,
7158 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007159 DCHECK(kEmitCompilerReadBarrier);
7160
Roland Levillain7c1559a2015-12-15 10:55:36 +00007161 // Insert a slow path based read barrier *after* the reference load.
7162 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007163 // If heap poisoning is enabled, the unpoisoning of the loaded
7164 // reference will be carried out by the runtime within the slow
7165 // path.
7166 //
7167 // Note that `ref` currently does not get unpoisoned (when heap
7168 // poisoning is enabled), which is alright as the `ref` argument is
7169 // not used by the artReadBarrierSlow entry point.
7170 //
7171 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7172 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7173 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7174 AddSlowPath(slow_path);
7175
Roland Levillain0d5a2812015-11-13 10:07:31 +00007176 __ jmp(slow_path->GetEntryLabel());
7177 __ Bind(slow_path->GetExitLabel());
7178}
7179
Roland Levillain7c1559a2015-12-15 10:55:36 +00007180void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7181 Location out,
7182 Location ref,
7183 Location obj,
7184 uint32_t offset,
7185 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007186 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007187 // Baker's read barriers shall be handled by the fast path
7188 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7189 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007190 // If heap poisoning is enabled, unpoisoning will be taken care of
7191 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007192 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007193 } else if (kPoisonHeapReferences) {
7194 __ UnpoisonHeapReference(out.AsRegister<Register>());
7195 }
7196}
7197
Roland Levillain7c1559a2015-12-15 10:55:36 +00007198void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7199 Location out,
7200 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007201 DCHECK(kEmitCompilerReadBarrier);
7202
Roland Levillain7c1559a2015-12-15 10:55:36 +00007203 // Insert a slow path based read barrier *after* the GC root load.
7204 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007205 // Note that GC roots are not affected by heap poisoning, so we do
7206 // not need to do anything special for this here.
7207 SlowPathCode* slow_path =
7208 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7209 AddSlowPath(slow_path);
7210
Roland Levillain0d5a2812015-11-13 10:07:31 +00007211 __ jmp(slow_path->GetEntryLabel());
7212 __ Bind(slow_path->GetExitLabel());
7213}
7214
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007215void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007216 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007217 LOG(FATAL) << "Unreachable";
7218}
7219
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007220void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007221 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007222 LOG(FATAL) << "Unreachable";
7223}
7224
Mark Mendellfe57faa2015-09-18 09:26:15 -04007225// Simple implementation of packed switch - generate cascaded compare/jumps.
7226void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7227 LocationSummary* locations =
7228 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7229 locations->SetInAt(0, Location::RequiresRegister());
7230}
7231
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007232void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7233 int32_t lower_bound,
7234 uint32_t num_entries,
7235 HBasicBlock* switch_block,
7236 HBasicBlock* default_block) {
7237 // Figure out the correct compare values and jump conditions.
7238 // Handle the first compare/branch as a special case because it might
7239 // jump to the default case.
7240 DCHECK_GT(num_entries, 2u);
7241 Condition first_condition;
7242 uint32_t index;
7243 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7244 if (lower_bound != 0) {
7245 first_condition = kLess;
7246 __ cmpl(value_reg, Immediate(lower_bound));
7247 __ j(first_condition, codegen_->GetLabelOf(default_block));
7248 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007249
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007250 index = 1;
7251 } else {
7252 // Handle all the compare/jumps below.
7253 first_condition = kBelow;
7254 index = 0;
7255 }
7256
7257 // Handle the rest of the compare/jumps.
7258 for (; index + 1 < num_entries; index += 2) {
7259 int32_t compare_to_value = lower_bound + index + 1;
7260 __ cmpl(value_reg, Immediate(compare_to_value));
7261 // Jump to successors[index] if value < case_value[index].
7262 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7263 // Jump to successors[index + 1] if value == case_value[index + 1].
7264 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7265 }
7266
7267 if (index != num_entries) {
7268 // There are an odd number of entries. Handle the last one.
7269 DCHECK_EQ(index + 1, num_entries);
7270 __ cmpl(value_reg, Immediate(lower_bound + index));
7271 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007272 }
7273
7274 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007275 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7276 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007277 }
7278}
7279
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007280void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7281 int32_t lower_bound = switch_instr->GetStartValue();
7282 uint32_t num_entries = switch_instr->GetNumEntries();
7283 LocationSummary* locations = switch_instr->GetLocations();
7284 Register value_reg = locations->InAt(0).AsRegister<Register>();
7285
7286 GenPackedSwitchWithCompares(value_reg,
7287 lower_bound,
7288 num_entries,
7289 switch_instr->GetBlock(),
7290 switch_instr->GetDefaultBlock());
7291}
7292
Mark Mendell805b3b52015-09-18 14:10:29 -04007293void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7294 LocationSummary* locations =
7295 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7296 locations->SetInAt(0, Location::RequiresRegister());
7297
7298 // Constant area pointer.
7299 locations->SetInAt(1, Location::RequiresRegister());
7300
7301 // And the temporary we need.
7302 locations->AddTemp(Location::RequiresRegister());
7303}
7304
7305void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7306 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007307 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007308 LocationSummary* locations = switch_instr->GetLocations();
7309 Register value_reg = locations->InAt(0).AsRegister<Register>();
7310 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7311
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007312 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7313 GenPackedSwitchWithCompares(value_reg,
7314 lower_bound,
7315 num_entries,
7316 switch_instr->GetBlock(),
7317 default_block);
7318 return;
7319 }
7320
Mark Mendell805b3b52015-09-18 14:10:29 -04007321 // Optimizing has a jump area.
7322 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7323 Register constant_area = locations->InAt(1).AsRegister<Register>();
7324
7325 // Remove the bias, if needed.
7326 if (lower_bound != 0) {
7327 __ leal(temp_reg, Address(value_reg, -lower_bound));
7328 value_reg = temp_reg;
7329 }
7330
7331 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007332 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007333 __ cmpl(value_reg, Immediate(num_entries - 1));
7334 __ j(kAbove, codegen_->GetLabelOf(default_block));
7335
7336 // We are in the range of the table.
7337 // Load (target-constant_area) from the jump table, indexing by the value.
7338 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7339
7340 // Compute the actual target address by adding in constant_area.
7341 __ addl(temp_reg, constant_area);
7342
7343 // And jump.
7344 __ jmp(temp_reg);
7345}
7346
Mark Mendell0616ae02015-04-17 12:49:27 -04007347void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7348 HX86ComputeBaseMethodAddress* insn) {
7349 LocationSummary* locations =
7350 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7351 locations->SetOut(Location::RequiresRegister());
7352}
7353
7354void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7355 HX86ComputeBaseMethodAddress* insn) {
7356 LocationSummary* locations = insn->GetLocations();
7357 Register reg = locations->Out().AsRegister<Register>();
7358
7359 // Generate call to next instruction.
7360 Label next_instruction;
7361 __ call(&next_instruction);
7362 __ Bind(&next_instruction);
7363
7364 // Remember this offset for later use with constant area.
7365 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7366
7367 // Grab the return address off the stack.
7368 __ popl(reg);
7369}
7370
7371void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7372 HX86LoadFromConstantTable* insn) {
7373 LocationSummary* locations =
7374 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7375
7376 locations->SetInAt(0, Location::RequiresRegister());
7377 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7378
7379 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007380 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007381 return;
7382 }
7383
7384 switch (insn->GetType()) {
7385 case Primitive::kPrimFloat:
7386 case Primitive::kPrimDouble:
7387 locations->SetOut(Location::RequiresFpuRegister());
7388 break;
7389
7390 case Primitive::kPrimInt:
7391 locations->SetOut(Location::RequiresRegister());
7392 break;
7393
7394 default:
7395 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7396 }
7397}
7398
7399void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007400 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007401 return;
7402 }
7403
7404 LocationSummary* locations = insn->GetLocations();
7405 Location out = locations->Out();
7406 Register const_area = locations->InAt(0).AsRegister<Register>();
7407 HConstant *value = insn->GetConstant();
7408
7409 switch (insn->GetType()) {
7410 case Primitive::kPrimFloat:
7411 __ movss(out.AsFpuRegister<XmmRegister>(),
7412 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7413 break;
7414
7415 case Primitive::kPrimDouble:
7416 __ movsd(out.AsFpuRegister<XmmRegister>(),
7417 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7418 break;
7419
7420 case Primitive::kPrimInt:
7421 __ movl(out.AsRegister<Register>(),
7422 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7423 break;
7424
7425 default:
7426 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7427 }
7428}
7429
Mark Mendell0616ae02015-04-17 12:49:27 -04007430/**
7431 * Class to handle late fixup of offsets into constant area.
7432 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007433class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007434 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007435 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7436 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7437
7438 protected:
7439 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7440
7441 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007442
7443 private:
7444 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7445 // Patch the correct offset for the instruction. The place to patch is the
7446 // last 4 bytes of the instruction.
7447 // The value to patch is the distance from the offset in the constant area
7448 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007449 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7450 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007451
7452 // Patch in the right value.
7453 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7454 }
7455
Mark Mendell0616ae02015-04-17 12:49:27 -04007456 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007457 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007458};
7459
Mark Mendell805b3b52015-09-18 14:10:29 -04007460/**
7461 * Class to handle late fixup of offsets to a jump table that will be created in the
7462 * constant area.
7463 */
7464class JumpTableRIPFixup : public RIPFixup {
7465 public:
7466 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7467 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7468
7469 void CreateJumpTable() {
7470 X86Assembler* assembler = codegen_->GetAssembler();
7471
7472 // Ensure that the reference to the jump table has the correct offset.
7473 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7474 SetOffset(offset_in_constant_table);
7475
7476 // The label values in the jump table are computed relative to the
7477 // instruction addressing the constant area.
7478 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7479
7480 // Populate the jump table with the correct values for the jump table.
7481 int32_t num_entries = switch_instr_->GetNumEntries();
7482 HBasicBlock* block = switch_instr_->GetBlock();
7483 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7484 // The value that we want is the target offset - the position of the table.
7485 for (int32_t i = 0; i < num_entries; i++) {
7486 HBasicBlock* b = successors[i];
7487 Label* l = codegen_->GetLabelOf(b);
7488 DCHECK(l->IsBound());
7489 int32_t offset_to_block = l->Position() - relative_offset;
7490 assembler->AppendInt32(offset_to_block);
7491 }
7492 }
7493
7494 private:
7495 const HX86PackedSwitch* switch_instr_;
7496};
7497
7498void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7499 // Generate the constant area if needed.
7500 X86Assembler* assembler = GetAssembler();
7501 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7502 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7503 // byte values.
7504 assembler->Align(4, 0);
7505 constant_area_start_ = assembler->CodeSize();
7506
7507 // Populate any jump tables.
7508 for (auto jump_table : fixups_to_jump_tables_) {
7509 jump_table->CreateJumpTable();
7510 }
7511
7512 // And now add the constant area to the generated code.
7513 assembler->AddConstantArea();
7514 }
7515
7516 // And finish up.
7517 CodeGenerator::Finalize(allocator);
7518}
7519
Mark Mendell0616ae02015-04-17 12:49:27 -04007520Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7521 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7522 return Address(reg, kDummy32BitOffset, fixup);
7523}
7524
7525Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7526 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7527 return Address(reg, kDummy32BitOffset, fixup);
7528}
7529
7530Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7531 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7532 return Address(reg, kDummy32BitOffset, fixup);
7533}
7534
7535Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7536 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7537 return Address(reg, kDummy32BitOffset, fixup);
7538}
7539
Aart Bika19616e2016-02-01 18:57:58 -08007540void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7541 if (value == 0) {
7542 __ xorl(dest, dest);
7543 } else {
7544 __ movl(dest, Immediate(value));
7545 }
7546}
7547
7548void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7549 if (value == 0) {
7550 __ testl(dest, dest);
7551 } else {
7552 __ cmpl(dest, Immediate(value));
7553 }
7554}
7555
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007556void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7557 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007558 GenerateIntCompare(lhs_reg, rhs);
7559}
7560
7561void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007562 if (rhs.IsConstant()) {
7563 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007564 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007565 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007566 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007567 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007568 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007569 }
7570}
7571
7572Address CodeGeneratorX86::ArrayAddress(Register obj,
7573 Location index,
7574 ScaleFactor scale,
7575 uint32_t data_offset) {
7576 return index.IsConstant() ?
7577 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7578 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7579}
7580
Mark Mendell805b3b52015-09-18 14:10:29 -04007581Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7582 Register reg,
7583 Register value) {
7584 // Create a fixup to be used to create and address the jump table.
7585 JumpTableRIPFixup* table_fixup =
7586 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7587
7588 // We have to populate the jump tables.
7589 fixups_to_jump_tables_.push_back(table_fixup);
7590
7591 // We want a scaled address, as we are extracting the correct offset from the table.
7592 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7593}
7594
Andreas Gampe85b62f22015-09-09 13:15:38 -07007595// TODO: target as memory.
7596void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7597 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007598 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007599 return;
7600 }
7601
7602 DCHECK_NE(type, Primitive::kPrimVoid);
7603
7604 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7605 if (target.Equals(return_loc)) {
7606 return;
7607 }
7608
7609 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7610 // with the else branch.
7611 if (type == Primitive::kPrimLong) {
7612 HParallelMove parallel_move(GetGraph()->GetArena());
7613 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7614 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7615 GetMoveResolver()->EmitNativeCode(&parallel_move);
7616 } else {
7617 // Let the parallel move resolver take care of all of this.
7618 HParallelMove parallel_move(GetGraph()->GetArena());
7619 parallel_move.AddMove(return_loc, target, type, nullptr);
7620 GetMoveResolver()->EmitNativeCode(&parallel_move);
7621 }
7622}
7623
Nicolas Geoffray997d1212016-11-09 10:36:29 +00007624void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7625 for (const PatchInfo<Label>& info : jit_string_patches_) {
7626 const auto& it = jit_string_roots_.find(StringReference(&info.dex_file, info.index));
7627 DCHECK(it != jit_string_roots_.end());
7628 size_t index_in_table = it->second;
7629 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7630 uintptr_t address =
7631 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7632 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7633 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7634 dchecked_integral_cast<uint32_t>(address);
7635 }
7636}
7637
Roland Levillain4d027112015-07-01 15:41:14 +01007638#undef __
7639
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007640} // namespace x86
7641} // namespace art