blob: 2a9e21d1e1369f09b4c1a42d399a8d0095312d36 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010029#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010033#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace x86 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050044static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Roland Levillain7cbd27f2016-08-11 23:53:33 +010050// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
51#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Andreas Gampe85b62f22015-09-09 13:15:38 -070054class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000056 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Alexandre Rames2ed20af2015-03-06 13:55:35 +000058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010059 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000061 if (instruction_->CanThrowIntoCatchBlock()) {
62 // Live registers will be restored in the catch block if caught.
63 SaveLiveRegisters(codegen, instruction_->GetLocations());
64 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010065 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010066 instruction_,
67 instruction_->GetDexPc(),
68 this);
Roland Levillain888d0672015-11-23 18:53:50 +000069 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
Alexandre Rames8158f282015-08-07 10:26:17 +010072 bool IsFatal() const OVERRIDE { return true; }
73
Alexandre Rames9931f312015-06-19 14:47:01 +010074 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
75
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
78};
79
Andreas Gampe85b62f22015-09-09 13:15:38 -070080class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000081 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000082 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000083
Alexandre Rames2ed20af2015-03-06 13:55:35 +000084 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010085 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000086 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010087 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000088 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000089 }
90
Alexandre Rames8158f282015-08-07 10:26:17 +010091 bool IsFatal() const OVERRIDE { return true; }
92
Alexandre Rames9931f312015-06-19 14:47:01 +010093 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
94
Calin Juravled0d48522014-11-04 16:40:20 +000095 private:
Calin Juravled0d48522014-11-04 16:40:20 +000096 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
97};
98
Andreas Gampe85b62f22015-09-09 13:15:38 -070099class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000100 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000101 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
102 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000103
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000104 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000106 if (is_div_) {
107 __ negl(reg_);
108 } else {
109 __ movl(reg_, Immediate(0));
110 }
Calin Juravled0d48522014-11-04 16:40:20 +0000111 __ jmp(GetExitLabel());
112 }
113
Alexandre Rames9931f312015-06-19 14:47:01 +0100114 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
115
Calin Juravled0d48522014-11-04 16:40:20 +0000116 private:
117 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000118 bool is_div_;
119 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000120};
121
Andreas Gampe85b62f22015-09-09 13:15:38 -0700122class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100123 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000124 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100125
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000126 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100127 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100128 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000130 // We're moving two locations to locations that could overlap, so we need a parallel
131 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000132 if (instruction_->CanThrowIntoCatchBlock()) {
133 // Live registers will be restored in the catch block if caught.
134 SaveLiveRegisters(codegen, instruction_->GetLocations());
135 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400136
137 // Are we using an array length from memory?
138 HInstruction* array_length = instruction_->InputAt(1);
139 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100140 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400141 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
142 // Load the array length into our temporary.
143 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
144 Location array_loc = array_length->GetLocations()->InAt(0);
145 Address array_len(array_loc.AsRegister<Register>(), len_offset);
146 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
147 // Check for conflicts with index.
148 if (length_loc.Equals(locations->InAt(0))) {
149 // We know we aren't using parameter 2.
150 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
151 }
152 __ movl(length_loc.AsRegister<Register>(), array_len);
jessicahandojo4877b792016-09-08 19:49:13 -0700153 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100154 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700155 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400156 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000157 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100158 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000159 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100160 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400161 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100162 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
163 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100164 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
165 ? kQuickThrowStringBounds
166 : kQuickThrowArrayBounds;
167 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100168 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000169 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100170 }
171
Alexandre Rames8158f282015-08-07 10:26:17 +0100172 bool IsFatal() const OVERRIDE { return true; }
173
Alexandre Rames9931f312015-06-19 14:47:01 +0100174 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
175
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100176 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
178};
179
Andreas Gampe85b62f22015-09-09 13:15:38 -0700180class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000182 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000183 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000184
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000185 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100186 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000187 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100188 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000189 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100190 if (successor_ == nullptr) {
191 __ jmp(GetReturnLabel());
192 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100193 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100194 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000195 }
196
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100197 Label* GetReturnLabel() {
198 DCHECK(successor_ == nullptr);
199 return &return_label_;
200 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000201
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100202 HBasicBlock* GetSuccessor() const {
203 return successor_;
204 }
205
Alexandre Rames9931f312015-06-19 14:47:01 +0100206 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
207
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000208 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100209 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000210 Label return_label_;
211
212 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
213};
214
Vladimir Markoaad75c62016-10-03 08:46:48 +0000215class LoadStringSlowPathX86 : public SlowPathCode {
216 public:
217 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
218
219 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
220 LocationSummary* locations = instruction_->GetLocations();
221 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
222
223 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
224 __ Bind(GetEntryLabel());
225 SaveLiveRegisters(codegen, locations);
226
227 InvokeRuntimeCallingConvention calling_convention;
228 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
229 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
230 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
231 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
232 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
233 RestoreLiveRegisters(codegen, locations);
234
235 // Store the resolved String to the BSS entry.
236 Register method_address = locations->InAt(0).AsRegister<Register>();
237 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
238 locations->Out().AsRegister<Register>());
239 Label* fixup_label = x86_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
240 __ Bind(fixup_label);
241
242 __ jmp(GetExitLabel());
243 }
244
245 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
246
247 private:
248 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
249};
250
Andreas Gampe85b62f22015-09-09 13:15:38 -0700251class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000252 public:
253 LoadClassSlowPathX86(HLoadClass* cls,
254 HInstruction* at,
255 uint32_t dex_pc,
256 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000257 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
259 }
260
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000261 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000262 LocationSummary* locations = at_->GetLocations();
263 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
264 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000265 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266
267 InvokeRuntimeCallingConvention calling_convention;
268 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100269 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
270 : kQuickInitializeType,
Alexandre Rames8158f282015-08-07 10:26:17 +0100271 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000272 if (do_clinit_) {
273 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
274 } else {
275 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
276 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277
278 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000279 Location out = locations->Out();
280 if (out.IsValid()) {
281 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
282 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000285 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000286 __ jmp(GetExitLabel());
287 }
288
Alexandre Rames9931f312015-06-19 14:47:01 +0100289 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
290
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000291 private:
292 // The class this slow path will load.
293 HLoadClass* const cls_;
294
295 // The instruction where this slow path is happening.
296 // (Might be the load class or an initialization check).
297 HInstruction* const at_;
298
299 // The dex PC of `at_`.
300 const uint32_t dex_pc_;
301
302 // Whether to initialize the class.
303 const bool do_clinit_;
304
305 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
306};
307
Andreas Gampe85b62f22015-09-09 13:15:38 -0700308class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000309 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000310 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000311 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000313 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314 LocationSummary* locations = instruction_->GetLocations();
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)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001023 constant_area_start_(-1),
1024 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1025 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001026 // Use a fake return address register to mimic Quick.
1027 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001028}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001029
David Brazdil58282f42016-01-14 12:45:10 +00001030void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001031 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001032 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001033}
1034
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001035InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001036 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001037 assembler_(codegen->GetAssembler()),
1038 codegen_(codegen) {}
1039
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001040static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001041 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001042}
1043
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001044void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001045 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001046 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001047 bool skip_overflow_check =
1048 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001049 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001050
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001051 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001052 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001053 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001054 }
1055
Mark Mendell5f874182015-03-04 15:42:45 -05001056 if (HasEmptyFrame()) {
1057 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001058 }
Mark Mendell5f874182015-03-04 15:42:45 -05001059
1060 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1061 Register reg = kCoreCalleeSaves[i];
1062 if (allocated_registers_.ContainsCoreRegister(reg)) {
1063 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001064 __ cfi().AdjustCFAOffset(kX86WordSize);
1065 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001066 }
1067 }
1068
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001069 int adjust = GetFrameSize() - FrameEntrySpillSize();
1070 __ subl(ESP, Immediate(adjust));
1071 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001072 // Save the current method if we need it. Note that we do not
1073 // do this in HCurrentMethod, as the instruction might have been removed
1074 // in the SSA graph.
1075 if (RequiresCurrentMethod()) {
1076 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1077 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001078}
1079
1080void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001081 __ cfi().RememberState();
1082 if (!HasEmptyFrame()) {
1083 int adjust = GetFrameSize() - FrameEntrySpillSize();
1084 __ addl(ESP, Immediate(adjust));
1085 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001086
David Srbeckyc34dc932015-04-12 09:27:43 +01001087 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1088 Register reg = kCoreCalleeSaves[i];
1089 if (allocated_registers_.ContainsCoreRegister(reg)) {
1090 __ popl(reg);
1091 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1092 __ cfi().Restore(DWARFReg(reg));
1093 }
Mark Mendell5f874182015-03-04 15:42:45 -05001094 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001095 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001096 __ ret();
1097 __ cfi().RestoreState();
1098 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001099}
1100
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001101void CodeGeneratorX86::Bind(HBasicBlock* block) {
1102 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001103}
1104
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001105Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1106 switch (type) {
1107 case Primitive::kPrimBoolean:
1108 case Primitive::kPrimByte:
1109 case Primitive::kPrimChar:
1110 case Primitive::kPrimShort:
1111 case Primitive::kPrimInt:
1112 case Primitive::kPrimNot:
1113 return Location::RegisterLocation(EAX);
1114
1115 case Primitive::kPrimLong:
1116 return Location::RegisterPairLocation(EAX, EDX);
1117
1118 case Primitive::kPrimVoid:
1119 return Location::NoLocation();
1120
1121 case Primitive::kPrimDouble:
1122 case Primitive::kPrimFloat:
1123 return Location::FpuRegisterLocation(XMM0);
1124 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001125
1126 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001127}
1128
1129Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1130 return Location::RegisterLocation(kMethodRegisterArgument);
1131}
1132
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001133Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001134 switch (type) {
1135 case Primitive::kPrimBoolean:
1136 case Primitive::kPrimByte:
1137 case Primitive::kPrimChar:
1138 case Primitive::kPrimShort:
1139 case Primitive::kPrimInt:
1140 case Primitive::kPrimNot: {
1141 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001142 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001143 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001144 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001145 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001146 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001147 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001148 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001149
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001150 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001151 uint32_t index = gp_index_;
1152 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001153 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001154 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001155 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1156 calling_convention.GetRegisterPairAt(index));
1157 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001158 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001159 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1160 }
1161 }
1162
1163 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001164 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001165 stack_index_++;
1166 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1167 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1168 } else {
1169 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1170 }
1171 }
1172
1173 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001174 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001175 stack_index_ += 2;
1176 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1177 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1178 } else {
1179 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001180 }
1181 }
1182
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001183 case Primitive::kPrimVoid:
1184 LOG(FATAL) << "Unexpected parameter type " << type;
1185 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001186 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001187 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001188}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001189
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001190void CodeGeneratorX86::Move32(Location destination, Location source) {
1191 if (source.Equals(destination)) {
1192 return;
1193 }
1194 if (destination.IsRegister()) {
1195 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001196 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001197 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001198 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001199 } else {
1200 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001201 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001202 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001203 } else if (destination.IsFpuRegister()) {
1204 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001205 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001206 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001207 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001208 } else {
1209 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001210 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001211 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001212 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001213 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001214 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001215 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001216 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001217 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001218 } else if (source.IsConstant()) {
1219 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001220 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001221 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001222 } else {
1223 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001224 __ pushl(Address(ESP, source.GetStackIndex()));
1225 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226 }
1227 }
1228}
1229
1230void CodeGeneratorX86::Move64(Location destination, Location source) {
1231 if (source.Equals(destination)) {
1232 return;
1233 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001234 if (destination.IsRegisterPair()) {
1235 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001236 EmitParallelMoves(
1237 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1238 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001239 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001240 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001241 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1242 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001243 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1245 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1246 __ psrlq(src_reg, Immediate(32));
1247 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001248 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001249 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001250 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001251 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1252 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001253 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1254 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001255 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001256 if (source.IsFpuRegister()) {
1257 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1258 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001259 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001260 } else if (source.IsRegisterPair()) {
1261 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1262 // Create stack space for 2 elements.
1263 __ subl(ESP, Immediate(2 * elem_size));
1264 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1265 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1266 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1267 // And remove the temporary stack space we allocated.
1268 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001269 } else {
1270 LOG(FATAL) << "Unimplemented";
1271 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001272 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001273 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001274 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001275 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001276 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001277 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001278 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001279 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001280 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001281 } else if (source.IsConstant()) {
1282 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001283 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1284 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001285 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001286 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1287 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001288 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001289 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001290 EmitParallelMoves(
1291 Location::StackSlot(source.GetStackIndex()),
1292 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001293 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001294 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001295 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1296 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001297 }
1298 }
1299}
1300
Calin Juravle175dc732015-08-25 15:42:32 +01001301void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1302 DCHECK(location.IsRegister());
1303 __ movl(location.AsRegister<Register>(), Immediate(value));
1304}
1305
Calin Juravlee460d1d2015-09-29 04:52:17 +01001306void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001307 HParallelMove move(GetGraph()->GetArena());
1308 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1309 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1310 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001311 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001312 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001313 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001314 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001315}
1316
1317void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1318 if (location.IsRegister()) {
1319 locations->AddTemp(location);
1320 } else if (location.IsRegisterPair()) {
1321 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1322 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1323 } else {
1324 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1325 }
1326}
1327
David Brazdilfc6a86a2015-06-26 10:33:45 +00001328void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001329 DCHECK(!successor->IsExitBlock());
1330
1331 HBasicBlock* block = got->GetBlock();
1332 HInstruction* previous = got->GetPrevious();
1333
1334 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001335 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001336 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1337 return;
1338 }
1339
1340 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1341 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1342 }
1343 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001344 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001345 }
1346}
1347
David Brazdilfc6a86a2015-06-26 10:33:45 +00001348void LocationsBuilderX86::VisitGoto(HGoto* got) {
1349 got->SetLocations(nullptr);
1350}
1351
1352void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1353 HandleGoto(got, got->GetSuccessor());
1354}
1355
1356void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1357 try_boundary->SetLocations(nullptr);
1358}
1359
1360void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1361 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1362 if (!successor->IsExitBlock()) {
1363 HandleGoto(try_boundary, successor);
1364 }
1365}
1366
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001367void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001368 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001369}
1370
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001371void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001372}
1373
Mark Mendell152408f2015-12-31 12:28:50 -05001374template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001375void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001376 LabelType* true_label,
1377 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001378 if (cond->IsFPConditionTrueIfNaN()) {
1379 __ j(kUnordered, true_label);
1380 } else if (cond->IsFPConditionFalseIfNaN()) {
1381 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001382 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001383 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001384}
1385
Mark Mendell152408f2015-12-31 12:28:50 -05001386template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001387void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001388 LabelType* true_label,
1389 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001390 LocationSummary* locations = cond->GetLocations();
1391 Location left = locations->InAt(0);
1392 Location right = locations->InAt(1);
1393 IfCondition if_cond = cond->GetCondition();
1394
Mark Mendellc4701932015-04-10 13:18:51 -04001395 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001396 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001397 IfCondition true_high_cond = if_cond;
1398 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001399 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001400
1401 // Set the conditions for the test, remembering that == needs to be
1402 // decided using the low words.
1403 switch (if_cond) {
1404 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001405 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001406 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001407 break;
1408 case kCondLT:
1409 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001410 break;
1411 case kCondLE:
1412 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001413 break;
1414 case kCondGT:
1415 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001416 break;
1417 case kCondGE:
1418 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001419 break;
Aart Bike9f37602015-10-09 11:15:55 -07001420 case kCondB:
1421 false_high_cond = kCondA;
1422 break;
1423 case kCondBE:
1424 true_high_cond = kCondB;
1425 break;
1426 case kCondA:
1427 false_high_cond = kCondB;
1428 break;
1429 case kCondAE:
1430 true_high_cond = kCondA;
1431 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001432 }
1433
1434 if (right.IsConstant()) {
1435 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001436 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001437 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001438
Aart Bika19616e2016-02-01 18:57:58 -08001439 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001440 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001441 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001442 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001443 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001444 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001445 __ j(X86Condition(true_high_cond), true_label);
1446 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001447 }
1448 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001449 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001450 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001451 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001452 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001453
1454 __ cmpl(left_high, right_high);
1455 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001456 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001457 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001458 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001459 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001460 __ j(X86Condition(true_high_cond), true_label);
1461 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001462 }
1463 // Must be equal high, so compare the lows.
1464 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001465 } else {
1466 DCHECK(right.IsDoubleStackSlot());
1467 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1468 if (if_cond == kCondNE) {
1469 __ j(X86Condition(true_high_cond), true_label);
1470 } else if (if_cond == kCondEQ) {
1471 __ j(X86Condition(false_high_cond), false_label);
1472 } else {
1473 __ j(X86Condition(true_high_cond), true_label);
1474 __ j(X86Condition(false_high_cond), false_label);
1475 }
1476 // Must be equal high, so compare the lows.
1477 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001478 }
1479 // The last comparison might be unsigned.
1480 __ j(final_condition, true_label);
1481}
1482
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001483void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1484 Location rhs,
1485 HInstruction* insn,
1486 bool is_double) {
1487 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1488 if (is_double) {
1489 if (rhs.IsFpuRegister()) {
1490 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1491 } else if (const_area != nullptr) {
1492 DCHECK(const_area->IsEmittedAtUseSite());
1493 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1494 codegen_->LiteralDoubleAddress(
1495 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1496 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1497 } else {
1498 DCHECK(rhs.IsDoubleStackSlot());
1499 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1500 }
1501 } else {
1502 if (rhs.IsFpuRegister()) {
1503 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1504 } else if (const_area != nullptr) {
1505 DCHECK(const_area->IsEmittedAtUseSite());
1506 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1507 codegen_->LiteralFloatAddress(
1508 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1509 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1510 } else {
1511 DCHECK(rhs.IsStackSlot());
1512 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1513 }
1514 }
1515}
1516
Mark Mendell152408f2015-12-31 12:28:50 -05001517template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001518void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001519 LabelType* true_target_in,
1520 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001521 // Generated branching requires both targets to be explicit. If either of the
1522 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001523 LabelType fallthrough_target;
1524 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1525 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001526
Mark Mendellc4701932015-04-10 13:18:51 -04001527 LocationSummary* locations = condition->GetLocations();
1528 Location left = locations->InAt(0);
1529 Location right = locations->InAt(1);
1530
Mark Mendellc4701932015-04-10 13:18:51 -04001531 Primitive::Type type = condition->InputAt(0)->GetType();
1532 switch (type) {
1533 case Primitive::kPrimLong:
1534 GenerateLongComparesAndJumps(condition, true_target, false_target);
1535 break;
1536 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001537 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001538 GenerateFPJumps(condition, true_target, false_target);
1539 break;
1540 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001541 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001542 GenerateFPJumps(condition, true_target, false_target);
1543 break;
1544 default:
1545 LOG(FATAL) << "Unexpected compare type " << type;
1546 }
1547
David Brazdil0debae72015-11-12 18:37:00 +00001548 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001549 __ jmp(false_target);
1550 }
David Brazdil0debae72015-11-12 18:37:00 +00001551
1552 if (fallthrough_target.IsLinked()) {
1553 __ Bind(&fallthrough_target);
1554 }
Mark Mendellc4701932015-04-10 13:18:51 -04001555}
1556
David Brazdil0debae72015-11-12 18:37:00 +00001557static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1558 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1559 // are set only strictly before `branch`. We can't use the eflags on long/FP
1560 // conditions if they are materialized due to the complex branching.
1561 return cond->IsCondition() &&
1562 cond->GetNext() == branch &&
1563 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1564 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1565}
1566
Mark Mendell152408f2015-12-31 12:28:50 -05001567template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001568void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001569 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001570 LabelType* true_target,
1571 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001572 HInstruction* cond = instruction->InputAt(condition_input_index);
1573
1574 if (true_target == nullptr && false_target == nullptr) {
1575 // Nothing to do. The code always falls through.
1576 return;
1577 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001578 // Constant condition, statically compared against "true" (integer value 1).
1579 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001580 if (true_target != nullptr) {
1581 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001582 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001583 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001584 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001585 if (false_target != nullptr) {
1586 __ jmp(false_target);
1587 }
1588 }
1589 return;
1590 }
1591
1592 // The following code generates these patterns:
1593 // (1) true_target == nullptr && false_target != nullptr
1594 // - opposite condition true => branch to false_target
1595 // (2) true_target != nullptr && false_target == nullptr
1596 // - condition true => branch to true_target
1597 // (3) true_target != nullptr && false_target != nullptr
1598 // - condition true => branch to true_target
1599 // - branch to false_target
1600 if (IsBooleanValueOrMaterializedCondition(cond)) {
1601 if (AreEflagsSetFrom(cond, instruction)) {
1602 if (true_target == nullptr) {
1603 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1604 } else {
1605 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1606 }
1607 } else {
1608 // Materialized condition, compare against 0.
1609 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1610 if (lhs.IsRegister()) {
1611 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1612 } else {
1613 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1614 }
1615 if (true_target == nullptr) {
1616 __ j(kEqual, false_target);
1617 } else {
1618 __ j(kNotEqual, true_target);
1619 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001620 }
1621 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001622 // Condition has not been materialized, use its inputs as the comparison and
1623 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001624 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001625
1626 // If this is a long or FP comparison that has been folded into
1627 // the HCondition, generate the comparison directly.
1628 Primitive::Type type = condition->InputAt(0)->GetType();
1629 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1630 GenerateCompareTestAndBranch(condition, true_target, false_target);
1631 return;
1632 }
1633
1634 Location lhs = condition->GetLocations()->InAt(0);
1635 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001636 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001637 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001638 if (true_target == nullptr) {
1639 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1640 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001641 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001642 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001643 }
David Brazdil0debae72015-11-12 18:37:00 +00001644
1645 // If neither branch falls through (case 3), the conditional branch to `true_target`
1646 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1647 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001648 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001649 }
1650}
1651
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001652void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001653 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1654 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001655 locations->SetInAt(0, Location::Any());
1656 }
1657}
1658
1659void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001660 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1661 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1662 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1663 nullptr : codegen_->GetLabelOf(true_successor);
1664 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1665 nullptr : codegen_->GetLabelOf(false_successor);
1666 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001667}
1668
1669void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1670 LocationSummary* locations = new (GetGraph()->GetArena())
1671 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001672 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001673 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001674 locations->SetInAt(0, Location::Any());
1675 }
1676}
1677
1678void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001679 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001680 GenerateTestAndBranch<Label>(deoptimize,
1681 /* condition_input_index */ 0,
1682 slow_path->GetEntryLabel(),
1683 /* false_target */ nullptr);
1684}
1685
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001686static bool SelectCanUseCMOV(HSelect* select) {
1687 // There are no conditional move instructions for XMMs.
1688 if (Primitive::IsFloatingPointType(select->GetType())) {
1689 return false;
1690 }
1691
1692 // A FP condition doesn't generate the single CC that we need.
1693 // In 32 bit mode, a long condition doesn't generate a single CC either.
1694 HInstruction* condition = select->GetCondition();
1695 if (condition->IsCondition()) {
1696 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1697 if (compare_type == Primitive::kPrimLong ||
1698 Primitive::IsFloatingPointType(compare_type)) {
1699 return false;
1700 }
1701 }
1702
1703 // We can generate a CMOV for this Select.
1704 return true;
1705}
1706
David Brazdil74eb1b22015-12-14 11:44:01 +00001707void LocationsBuilderX86::VisitSelect(HSelect* select) {
1708 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001709 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001710 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001711 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001712 } else {
1713 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001714 if (SelectCanUseCMOV(select)) {
1715 if (select->InputAt(1)->IsConstant()) {
1716 // Cmov can't handle a constant value.
1717 locations->SetInAt(1, Location::RequiresRegister());
1718 } else {
1719 locations->SetInAt(1, Location::Any());
1720 }
1721 } else {
1722 locations->SetInAt(1, Location::Any());
1723 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001724 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001725 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1726 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001727 }
1728 locations->SetOut(Location::SameAsFirstInput());
1729}
1730
1731void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1732 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001733 DCHECK(locations->InAt(0).Equals(locations->Out()));
1734 if (SelectCanUseCMOV(select)) {
1735 // If both the condition and the source types are integer, we can generate
1736 // a CMOV to implement Select.
1737
1738 HInstruction* select_condition = select->GetCondition();
1739 Condition cond = kNotEqual;
1740
1741 // Figure out how to test the 'condition'.
1742 if (select_condition->IsCondition()) {
1743 HCondition* condition = select_condition->AsCondition();
1744 if (!condition->IsEmittedAtUseSite()) {
1745 // This was a previously materialized condition.
1746 // Can we use the existing condition code?
1747 if (AreEflagsSetFrom(condition, select)) {
1748 // Materialization was the previous instruction. Condition codes are right.
1749 cond = X86Condition(condition->GetCondition());
1750 } else {
1751 // No, we have to recreate the condition code.
1752 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1753 __ testl(cond_reg, cond_reg);
1754 }
1755 } else {
1756 // We can't handle FP or long here.
1757 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1758 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1759 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001760 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001761 cond = X86Condition(condition->GetCondition());
1762 }
1763 } else {
1764 // Must be a boolean condition, which needs to be compared to 0.
1765 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1766 __ testl(cond_reg, cond_reg);
1767 }
1768
1769 // If the condition is true, overwrite the output, which already contains false.
1770 Location false_loc = locations->InAt(0);
1771 Location true_loc = locations->InAt(1);
1772 if (select->GetType() == Primitive::kPrimLong) {
1773 // 64 bit conditional move.
1774 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1775 Register false_low = false_loc.AsRegisterPairLow<Register>();
1776 if (true_loc.IsRegisterPair()) {
1777 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1778 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1779 } else {
1780 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1781 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1782 }
1783 } else {
1784 // 32 bit conditional move.
1785 Register false_reg = false_loc.AsRegister<Register>();
1786 if (true_loc.IsRegister()) {
1787 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1788 } else {
1789 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1790 }
1791 }
1792 } else {
1793 NearLabel false_target;
1794 GenerateTestAndBranch<NearLabel>(
1795 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1796 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1797 __ Bind(&false_target);
1798 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001799}
1800
David Srbecky0cf44932015-12-09 14:09:59 +00001801void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1802 new (GetGraph()->GetArena()) LocationSummary(info);
1803}
1804
David Srbeckyd28f4a02016-03-14 17:14:24 +00001805void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1806 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001807}
1808
1809void CodeGeneratorX86::GenerateNop() {
1810 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001811}
1812
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001813void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001814 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001815 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001816 // Handle the long/FP comparisons made in instruction simplification.
1817 switch (cond->InputAt(0)->GetType()) {
1818 case Primitive::kPrimLong: {
1819 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001820 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001821 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001822 locations->SetOut(Location::RequiresRegister());
1823 }
1824 break;
1825 }
1826 case Primitive::kPrimFloat:
1827 case Primitive::kPrimDouble: {
1828 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001829 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1830 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1831 } else if (cond->InputAt(1)->IsConstant()) {
1832 locations->SetInAt(1, Location::RequiresFpuRegister());
1833 } else {
1834 locations->SetInAt(1, Location::Any());
1835 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001836 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001837 locations->SetOut(Location::RequiresRegister());
1838 }
1839 break;
1840 }
1841 default:
1842 locations->SetInAt(0, Location::RequiresRegister());
1843 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001844 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001845 // We need a byte register.
1846 locations->SetOut(Location::RegisterLocation(ECX));
1847 }
1848 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001849 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001850}
1851
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001852void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001853 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001854 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001855 }
Mark Mendellc4701932015-04-10 13:18:51 -04001856
1857 LocationSummary* locations = cond->GetLocations();
1858 Location lhs = locations->InAt(0);
1859 Location rhs = locations->InAt(1);
1860 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001861 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001862
1863 switch (cond->InputAt(0)->GetType()) {
1864 default: {
1865 // Integer case.
1866
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001867 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001868 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001869 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001870 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001871 return;
1872 }
1873 case Primitive::kPrimLong:
1874 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1875 break;
1876 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001877 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001878 GenerateFPJumps(cond, &true_label, &false_label);
1879 break;
1880 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001881 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001882 GenerateFPJumps(cond, &true_label, &false_label);
1883 break;
1884 }
1885
1886 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001887 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001888
Roland Levillain4fa13f62015-07-06 18:11:54 +01001889 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001890 __ Bind(&false_label);
1891 __ xorl(reg, reg);
1892 __ jmp(&done_label);
1893
Roland Levillain4fa13f62015-07-06 18:11:54 +01001894 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001895 __ Bind(&true_label);
1896 __ movl(reg, Immediate(1));
1897 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001898}
1899
1900void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001901 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001902}
1903
1904void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001905 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001906}
1907
1908void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001909 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001910}
1911
1912void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001913 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001914}
1915
1916void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001917 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001918}
1919
1920void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001921 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001922}
1923
1924void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001925 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001926}
1927
1928void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001929 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001930}
1931
1932void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001933 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001934}
1935
1936void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001937 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001938}
1939
1940void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001941 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001942}
1943
1944void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001945 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001946}
1947
Aart Bike9f37602015-10-09 11:15:55 -07001948void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001949 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001950}
1951
1952void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001953 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001954}
1955
1956void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001957 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001958}
1959
1960void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001961 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001962}
1963
1964void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001965 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001966}
1967
1968void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001969 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001970}
1971
1972void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001973 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001974}
1975
1976void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001977 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001978}
1979
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001980void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001981 LocationSummary* locations =
1982 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001983 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001984}
1985
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001986void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001987 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001988}
1989
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001990void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1991 LocationSummary* locations =
1992 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1993 locations->SetOut(Location::ConstantLocation(constant));
1994}
1995
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001996void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001997 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001998}
1999
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002000void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002001 LocationSummary* locations =
2002 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002003 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002004}
2005
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002006void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002007 // Will be generated at use site.
2008}
2009
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002010void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2011 LocationSummary* locations =
2012 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2013 locations->SetOut(Location::ConstantLocation(constant));
2014}
2015
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002016void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002017 // Will be generated at use site.
2018}
2019
2020void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2021 LocationSummary* locations =
2022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2023 locations->SetOut(Location::ConstantLocation(constant));
2024}
2025
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002026void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002027 // Will be generated at use site.
2028}
2029
Calin Juravle27df7582015-04-17 19:12:31 +01002030void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2031 memory_barrier->SetLocations(nullptr);
2032}
2033
2034void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002035 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002036}
2037
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002038void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002039 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002040}
2041
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002042void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002043 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002044}
2045
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002046void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002047 LocationSummary* locations =
2048 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002049 switch (ret->InputAt(0)->GetType()) {
2050 case Primitive::kPrimBoolean:
2051 case Primitive::kPrimByte:
2052 case Primitive::kPrimChar:
2053 case Primitive::kPrimShort:
2054 case Primitive::kPrimInt:
2055 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002056 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002057 break;
2058
2059 case Primitive::kPrimLong:
2060 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002061 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002062 break;
2063
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002064 case Primitive::kPrimFloat:
2065 case Primitive::kPrimDouble:
2066 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002067 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002068 break;
2069
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002070 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002071 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002072 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002073}
2074
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002075void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002076 if (kIsDebugBuild) {
2077 switch (ret->InputAt(0)->GetType()) {
2078 case Primitive::kPrimBoolean:
2079 case Primitive::kPrimByte:
2080 case Primitive::kPrimChar:
2081 case Primitive::kPrimShort:
2082 case Primitive::kPrimInt:
2083 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002084 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002085 break;
2086
2087 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002088 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2089 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002090 break;
2091
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002092 case Primitive::kPrimFloat:
2093 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002094 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002095 break;
2096
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002097 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002098 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002099 }
2100 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002101 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002102}
2103
Calin Juravle175dc732015-08-25 15:42:32 +01002104void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2105 // The trampoline uses the same calling convention as dex calling conventions,
2106 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2107 // the method_idx.
2108 HandleInvoke(invoke);
2109}
2110
2111void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2112 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2113}
2114
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002115void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002116 // Explicit clinit checks triggered by static invokes must have been pruned by
2117 // art::PrepareForRegisterAllocation.
2118 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002119
Mark Mendellfb8d2792015-03-31 22:16:59 -04002120 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002121 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002122 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002123 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002124 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002125 return;
2126 }
2127
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002128 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002129
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002130 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2131 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002132 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002133 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002134}
2135
Mark Mendell09ed1a32015-03-25 08:30:06 -04002136static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2137 if (invoke->GetLocations()->Intrinsified()) {
2138 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2139 intrinsic.Dispatch(invoke);
2140 return true;
2141 }
2142 return false;
2143}
2144
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002145void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002146 // Explicit clinit checks triggered by static invokes must have been pruned by
2147 // art::PrepareForRegisterAllocation.
2148 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002149
Mark Mendell09ed1a32015-03-25 08:30:06 -04002150 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2151 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002152 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002153
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002154 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002155 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002156 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002157 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002158}
2159
2160void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002161 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2162 if (intrinsic.TryDispatch(invoke)) {
2163 return;
2164 }
2165
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002166 HandleInvoke(invoke);
2167}
2168
2169void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002170 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002171 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002172}
2173
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002174void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002175 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2176 return;
2177 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002178
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002179 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002180 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002181 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002182}
2183
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002184void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002185 // This call to HandleInvoke allocates a temporary (core) register
2186 // which is also used to transfer the hidden argument from FP to
2187 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002188 HandleInvoke(invoke);
2189 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002190 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002191}
2192
2193void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2194 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002195 LocationSummary* locations = invoke->GetLocations();
2196 Register temp = locations->GetTemp(0).AsRegister<Register>();
2197 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002198 Location receiver = locations->InAt(0);
2199 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2200
Roland Levillain0d5a2812015-11-13 10:07:31 +00002201 // Set the hidden argument. This is safe to do this here, as XMM7
2202 // won't be modified thereafter, before the `call` instruction.
2203 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002204 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002205 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002206
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002207 if (receiver.IsStackSlot()) {
2208 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002209 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002210 __ movl(temp, Address(temp, class_offset));
2211 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002212 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002213 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002214 }
Roland Levillain4d027112015-07-01 15:41:14 +01002215 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002216 // Instead of simply (possibly) unpoisoning `temp` here, we should
2217 // emit a read barrier for the previous class reference load.
2218 // However this is not required in practice, as this is an
2219 // intermediate/temporary reference and because the current
2220 // concurrent copying collector keeps the from-space memory
2221 // intact/accessible until the end of the marking phase (the
2222 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002223 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002224 // temp = temp->GetAddressOfIMT()
2225 __ movl(temp,
2226 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002227 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002228 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002229 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002230 __ movl(temp, Address(temp, method_offset));
2231 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002232 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002233 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002234
2235 DCHECK(!codegen_->IsLeafMethod());
2236 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2237}
2238
Roland Levillain88cb1752014-10-20 16:36:47 +01002239void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2240 LocationSummary* locations =
2241 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2242 switch (neg->GetResultType()) {
2243 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002244 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002245 locations->SetInAt(0, Location::RequiresRegister());
2246 locations->SetOut(Location::SameAsFirstInput());
2247 break;
2248
Roland Levillain88cb1752014-10-20 16:36:47 +01002249 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002250 locations->SetInAt(0, Location::RequiresFpuRegister());
2251 locations->SetOut(Location::SameAsFirstInput());
2252 locations->AddTemp(Location::RequiresRegister());
2253 locations->AddTemp(Location::RequiresFpuRegister());
2254 break;
2255
Roland Levillain88cb1752014-10-20 16:36:47 +01002256 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002257 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002258 locations->SetOut(Location::SameAsFirstInput());
2259 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002260 break;
2261
2262 default:
2263 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2264 }
2265}
2266
2267void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2268 LocationSummary* locations = neg->GetLocations();
2269 Location out = locations->Out();
2270 Location in = locations->InAt(0);
2271 switch (neg->GetResultType()) {
2272 case Primitive::kPrimInt:
2273 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002274 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002275 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002276 break;
2277
2278 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002279 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002280 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002281 __ negl(out.AsRegisterPairLow<Register>());
2282 // Negation is similar to subtraction from zero. The least
2283 // significant byte triggers a borrow when it is different from
2284 // zero; to take it into account, add 1 to the most significant
2285 // byte if the carry flag (CF) is set to 1 after the first NEGL
2286 // operation.
2287 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2288 __ negl(out.AsRegisterPairHigh<Register>());
2289 break;
2290
Roland Levillain5368c212014-11-27 15:03:41 +00002291 case Primitive::kPrimFloat: {
2292 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002293 Register constant = locations->GetTemp(0).AsRegister<Register>();
2294 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002295 // Implement float negation with an exclusive or with value
2296 // 0x80000000 (mask for bit 31, representing the sign of a
2297 // single-precision floating-point number).
2298 __ movl(constant, Immediate(INT32_C(0x80000000)));
2299 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002300 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002301 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002302 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002303
Roland Levillain5368c212014-11-27 15:03:41 +00002304 case Primitive::kPrimDouble: {
2305 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002306 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002307 // Implement double negation with an exclusive or with value
2308 // 0x8000000000000000 (mask for bit 63, representing the sign of
2309 // a double-precision floating-point number).
2310 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002311 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002312 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002313 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002314
2315 default:
2316 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2317 }
2318}
2319
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002320void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2321 LocationSummary* locations =
2322 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2323 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2324 locations->SetInAt(0, Location::RequiresFpuRegister());
2325 locations->SetInAt(1, Location::RequiresRegister());
2326 locations->SetOut(Location::SameAsFirstInput());
2327 locations->AddTemp(Location::RequiresFpuRegister());
2328}
2329
2330void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2331 LocationSummary* locations = neg->GetLocations();
2332 Location out = locations->Out();
2333 DCHECK(locations->InAt(0).Equals(out));
2334
2335 Register constant_area = locations->InAt(1).AsRegister<Register>();
2336 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2337 if (neg->GetType() == Primitive::kPrimFloat) {
2338 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2339 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2340 } else {
2341 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2342 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2343 }
2344}
2345
Roland Levillaindff1f282014-11-05 14:15:05 +00002346void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002347 Primitive::Type result_type = conversion->GetResultType();
2348 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002349 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002350
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002351 // The float-to-long and double-to-long type conversions rely on a
2352 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002353 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002354 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2355 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002356 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002357 : LocationSummary::kNoCall;
2358 LocationSummary* locations =
2359 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2360
David Brazdilb2bd1c52015-03-25 11:17:37 +00002361 // The Java language does not allow treating boolean as an integral type but
2362 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002363
Roland Levillaindff1f282014-11-05 14:15:05 +00002364 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002365 case Primitive::kPrimByte:
2366 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002367 case Primitive::kPrimLong: {
2368 // Type conversion from long to byte is a result of code transformations.
2369 HInstruction* input = conversion->InputAt(0);
2370 Location input_location = input->IsConstant()
2371 ? Location::ConstantLocation(input->AsConstant())
2372 : Location::RegisterPairLocation(EAX, EDX);
2373 locations->SetInAt(0, input_location);
2374 // Make the output overlap to please the register allocator. This greatly simplifies
2375 // the validation of the linear scan implementation
2376 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2377 break;
2378 }
David Brazdil46e2a392015-03-16 17:31:52 +00002379 case Primitive::kPrimBoolean:
2380 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002381 case Primitive::kPrimShort:
2382 case Primitive::kPrimInt:
2383 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002384 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002385 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2386 // Make the output overlap to please the register allocator. This greatly simplifies
2387 // the validation of the linear scan implementation
2388 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002389 break;
2390
2391 default:
2392 LOG(FATAL) << "Unexpected type conversion from " << input_type
2393 << " to " << result_type;
2394 }
2395 break;
2396
Roland Levillain01a8d712014-11-14 16:27:39 +00002397 case Primitive::kPrimShort:
2398 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002399 case Primitive::kPrimLong:
2400 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002401 case Primitive::kPrimBoolean:
2402 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002403 case Primitive::kPrimByte:
2404 case Primitive::kPrimInt:
2405 case Primitive::kPrimChar:
2406 // Processing a Dex `int-to-short' instruction.
2407 locations->SetInAt(0, Location::Any());
2408 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2409 break;
2410
2411 default:
2412 LOG(FATAL) << "Unexpected type conversion from " << input_type
2413 << " to " << result_type;
2414 }
2415 break;
2416
Roland Levillain946e1432014-11-11 17:35:19 +00002417 case Primitive::kPrimInt:
2418 switch (input_type) {
2419 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002420 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002421 locations->SetInAt(0, Location::Any());
2422 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2423 break;
2424
2425 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002426 // Processing a Dex `float-to-int' instruction.
2427 locations->SetInAt(0, Location::RequiresFpuRegister());
2428 locations->SetOut(Location::RequiresRegister());
2429 locations->AddTemp(Location::RequiresFpuRegister());
2430 break;
2431
Roland Levillain946e1432014-11-11 17:35:19 +00002432 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002433 // Processing a Dex `double-to-int' instruction.
2434 locations->SetInAt(0, Location::RequiresFpuRegister());
2435 locations->SetOut(Location::RequiresRegister());
2436 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002437 break;
2438
2439 default:
2440 LOG(FATAL) << "Unexpected type conversion from " << input_type
2441 << " to " << result_type;
2442 }
2443 break;
2444
Roland Levillaindff1f282014-11-05 14:15:05 +00002445 case Primitive::kPrimLong:
2446 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002447 case Primitive::kPrimBoolean:
2448 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002449 case Primitive::kPrimByte:
2450 case Primitive::kPrimShort:
2451 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002452 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002453 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002454 locations->SetInAt(0, Location::RegisterLocation(EAX));
2455 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2456 break;
2457
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002458 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002459 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002460 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002461 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002462 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2463 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2464
Vladimir Marko949c91f2015-01-27 10:48:44 +00002465 // The runtime helper puts the result in EAX, EDX.
2466 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002467 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002468 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002469
2470 default:
2471 LOG(FATAL) << "Unexpected type conversion from " << input_type
2472 << " to " << result_type;
2473 }
2474 break;
2475
Roland Levillain981e4542014-11-14 11:47:14 +00002476 case Primitive::kPrimChar:
2477 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002478 case Primitive::kPrimLong:
2479 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002480 case Primitive::kPrimBoolean:
2481 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002482 case Primitive::kPrimByte:
2483 case Primitive::kPrimShort:
2484 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002485 // Processing a Dex `int-to-char' instruction.
2486 locations->SetInAt(0, Location::Any());
2487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2488 break;
2489
2490 default:
2491 LOG(FATAL) << "Unexpected type conversion from " << input_type
2492 << " to " << result_type;
2493 }
2494 break;
2495
Roland Levillaindff1f282014-11-05 14:15:05 +00002496 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002497 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002498 case Primitive::kPrimBoolean:
2499 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002500 case Primitive::kPrimByte:
2501 case Primitive::kPrimShort:
2502 case Primitive::kPrimInt:
2503 case Primitive::kPrimChar:
2504 // Processing a Dex `int-to-float' instruction.
2505 locations->SetInAt(0, Location::RequiresRegister());
2506 locations->SetOut(Location::RequiresFpuRegister());
2507 break;
2508
2509 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002510 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002511 locations->SetInAt(0, Location::Any());
2512 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002513 break;
2514
Roland Levillaincff13742014-11-17 14:32:17 +00002515 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002516 // Processing a Dex `double-to-float' instruction.
2517 locations->SetInAt(0, Location::RequiresFpuRegister());
2518 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002519 break;
2520
2521 default:
2522 LOG(FATAL) << "Unexpected type conversion from " << input_type
2523 << " to " << result_type;
2524 };
2525 break;
2526
Roland Levillaindff1f282014-11-05 14:15:05 +00002527 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002528 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002529 case Primitive::kPrimBoolean:
2530 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002531 case Primitive::kPrimByte:
2532 case Primitive::kPrimShort:
2533 case Primitive::kPrimInt:
2534 case Primitive::kPrimChar:
2535 // Processing a Dex `int-to-double' instruction.
2536 locations->SetInAt(0, Location::RequiresRegister());
2537 locations->SetOut(Location::RequiresFpuRegister());
2538 break;
2539
2540 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002541 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002542 locations->SetInAt(0, Location::Any());
2543 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002544 break;
2545
Roland Levillaincff13742014-11-17 14:32:17 +00002546 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002547 // Processing a Dex `float-to-double' instruction.
2548 locations->SetInAt(0, Location::RequiresFpuRegister());
2549 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002550 break;
2551
2552 default:
2553 LOG(FATAL) << "Unexpected type conversion from " << input_type
2554 << " to " << result_type;
2555 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002556 break;
2557
2558 default:
2559 LOG(FATAL) << "Unexpected type conversion from " << input_type
2560 << " to " << result_type;
2561 }
2562}
2563
2564void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2565 LocationSummary* locations = conversion->GetLocations();
2566 Location out = locations->Out();
2567 Location in = locations->InAt(0);
2568 Primitive::Type result_type = conversion->GetResultType();
2569 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002570 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002571 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002572 case Primitive::kPrimByte:
2573 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002574 case Primitive::kPrimLong:
2575 // Type conversion from long to byte is a result of code transformations.
2576 if (in.IsRegisterPair()) {
2577 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2578 } else {
2579 DCHECK(in.GetConstant()->IsLongConstant());
2580 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2581 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2582 }
2583 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002584 case Primitive::kPrimBoolean:
2585 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002586 case Primitive::kPrimShort:
2587 case Primitive::kPrimInt:
2588 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002589 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002590 if (in.IsRegister()) {
2591 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002592 } else {
2593 DCHECK(in.GetConstant()->IsIntConstant());
2594 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2595 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2596 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002597 break;
2598
2599 default:
2600 LOG(FATAL) << "Unexpected type conversion from " << input_type
2601 << " to " << result_type;
2602 }
2603 break;
2604
Roland Levillain01a8d712014-11-14 16:27:39 +00002605 case Primitive::kPrimShort:
2606 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002607 case Primitive::kPrimLong:
2608 // Type conversion from long to short is a result of code transformations.
2609 if (in.IsRegisterPair()) {
2610 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2611 } else if (in.IsDoubleStackSlot()) {
2612 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2613 } else {
2614 DCHECK(in.GetConstant()->IsLongConstant());
2615 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2616 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2617 }
2618 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002619 case Primitive::kPrimBoolean:
2620 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002621 case Primitive::kPrimByte:
2622 case Primitive::kPrimInt:
2623 case Primitive::kPrimChar:
2624 // Processing a Dex `int-to-short' instruction.
2625 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002626 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002627 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002628 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002629 } else {
2630 DCHECK(in.GetConstant()->IsIntConstant());
2631 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002632 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002633 }
2634 break;
2635
2636 default:
2637 LOG(FATAL) << "Unexpected type conversion from " << input_type
2638 << " to " << result_type;
2639 }
2640 break;
2641
Roland Levillain946e1432014-11-11 17:35:19 +00002642 case Primitive::kPrimInt:
2643 switch (input_type) {
2644 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002645 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002646 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002647 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002648 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002649 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002650 } else {
2651 DCHECK(in.IsConstant());
2652 DCHECK(in.GetConstant()->IsLongConstant());
2653 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002654 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002655 }
2656 break;
2657
Roland Levillain3f8f9362014-12-02 17:45:01 +00002658 case Primitive::kPrimFloat: {
2659 // Processing a Dex `float-to-int' instruction.
2660 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2661 Register output = out.AsRegister<Register>();
2662 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002663 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002664
2665 __ movl(output, Immediate(kPrimIntMax));
2666 // temp = int-to-float(output)
2667 __ cvtsi2ss(temp, output);
2668 // if input >= temp goto done
2669 __ comiss(input, temp);
2670 __ j(kAboveEqual, &done);
2671 // if input == NaN goto nan
2672 __ j(kUnordered, &nan);
2673 // output = float-to-int-truncate(input)
2674 __ cvttss2si(output, input);
2675 __ jmp(&done);
2676 __ Bind(&nan);
2677 // output = 0
2678 __ xorl(output, output);
2679 __ Bind(&done);
2680 break;
2681 }
2682
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002683 case Primitive::kPrimDouble: {
2684 // Processing a Dex `double-to-int' instruction.
2685 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2686 Register output = out.AsRegister<Register>();
2687 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002688 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002689
2690 __ movl(output, Immediate(kPrimIntMax));
2691 // temp = int-to-double(output)
2692 __ cvtsi2sd(temp, output);
2693 // if input >= temp goto done
2694 __ comisd(input, temp);
2695 __ j(kAboveEqual, &done);
2696 // if input == NaN goto nan
2697 __ j(kUnordered, &nan);
2698 // output = double-to-int-truncate(input)
2699 __ cvttsd2si(output, input);
2700 __ jmp(&done);
2701 __ Bind(&nan);
2702 // output = 0
2703 __ xorl(output, output);
2704 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002705 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002706 }
Roland Levillain946e1432014-11-11 17:35:19 +00002707
2708 default:
2709 LOG(FATAL) << "Unexpected type conversion from " << input_type
2710 << " to " << result_type;
2711 }
2712 break;
2713
Roland Levillaindff1f282014-11-05 14:15:05 +00002714 case Primitive::kPrimLong:
2715 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002716 case Primitive::kPrimBoolean:
2717 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002718 case Primitive::kPrimByte:
2719 case Primitive::kPrimShort:
2720 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002721 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002722 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002723 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2724 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002725 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002726 __ cdq();
2727 break;
2728
2729 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002730 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002731 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002732 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002733 break;
2734
Roland Levillaindff1f282014-11-05 14:15:05 +00002735 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002736 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002737 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002738 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002739 break;
2740
2741 default:
2742 LOG(FATAL) << "Unexpected type conversion from " << input_type
2743 << " to " << result_type;
2744 }
2745 break;
2746
Roland Levillain981e4542014-11-14 11:47:14 +00002747 case Primitive::kPrimChar:
2748 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002749 case Primitive::kPrimLong:
2750 // Type conversion from long to short is a result of code transformations.
2751 if (in.IsRegisterPair()) {
2752 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2753 } else if (in.IsDoubleStackSlot()) {
2754 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2755 } else {
2756 DCHECK(in.GetConstant()->IsLongConstant());
2757 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2758 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2759 }
2760 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002761 case Primitive::kPrimBoolean:
2762 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002763 case Primitive::kPrimByte:
2764 case Primitive::kPrimShort:
2765 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002766 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2767 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002768 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002769 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002770 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002771 } else {
2772 DCHECK(in.GetConstant()->IsIntConstant());
2773 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002774 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002775 }
2776 break;
2777
2778 default:
2779 LOG(FATAL) << "Unexpected type conversion from " << input_type
2780 << " to " << result_type;
2781 }
2782 break;
2783
Roland Levillaindff1f282014-11-05 14:15:05 +00002784 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002785 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002786 case Primitive::kPrimBoolean:
2787 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002788 case Primitive::kPrimByte:
2789 case Primitive::kPrimShort:
2790 case Primitive::kPrimInt:
2791 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002792 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002793 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002794 break;
2795
Roland Levillain6d0e4832014-11-27 18:31:21 +00002796 case Primitive::kPrimLong: {
2797 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002798 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002799
Roland Levillain232ade02015-04-20 15:14:36 +01002800 // Create stack space for the call to
2801 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2802 // TODO: enhance register allocator to ask for stack temporaries.
2803 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2804 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2805 __ subl(ESP, Immediate(adjustment));
2806 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002807
Roland Levillain232ade02015-04-20 15:14:36 +01002808 // Load the value to the FP stack, using temporaries if needed.
2809 PushOntoFPStack(in, 0, adjustment, false, true);
2810
2811 if (out.IsStackSlot()) {
2812 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2813 } else {
2814 __ fstps(Address(ESP, 0));
2815 Location stack_temp = Location::StackSlot(0);
2816 codegen_->Move32(out, stack_temp);
2817 }
2818
2819 // Remove the temporary stack space we allocated.
2820 if (adjustment != 0) {
2821 __ addl(ESP, Immediate(adjustment));
2822 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002823 break;
2824 }
2825
Roland Levillaincff13742014-11-17 14:32:17 +00002826 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002827 // Processing a Dex `double-to-float' instruction.
2828 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002829 break;
2830
2831 default:
2832 LOG(FATAL) << "Unexpected type conversion from " << input_type
2833 << " to " << result_type;
2834 };
2835 break;
2836
Roland Levillaindff1f282014-11-05 14:15:05 +00002837 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002838 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002839 case Primitive::kPrimBoolean:
2840 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002841 case Primitive::kPrimByte:
2842 case Primitive::kPrimShort:
2843 case Primitive::kPrimInt:
2844 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002845 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002846 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002847 break;
2848
Roland Levillain647b9ed2014-11-27 12:06:00 +00002849 case Primitive::kPrimLong: {
2850 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002851 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002852
Roland Levillain232ade02015-04-20 15:14:36 +01002853 // Create stack space for the call to
2854 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2855 // TODO: enhance register allocator to ask for stack temporaries.
2856 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2857 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2858 __ subl(ESP, Immediate(adjustment));
2859 }
2860
2861 // Load the value to the FP stack, using temporaries if needed.
2862 PushOntoFPStack(in, 0, adjustment, false, true);
2863
2864 if (out.IsDoubleStackSlot()) {
2865 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2866 } else {
2867 __ fstpl(Address(ESP, 0));
2868 Location stack_temp = Location::DoubleStackSlot(0);
2869 codegen_->Move64(out, stack_temp);
2870 }
2871
2872 // Remove the temporary stack space we allocated.
2873 if (adjustment != 0) {
2874 __ addl(ESP, Immediate(adjustment));
2875 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002876 break;
2877 }
2878
Roland Levillaincff13742014-11-17 14:32:17 +00002879 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002880 // Processing a Dex `float-to-double' instruction.
2881 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002882 break;
2883
2884 default:
2885 LOG(FATAL) << "Unexpected type conversion from " << input_type
2886 << " to " << result_type;
2887 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002888 break;
2889
2890 default:
2891 LOG(FATAL) << "Unexpected type conversion from " << input_type
2892 << " to " << result_type;
2893 }
2894}
2895
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002896void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002897 LocationSummary* locations =
2898 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002899 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002900 case Primitive::kPrimInt: {
2901 locations->SetInAt(0, Location::RequiresRegister());
2902 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2903 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2904 break;
2905 }
2906
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002907 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002908 locations->SetInAt(0, Location::RequiresRegister());
2909 locations->SetInAt(1, Location::Any());
2910 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002911 break;
2912 }
2913
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002914 case Primitive::kPrimFloat:
2915 case Primitive::kPrimDouble: {
2916 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002917 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2918 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002919 } else if (add->InputAt(1)->IsConstant()) {
2920 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002921 } else {
2922 locations->SetInAt(1, Location::Any());
2923 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002924 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002925 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002926 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002927
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002928 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002929 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2930 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002931 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002932}
2933
2934void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2935 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002936 Location first = locations->InAt(0);
2937 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002938 Location out = locations->Out();
2939
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002940 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002941 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002942 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002943 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2944 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002945 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2946 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002947 } else {
2948 __ leal(out.AsRegister<Register>(), Address(
2949 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2950 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002951 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002952 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2953 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2954 __ addl(out.AsRegister<Register>(), Immediate(value));
2955 } else {
2956 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2957 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002958 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002959 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002960 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002961 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002962 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002963 }
2964
2965 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002966 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002967 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2968 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002969 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002970 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2971 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002972 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002973 } else {
2974 DCHECK(second.IsConstant()) << second;
2975 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2976 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2977 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002978 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002979 break;
2980 }
2981
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002982 case Primitive::kPrimFloat: {
2983 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002984 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002985 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2986 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002987 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002988 __ addss(first.AsFpuRegister<XmmRegister>(),
2989 codegen_->LiteralFloatAddress(
2990 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2991 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2992 } else {
2993 DCHECK(second.IsStackSlot());
2994 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002995 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002996 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002997 }
2998
2999 case Primitive::kPrimDouble: {
3000 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003001 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003002 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3003 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003004 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003005 __ addsd(first.AsFpuRegister<XmmRegister>(),
3006 codegen_->LiteralDoubleAddress(
3007 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3008 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3009 } else {
3010 DCHECK(second.IsDoubleStackSlot());
3011 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003012 }
3013 break;
3014 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003015
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003016 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003017 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003018 }
3019}
3020
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003021void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003022 LocationSummary* locations =
3023 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003024 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003025 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003026 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003027 locations->SetInAt(0, Location::RequiresRegister());
3028 locations->SetInAt(1, Location::Any());
3029 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003030 break;
3031 }
Calin Juravle11351682014-10-23 15:38:15 +01003032 case Primitive::kPrimFloat:
3033 case Primitive::kPrimDouble: {
3034 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003035 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3036 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003037 } else if (sub->InputAt(1)->IsConstant()) {
3038 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003039 } else {
3040 locations->SetInAt(1, Location::Any());
3041 }
Calin Juravle11351682014-10-23 15:38:15 +01003042 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003043 break;
Calin Juravle11351682014-10-23 15:38:15 +01003044 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003045
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003046 default:
Calin Juravle11351682014-10-23 15:38:15 +01003047 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003048 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003049}
3050
3051void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3052 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003053 Location first = locations->InAt(0);
3054 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003055 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003056 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003057 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003058 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003059 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003060 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003061 __ subl(first.AsRegister<Register>(),
3062 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003063 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003064 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003065 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003066 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003067 }
3068
3069 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003070 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003071 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3072 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003073 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003074 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003075 __ sbbl(first.AsRegisterPairHigh<Register>(),
3076 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003077 } else {
3078 DCHECK(second.IsConstant()) << second;
3079 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3080 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3081 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003082 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003083 break;
3084 }
3085
Calin Juravle11351682014-10-23 15:38:15 +01003086 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003087 if (second.IsFpuRegister()) {
3088 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3089 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3090 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003091 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003092 __ subss(first.AsFpuRegister<XmmRegister>(),
3093 codegen_->LiteralFloatAddress(
3094 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3095 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3096 } else {
3097 DCHECK(second.IsStackSlot());
3098 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3099 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003100 break;
Calin Juravle11351682014-10-23 15:38:15 +01003101 }
3102
3103 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003104 if (second.IsFpuRegister()) {
3105 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3106 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3107 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003108 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003109 __ subsd(first.AsFpuRegister<XmmRegister>(),
3110 codegen_->LiteralDoubleAddress(
3111 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3112 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3113 } else {
3114 DCHECK(second.IsDoubleStackSlot());
3115 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3116 }
Calin Juravle11351682014-10-23 15:38:15 +01003117 break;
3118 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003119
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003120 default:
Calin Juravle11351682014-10-23 15:38:15 +01003121 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003122 }
3123}
3124
Calin Juravle34bacdf2014-10-07 20:23:36 +01003125void LocationsBuilderX86::VisitMul(HMul* mul) {
3126 LocationSummary* locations =
3127 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3128 switch (mul->GetResultType()) {
3129 case Primitive::kPrimInt:
3130 locations->SetInAt(0, Location::RequiresRegister());
3131 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003132 if (mul->InputAt(1)->IsIntConstant()) {
3133 // Can use 3 operand multiply.
3134 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3135 } else {
3136 locations->SetOut(Location::SameAsFirstInput());
3137 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003138 break;
3139 case Primitive::kPrimLong: {
3140 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003141 locations->SetInAt(1, Location::Any());
3142 locations->SetOut(Location::SameAsFirstInput());
3143 // Needed for imul on 32bits with 64bits output.
3144 locations->AddTemp(Location::RegisterLocation(EAX));
3145 locations->AddTemp(Location::RegisterLocation(EDX));
3146 break;
3147 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003148 case Primitive::kPrimFloat:
3149 case Primitive::kPrimDouble: {
3150 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003151 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3152 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003153 } else if (mul->InputAt(1)->IsConstant()) {
3154 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003155 } else {
3156 locations->SetInAt(1, Location::Any());
3157 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003158 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003159 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003160 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003161
3162 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003163 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003164 }
3165}
3166
3167void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3168 LocationSummary* locations = mul->GetLocations();
3169 Location first = locations->InAt(0);
3170 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003171 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003172
3173 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003174 case Primitive::kPrimInt:
3175 // The constant may have ended up in a register, so test explicitly to avoid
3176 // problems where the output may not be the same as the first operand.
3177 if (mul->InputAt(1)->IsIntConstant()) {
3178 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3179 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3180 } else if (second.IsRegister()) {
3181 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003182 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003183 } else {
3184 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003185 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003186 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003187 }
3188 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003189
3190 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003191 Register in1_hi = first.AsRegisterPairHigh<Register>();
3192 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003193 Register eax = locations->GetTemp(0).AsRegister<Register>();
3194 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003195
3196 DCHECK_EQ(EAX, eax);
3197 DCHECK_EQ(EDX, edx);
3198
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003199 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003200 // output: in1
3201 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3202 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3203 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003204 if (second.IsConstant()) {
3205 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003206
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003207 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3208 int32_t low_value = Low32Bits(value);
3209 int32_t high_value = High32Bits(value);
3210 Immediate low(low_value);
3211 Immediate high(high_value);
3212
3213 __ movl(eax, high);
3214 // eax <- in1.lo * in2.hi
3215 __ imull(eax, in1_lo);
3216 // in1.hi <- in1.hi * in2.lo
3217 __ imull(in1_hi, low);
3218 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3219 __ addl(in1_hi, eax);
3220 // move in2_lo to eax to prepare for double precision
3221 __ movl(eax, low);
3222 // edx:eax <- in1.lo * in2.lo
3223 __ mull(in1_lo);
3224 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3225 __ addl(in1_hi, edx);
3226 // in1.lo <- (in1.lo * in2.lo)[31:0];
3227 __ movl(in1_lo, eax);
3228 } else if (second.IsRegisterPair()) {
3229 Register in2_hi = second.AsRegisterPairHigh<Register>();
3230 Register in2_lo = second.AsRegisterPairLow<Register>();
3231
3232 __ movl(eax, in2_hi);
3233 // eax <- in1.lo * in2.hi
3234 __ imull(eax, in1_lo);
3235 // in1.hi <- in1.hi * in2.lo
3236 __ imull(in1_hi, in2_lo);
3237 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3238 __ addl(in1_hi, eax);
3239 // move in1_lo to eax to prepare for double precision
3240 __ movl(eax, in1_lo);
3241 // edx:eax <- in1.lo * in2.lo
3242 __ mull(in2_lo);
3243 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3244 __ addl(in1_hi, edx);
3245 // in1.lo <- (in1.lo * in2.lo)[31:0];
3246 __ movl(in1_lo, eax);
3247 } else {
3248 DCHECK(second.IsDoubleStackSlot()) << second;
3249 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3250 Address in2_lo(ESP, second.GetStackIndex());
3251
3252 __ movl(eax, in2_hi);
3253 // eax <- in1.lo * in2.hi
3254 __ imull(eax, in1_lo);
3255 // in1.hi <- in1.hi * in2.lo
3256 __ imull(in1_hi, in2_lo);
3257 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3258 __ addl(in1_hi, eax);
3259 // move in1_lo to eax to prepare for double precision
3260 __ movl(eax, in1_lo);
3261 // edx:eax <- in1.lo * in2.lo
3262 __ mull(in2_lo);
3263 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3264 __ addl(in1_hi, edx);
3265 // in1.lo <- (in1.lo * in2.lo)[31:0];
3266 __ movl(in1_lo, eax);
3267 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003268
3269 break;
3270 }
3271
Calin Juravleb5bfa962014-10-21 18:02:24 +01003272 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003273 DCHECK(first.Equals(locations->Out()));
3274 if (second.IsFpuRegister()) {
3275 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3276 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3277 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003278 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003279 __ mulss(first.AsFpuRegister<XmmRegister>(),
3280 codegen_->LiteralFloatAddress(
3281 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3282 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3283 } else {
3284 DCHECK(second.IsStackSlot());
3285 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3286 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003287 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003288 }
3289
3290 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003291 DCHECK(first.Equals(locations->Out()));
3292 if (second.IsFpuRegister()) {
3293 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3294 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3295 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003296 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003297 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3298 codegen_->LiteralDoubleAddress(
3299 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3300 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3301 } else {
3302 DCHECK(second.IsDoubleStackSlot());
3303 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3304 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003305 break;
3306 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003307
3308 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003309 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003310 }
3311}
3312
Roland Levillain232ade02015-04-20 15:14:36 +01003313void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3314 uint32_t temp_offset,
3315 uint32_t stack_adjustment,
3316 bool is_fp,
3317 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003318 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003319 DCHECK(!is_wide);
3320 if (is_fp) {
3321 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3322 } else {
3323 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3324 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003325 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003326 DCHECK(is_wide);
3327 if (is_fp) {
3328 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3329 } else {
3330 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3331 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003332 } else {
3333 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003334 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003335 Location stack_temp = Location::StackSlot(temp_offset);
3336 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003337 if (is_fp) {
3338 __ flds(Address(ESP, temp_offset));
3339 } else {
3340 __ filds(Address(ESP, temp_offset));
3341 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003342 } else {
3343 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3344 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003345 if (is_fp) {
3346 __ fldl(Address(ESP, temp_offset));
3347 } else {
3348 __ fildl(Address(ESP, temp_offset));
3349 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003350 }
3351 }
3352}
3353
3354void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3355 Primitive::Type type = rem->GetResultType();
3356 bool is_float = type == Primitive::kPrimFloat;
3357 size_t elem_size = Primitive::ComponentSize(type);
3358 LocationSummary* locations = rem->GetLocations();
3359 Location first = locations->InAt(0);
3360 Location second = locations->InAt(1);
3361 Location out = locations->Out();
3362
3363 // Create stack space for 2 elements.
3364 // TODO: enhance register allocator to ask for stack temporaries.
3365 __ subl(ESP, Immediate(2 * elem_size));
3366
3367 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003368 const bool is_wide = !is_float;
3369 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3370 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003371
3372 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003373 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003374 __ Bind(&retry);
3375 __ fprem();
3376
3377 // Move FP status to AX.
3378 __ fstsw();
3379
3380 // And see if the argument reduction is complete. This is signaled by the
3381 // C2 FPU flag bit set to 0.
3382 __ andl(EAX, Immediate(kC2ConditionMask));
3383 __ j(kNotEqual, &retry);
3384
3385 // We have settled on the final value. Retrieve it into an XMM register.
3386 // Store FP top of stack to real stack.
3387 if (is_float) {
3388 __ fsts(Address(ESP, 0));
3389 } else {
3390 __ fstl(Address(ESP, 0));
3391 }
3392
3393 // Pop the 2 items from the FP stack.
3394 __ fucompp();
3395
3396 // Load the value from the stack into an XMM register.
3397 DCHECK(out.IsFpuRegister()) << out;
3398 if (is_float) {
3399 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3400 } else {
3401 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3402 }
3403
3404 // And remove the temporary stack space we allocated.
3405 __ addl(ESP, Immediate(2 * elem_size));
3406}
3407
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003408
3409void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3410 DCHECK(instruction->IsDiv() || instruction->IsRem());
3411
3412 LocationSummary* locations = instruction->GetLocations();
3413 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003414 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003415
3416 Register out_register = locations->Out().AsRegister<Register>();
3417 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003418 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003419
3420 DCHECK(imm == 1 || imm == -1);
3421
3422 if (instruction->IsRem()) {
3423 __ xorl(out_register, out_register);
3424 } else {
3425 __ movl(out_register, input_register);
3426 if (imm == -1) {
3427 __ negl(out_register);
3428 }
3429 }
3430}
3431
3432
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003433void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003434 LocationSummary* locations = instruction->GetLocations();
3435
3436 Register out_register = locations->Out().AsRegister<Register>();
3437 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003438 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003439 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3440 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003441
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003442 Register num = locations->GetTemp(0).AsRegister<Register>();
3443
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003444 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003445 __ testl(input_register, input_register);
3446 __ cmovl(kGreaterEqual, num, input_register);
3447 int shift = CTZ(imm);
3448 __ sarl(num, Immediate(shift));
3449
3450 if (imm < 0) {
3451 __ negl(num);
3452 }
3453
3454 __ movl(out_register, num);
3455}
3456
3457void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3458 DCHECK(instruction->IsDiv() || instruction->IsRem());
3459
3460 LocationSummary* locations = instruction->GetLocations();
3461 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3462
3463 Register eax = locations->InAt(0).AsRegister<Register>();
3464 Register out = locations->Out().AsRegister<Register>();
3465 Register num;
3466 Register edx;
3467
3468 if (instruction->IsDiv()) {
3469 edx = locations->GetTemp(0).AsRegister<Register>();
3470 num = locations->GetTemp(1).AsRegister<Register>();
3471 } else {
3472 edx = locations->Out().AsRegister<Register>();
3473 num = locations->GetTemp(0).AsRegister<Register>();
3474 }
3475
3476 DCHECK_EQ(EAX, eax);
3477 DCHECK_EQ(EDX, edx);
3478 if (instruction->IsDiv()) {
3479 DCHECK_EQ(EAX, out);
3480 } else {
3481 DCHECK_EQ(EDX, out);
3482 }
3483
3484 int64_t magic;
3485 int shift;
3486 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3487
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003488 // Save the numerator.
3489 __ movl(num, eax);
3490
3491 // EAX = magic
3492 __ movl(eax, Immediate(magic));
3493
3494 // EDX:EAX = magic * numerator
3495 __ imull(num);
3496
3497 if (imm > 0 && magic < 0) {
3498 // EDX += num
3499 __ addl(edx, num);
3500 } else if (imm < 0 && magic > 0) {
3501 __ subl(edx, num);
3502 }
3503
3504 // Shift if needed.
3505 if (shift != 0) {
3506 __ sarl(edx, Immediate(shift));
3507 }
3508
3509 // EDX += 1 if EDX < 0
3510 __ movl(eax, edx);
3511 __ shrl(edx, Immediate(31));
3512 __ addl(edx, eax);
3513
3514 if (instruction->IsRem()) {
3515 __ movl(eax, num);
3516 __ imull(edx, Immediate(imm));
3517 __ subl(eax, edx);
3518 __ movl(edx, eax);
3519 } else {
3520 __ movl(eax, edx);
3521 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003522}
3523
Calin Juravlebacfec32014-11-14 15:54:36 +00003524void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3525 DCHECK(instruction->IsDiv() || instruction->IsRem());
3526
3527 LocationSummary* locations = instruction->GetLocations();
3528 Location out = locations->Out();
3529 Location first = locations->InAt(0);
3530 Location second = locations->InAt(1);
3531 bool is_div = instruction->IsDiv();
3532
3533 switch (instruction->GetResultType()) {
3534 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003535 DCHECK_EQ(EAX, first.AsRegister<Register>());
3536 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003537
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003538 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003539 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003540
3541 if (imm == 0) {
3542 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3543 } else if (imm == 1 || imm == -1) {
3544 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003545 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003546 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003547 } else {
3548 DCHECK(imm <= -2 || imm >= 2);
3549 GenerateDivRemWithAnyConstant(instruction);
3550 }
3551 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003552 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3553 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003554 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003555
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556 Register second_reg = second.AsRegister<Register>();
3557 // 0x80000000/-1 triggers an arithmetic exception!
3558 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3559 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003560
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003561 __ cmpl(second_reg, Immediate(-1));
3562 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003563
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003564 // edx:eax <- sign-extended of eax
3565 __ cdq();
3566 // eax = quotient, edx = remainder
3567 __ idivl(second_reg);
3568 __ Bind(slow_path->GetExitLabel());
3569 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003570 break;
3571 }
3572
3573 case Primitive::kPrimLong: {
3574 InvokeRuntimeCallingConvention calling_convention;
3575 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3576 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3577 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3578 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3579 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3580 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3581
3582 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003583 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003584 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003585 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003586 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003587 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003588 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003589 break;
3590 }
3591
3592 default:
3593 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3594 }
3595}
3596
Calin Juravle7c4954d2014-10-28 16:57:40 +00003597void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003598 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003599 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003600 : LocationSummary::kNoCall;
3601 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3602
Calin Juravle7c4954d2014-10-28 16:57:40 +00003603 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003604 case Primitive::kPrimInt: {
3605 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003606 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003607 locations->SetOut(Location::SameAsFirstInput());
3608 // Intel uses edx:eax as the dividend.
3609 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003610 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3611 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3612 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003613 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003614 locations->AddTemp(Location::RequiresRegister());
3615 }
Calin Juravled0d48522014-11-04 16:40:20 +00003616 break;
3617 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003618 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003619 InvokeRuntimeCallingConvention calling_convention;
3620 locations->SetInAt(0, Location::RegisterPairLocation(
3621 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3622 locations->SetInAt(1, Location::RegisterPairLocation(
3623 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3624 // Runtime helper puts the result in EAX, EDX.
3625 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003626 break;
3627 }
3628 case Primitive::kPrimFloat:
3629 case Primitive::kPrimDouble: {
3630 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003631 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3632 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003633 } else if (div->InputAt(1)->IsConstant()) {
3634 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003635 } else {
3636 locations->SetInAt(1, Location::Any());
3637 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003638 locations->SetOut(Location::SameAsFirstInput());
3639 break;
3640 }
3641
3642 default:
3643 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3644 }
3645}
3646
3647void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3648 LocationSummary* locations = div->GetLocations();
3649 Location first = locations->InAt(0);
3650 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003651
3652 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003653 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003654 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003655 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003656 break;
3657 }
3658
3659 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003660 if (second.IsFpuRegister()) {
3661 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3662 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3663 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003664 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003665 __ divss(first.AsFpuRegister<XmmRegister>(),
3666 codegen_->LiteralFloatAddress(
3667 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3668 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3669 } else {
3670 DCHECK(second.IsStackSlot());
3671 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3672 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003673 break;
3674 }
3675
3676 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003677 if (second.IsFpuRegister()) {
3678 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3679 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3680 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003681 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003682 __ divsd(first.AsFpuRegister<XmmRegister>(),
3683 codegen_->LiteralDoubleAddress(
3684 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3685 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3686 } else {
3687 DCHECK(second.IsDoubleStackSlot());
3688 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3689 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003690 break;
3691 }
3692
3693 default:
3694 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3695 }
3696}
3697
Calin Juravlebacfec32014-11-14 15:54:36 +00003698void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003699 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003700
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003701 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003702 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003703 : LocationSummary::kNoCall;
3704 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003705
Calin Juravled2ec87d2014-12-08 14:24:46 +00003706 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003707 case Primitive::kPrimInt: {
3708 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003709 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003710 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003711 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3712 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3713 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003714 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003715 locations->AddTemp(Location::RequiresRegister());
3716 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003717 break;
3718 }
3719 case Primitive::kPrimLong: {
3720 InvokeRuntimeCallingConvention calling_convention;
3721 locations->SetInAt(0, Location::RegisterPairLocation(
3722 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3723 locations->SetInAt(1, Location::RegisterPairLocation(
3724 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3725 // Runtime helper puts the result in EAX, EDX.
3726 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3727 break;
3728 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003729 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003730 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003731 locations->SetInAt(0, Location::Any());
3732 locations->SetInAt(1, Location::Any());
3733 locations->SetOut(Location::RequiresFpuRegister());
3734 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003735 break;
3736 }
3737
3738 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003739 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003740 }
3741}
3742
3743void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3744 Primitive::Type type = rem->GetResultType();
3745 switch (type) {
3746 case Primitive::kPrimInt:
3747 case Primitive::kPrimLong: {
3748 GenerateDivRemIntegral(rem);
3749 break;
3750 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003751 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003752 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003753 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003754 break;
3755 }
3756 default:
3757 LOG(FATAL) << "Unexpected rem type " << type;
3758 }
3759}
3760
Calin Juravled0d48522014-11-04 16:40:20 +00003761void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003762 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003763 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003764 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003765 case Primitive::kPrimByte:
3766 case Primitive::kPrimChar:
3767 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003768 case Primitive::kPrimInt: {
3769 locations->SetInAt(0, Location::Any());
3770 break;
3771 }
3772 case Primitive::kPrimLong: {
3773 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3774 if (!instruction->IsConstant()) {
3775 locations->AddTemp(Location::RequiresRegister());
3776 }
3777 break;
3778 }
3779 default:
3780 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3781 }
Calin Juravled0d48522014-11-04 16:40:20 +00003782}
3783
3784void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003785 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003786 codegen_->AddSlowPath(slow_path);
3787
3788 LocationSummary* locations = instruction->GetLocations();
3789 Location value = locations->InAt(0);
3790
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003791 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003792 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003793 case Primitive::kPrimByte:
3794 case Primitive::kPrimChar:
3795 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003796 case Primitive::kPrimInt: {
3797 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003798 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003799 __ j(kEqual, slow_path->GetEntryLabel());
3800 } else if (value.IsStackSlot()) {
3801 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3802 __ j(kEqual, slow_path->GetEntryLabel());
3803 } else {
3804 DCHECK(value.IsConstant()) << value;
3805 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003806 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003807 }
3808 }
3809 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003810 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003811 case Primitive::kPrimLong: {
3812 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003813 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003814 __ movl(temp, value.AsRegisterPairLow<Register>());
3815 __ orl(temp, value.AsRegisterPairHigh<Register>());
3816 __ j(kEqual, slow_path->GetEntryLabel());
3817 } else {
3818 DCHECK(value.IsConstant()) << value;
3819 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3820 __ jmp(slow_path->GetEntryLabel());
3821 }
3822 }
3823 break;
3824 }
3825 default:
3826 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003827 }
Calin Juravled0d48522014-11-04 16:40:20 +00003828}
3829
Calin Juravle9aec02f2014-11-18 23:06:35 +00003830void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3831 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3832
3833 LocationSummary* locations =
3834 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3835
3836 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003837 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003838 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003839 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003840 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003841 // The shift count needs to be in CL or a constant.
3842 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003843 locations->SetOut(Location::SameAsFirstInput());
3844 break;
3845 }
3846 default:
3847 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3848 }
3849}
3850
3851void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3852 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3853
3854 LocationSummary* locations = op->GetLocations();
3855 Location first = locations->InAt(0);
3856 Location second = locations->InAt(1);
3857 DCHECK(first.Equals(locations->Out()));
3858
3859 switch (op->GetResultType()) {
3860 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003861 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003862 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003863 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003864 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003865 DCHECK_EQ(ECX, second_reg);
3866 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003867 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003868 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003869 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003870 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003871 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003872 }
3873 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003874 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003875 if (shift == 0) {
3876 return;
3877 }
3878 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003879 if (op->IsShl()) {
3880 __ shll(first_reg, imm);
3881 } else if (op->IsShr()) {
3882 __ sarl(first_reg, imm);
3883 } else {
3884 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003885 }
3886 }
3887 break;
3888 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003889 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003890 if (second.IsRegister()) {
3891 Register second_reg = second.AsRegister<Register>();
3892 DCHECK_EQ(ECX, second_reg);
3893 if (op->IsShl()) {
3894 GenerateShlLong(first, second_reg);
3895 } else if (op->IsShr()) {
3896 GenerateShrLong(first, second_reg);
3897 } else {
3898 GenerateUShrLong(first, second_reg);
3899 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003900 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003901 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003902 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003903 // Nothing to do if the shift is 0, as the input is already the output.
3904 if (shift != 0) {
3905 if (op->IsShl()) {
3906 GenerateShlLong(first, shift);
3907 } else if (op->IsShr()) {
3908 GenerateShrLong(first, shift);
3909 } else {
3910 GenerateUShrLong(first, shift);
3911 }
3912 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003913 }
3914 break;
3915 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003916 default:
3917 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3918 }
3919}
3920
Mark P Mendell73945692015-04-29 14:56:17 +00003921void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3922 Register low = loc.AsRegisterPairLow<Register>();
3923 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003924 if (shift == 1) {
3925 // This is just an addition.
3926 __ addl(low, low);
3927 __ adcl(high, high);
3928 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003929 // Shift by 32 is easy. High gets low, and low gets 0.
3930 codegen_->EmitParallelMoves(
3931 loc.ToLow(),
3932 loc.ToHigh(),
3933 Primitive::kPrimInt,
3934 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3935 loc.ToLow(),
3936 Primitive::kPrimInt);
3937 } else if (shift > 32) {
3938 // Low part becomes 0. High part is low part << (shift-32).
3939 __ movl(high, low);
3940 __ shll(high, Immediate(shift - 32));
3941 __ xorl(low, low);
3942 } else {
3943 // Between 1 and 31.
3944 __ shld(high, low, Immediate(shift));
3945 __ shll(low, Immediate(shift));
3946 }
3947}
3948
Calin Juravle9aec02f2014-11-18 23:06:35 +00003949void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003950 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003951 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3952 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3953 __ testl(shifter, Immediate(32));
3954 __ j(kEqual, &done);
3955 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3956 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3957 __ Bind(&done);
3958}
3959
Mark P Mendell73945692015-04-29 14:56:17 +00003960void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3961 Register low = loc.AsRegisterPairLow<Register>();
3962 Register high = loc.AsRegisterPairHigh<Register>();
3963 if (shift == 32) {
3964 // Need to copy the sign.
3965 DCHECK_NE(low, high);
3966 __ movl(low, high);
3967 __ sarl(high, Immediate(31));
3968 } else if (shift > 32) {
3969 DCHECK_NE(low, high);
3970 // High part becomes sign. Low part is shifted by shift - 32.
3971 __ movl(low, high);
3972 __ sarl(high, Immediate(31));
3973 __ sarl(low, Immediate(shift - 32));
3974 } else {
3975 // Between 1 and 31.
3976 __ shrd(low, high, Immediate(shift));
3977 __ sarl(high, Immediate(shift));
3978 }
3979}
3980
Calin Juravle9aec02f2014-11-18 23:06:35 +00003981void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003982 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003983 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3984 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3985 __ testl(shifter, Immediate(32));
3986 __ j(kEqual, &done);
3987 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3988 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3989 __ Bind(&done);
3990}
3991
Mark P Mendell73945692015-04-29 14:56:17 +00003992void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3993 Register low = loc.AsRegisterPairLow<Register>();
3994 Register high = loc.AsRegisterPairHigh<Register>();
3995 if (shift == 32) {
3996 // Shift by 32 is easy. Low gets high, and high gets 0.
3997 codegen_->EmitParallelMoves(
3998 loc.ToHigh(),
3999 loc.ToLow(),
4000 Primitive::kPrimInt,
4001 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4002 loc.ToHigh(),
4003 Primitive::kPrimInt);
4004 } else if (shift > 32) {
4005 // Low part is high >> (shift - 32). High part becomes 0.
4006 __ movl(low, high);
4007 __ shrl(low, Immediate(shift - 32));
4008 __ xorl(high, high);
4009 } else {
4010 // Between 1 and 31.
4011 __ shrd(low, high, Immediate(shift));
4012 __ shrl(high, Immediate(shift));
4013 }
4014}
4015
Calin Juravle9aec02f2014-11-18 23:06:35 +00004016void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004017 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004018 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4019 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4020 __ testl(shifter, Immediate(32));
4021 __ j(kEqual, &done);
4022 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4023 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4024 __ Bind(&done);
4025}
4026
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004027void LocationsBuilderX86::VisitRor(HRor* ror) {
4028 LocationSummary* locations =
4029 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4030
4031 switch (ror->GetResultType()) {
4032 case Primitive::kPrimLong:
4033 // Add the temporary needed.
4034 locations->AddTemp(Location::RequiresRegister());
4035 FALLTHROUGH_INTENDED;
4036 case Primitive::kPrimInt:
4037 locations->SetInAt(0, Location::RequiresRegister());
4038 // The shift count needs to be in CL (unless it is a constant).
4039 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4040 locations->SetOut(Location::SameAsFirstInput());
4041 break;
4042 default:
4043 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4044 UNREACHABLE();
4045 }
4046}
4047
4048void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4049 LocationSummary* locations = ror->GetLocations();
4050 Location first = locations->InAt(0);
4051 Location second = locations->InAt(1);
4052
4053 if (ror->GetResultType() == Primitive::kPrimInt) {
4054 Register first_reg = first.AsRegister<Register>();
4055 if (second.IsRegister()) {
4056 Register second_reg = second.AsRegister<Register>();
4057 __ rorl(first_reg, second_reg);
4058 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004059 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004060 __ rorl(first_reg, imm);
4061 }
4062 return;
4063 }
4064
4065 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4066 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4067 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4068 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4069 if (second.IsRegister()) {
4070 Register second_reg = second.AsRegister<Register>();
4071 DCHECK_EQ(second_reg, ECX);
4072 __ movl(temp_reg, first_reg_hi);
4073 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4074 __ shrd(first_reg_lo, temp_reg, second_reg);
4075 __ movl(temp_reg, first_reg_hi);
4076 __ testl(second_reg, Immediate(32));
4077 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4078 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4079 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004080 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004081 if (shift_amt == 0) {
4082 // Already fine.
4083 return;
4084 }
4085 if (shift_amt == 32) {
4086 // Just swap.
4087 __ movl(temp_reg, first_reg_lo);
4088 __ movl(first_reg_lo, first_reg_hi);
4089 __ movl(first_reg_hi, temp_reg);
4090 return;
4091 }
4092
4093 Immediate imm(shift_amt);
4094 // Save the constents of the low value.
4095 __ movl(temp_reg, first_reg_lo);
4096
4097 // Shift right into low, feeding bits from high.
4098 __ shrd(first_reg_lo, first_reg_hi, imm);
4099
4100 // Shift right into high, feeding bits from the original low.
4101 __ shrd(first_reg_hi, temp_reg, imm);
4102
4103 // Swap if needed.
4104 if (shift_amt > 32) {
4105 __ movl(temp_reg, first_reg_lo);
4106 __ movl(first_reg_lo, first_reg_hi);
4107 __ movl(first_reg_hi, temp_reg);
4108 }
4109 }
4110}
4111
Calin Juravle9aec02f2014-11-18 23:06:35 +00004112void LocationsBuilderX86::VisitShl(HShl* shl) {
4113 HandleShift(shl);
4114}
4115
4116void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4117 HandleShift(shl);
4118}
4119
4120void LocationsBuilderX86::VisitShr(HShr* shr) {
4121 HandleShift(shr);
4122}
4123
4124void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4125 HandleShift(shr);
4126}
4127
4128void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4129 HandleShift(ushr);
4130}
4131
4132void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4133 HandleShift(ushr);
4134}
4135
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004136void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004137 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004138 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004139 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004140 if (instruction->IsStringAlloc()) {
4141 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4142 } else {
4143 InvokeRuntimeCallingConvention calling_convention;
4144 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4145 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4146 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004147}
4148
4149void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004150 // Note: if heap poisoning is enabled, the entry point takes cares
4151 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004152 if (instruction->IsStringAlloc()) {
4153 // String is allocated through StringFactory. Call NewEmptyString entry point.
4154 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004155 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004156 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4157 __ call(Address(temp, code_offset.Int32Value()));
4158 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4159 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004160 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004161 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4162 DCHECK(!codegen_->IsLeafMethod());
4163 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004164}
4165
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004166void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4167 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004168 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004169 locations->SetOut(Location::RegisterLocation(EAX));
4170 InvokeRuntimeCallingConvention calling_convention;
4171 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004172 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004173 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004174}
4175
4176void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4177 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004178 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004179 // Note: if heap poisoning is enabled, the entry point takes cares
4180 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004181 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004182 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004183 DCHECK(!codegen_->IsLeafMethod());
4184}
4185
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004186void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004187 LocationSummary* locations =
4188 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004189 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4190 if (location.IsStackSlot()) {
4191 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4192 } else if (location.IsDoubleStackSlot()) {
4193 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004194 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004195 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004196}
4197
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004198void InstructionCodeGeneratorX86::VisitParameterValue(
4199 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4200}
4201
4202void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4203 LocationSummary* locations =
4204 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4205 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4206}
4207
4208void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004209}
4210
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004211void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4212 LocationSummary* locations =
4213 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4214 locations->SetInAt(0, Location::RequiresRegister());
4215 locations->SetOut(Location::RequiresRegister());
4216}
4217
4218void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4219 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004220 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004221 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004222 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004223 __ movl(locations->Out().AsRegister<Register>(),
4224 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004225 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004226 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004227 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004228 __ movl(locations->Out().AsRegister<Register>(),
4229 Address(locations->InAt(0).AsRegister<Register>(),
4230 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4231 // temp = temp->GetImtEntryAt(method_offset);
4232 __ movl(locations->Out().AsRegister<Register>(),
4233 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004234 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004235}
4236
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004237void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004238 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004239 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004240 locations->SetInAt(0, Location::RequiresRegister());
4241 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004242}
4243
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004244void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4245 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004246 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004247 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004248 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004249 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004250 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004251 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004252 break;
4253
4254 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004255 __ notl(out.AsRegisterPairLow<Register>());
4256 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004257 break;
4258
4259 default:
4260 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4261 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004262}
4263
David Brazdil66d126e2015-04-03 16:02:44 +01004264void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4265 LocationSummary* locations =
4266 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4267 locations->SetInAt(0, Location::RequiresRegister());
4268 locations->SetOut(Location::SameAsFirstInput());
4269}
4270
4271void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004272 LocationSummary* locations = bool_not->GetLocations();
4273 Location in = locations->InAt(0);
4274 Location out = locations->Out();
4275 DCHECK(in.Equals(out));
4276 __ xorl(out.AsRegister<Register>(), Immediate(1));
4277}
4278
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004279void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004280 LocationSummary* locations =
4281 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004282 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004283 case Primitive::kPrimBoolean:
4284 case Primitive::kPrimByte:
4285 case Primitive::kPrimShort:
4286 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004287 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004288 case Primitive::kPrimLong: {
4289 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004290 locations->SetInAt(1, Location::Any());
4291 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4292 break;
4293 }
4294 case Primitive::kPrimFloat:
4295 case Primitive::kPrimDouble: {
4296 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004297 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4298 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4299 } else if (compare->InputAt(1)->IsConstant()) {
4300 locations->SetInAt(1, Location::RequiresFpuRegister());
4301 } else {
4302 locations->SetInAt(1, Location::Any());
4303 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004304 locations->SetOut(Location::RequiresRegister());
4305 break;
4306 }
4307 default:
4308 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4309 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004310}
4311
4312void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004313 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004314 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004315 Location left = locations->InAt(0);
4316 Location right = locations->InAt(1);
4317
Mark Mendell0c9497d2015-08-21 09:30:05 -04004318 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004319 Condition less_cond = kLess;
4320
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004321 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004322 case Primitive::kPrimBoolean:
4323 case Primitive::kPrimByte:
4324 case Primitive::kPrimShort:
4325 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004326 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004327 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004328 break;
4329 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004330 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004331 Register left_low = left.AsRegisterPairLow<Register>();
4332 Register left_high = left.AsRegisterPairHigh<Register>();
4333 int32_t val_low = 0;
4334 int32_t val_high = 0;
4335 bool right_is_const = false;
4336
4337 if (right.IsConstant()) {
4338 DCHECK(right.GetConstant()->IsLongConstant());
4339 right_is_const = true;
4340 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4341 val_low = Low32Bits(val);
4342 val_high = High32Bits(val);
4343 }
4344
Calin Juravleddb7df22014-11-25 20:56:51 +00004345 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004346 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004347 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004348 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004349 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004350 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004351 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004352 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004353 __ j(kLess, &less); // Signed compare.
4354 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004355 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004356 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004357 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004358 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004359 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004360 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004361 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004362 }
Aart Bika19616e2016-02-01 18:57:58 -08004363 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004364 break;
4365 }
4366 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004367 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004368 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004369 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004370 break;
4371 }
4372 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004373 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004374 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004375 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004376 break;
4377 }
4378 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004379 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004380 }
Aart Bika19616e2016-02-01 18:57:58 -08004381
Calin Juravleddb7df22014-11-25 20:56:51 +00004382 __ movl(out, Immediate(0));
4383 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004384 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004385
4386 __ Bind(&greater);
4387 __ movl(out, Immediate(1));
4388 __ jmp(&done);
4389
4390 __ Bind(&less);
4391 __ movl(out, Immediate(-1));
4392
4393 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004394}
4395
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004396void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004397 LocationSummary* locations =
4398 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004399 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004400 locations->SetInAt(i, Location::Any());
4401 }
4402 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004403}
4404
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004405void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004406 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004407}
4408
Roland Levillain7c1559a2015-12-15 10:55:36 +00004409void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004410 /*
4411 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4412 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4413 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4414 */
4415 switch (kind) {
4416 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004417 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004418 break;
4419 }
4420 case MemBarrierKind::kAnyStore:
4421 case MemBarrierKind::kLoadAny:
4422 case MemBarrierKind::kStoreStore: {
4423 // nop
4424 break;
4425 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004426 case MemBarrierKind::kNTStoreStore:
4427 // Non-Temporal Store/Store needs an explicit fence.
4428 MemoryFence(/* non-temporal */ true);
4429 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004430 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004431}
4432
Vladimir Markodc151b22015-10-15 18:02:30 +01004433HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4434 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004435 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004436 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4437
4438 // We disable pc-relative load when there is an irreducible loop, as the optimization
4439 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004440 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4441 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004442 if (GetGraph()->HasIrreducibleLoops() &&
4443 (dispatch_info.method_load_kind ==
4444 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4445 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4446 }
4447 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004448 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4449 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4450 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4451 // (Though the direct CALL ptr16:32 is available for consideration).
4452 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004453 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004454 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004455 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004456 0u
4457 };
4458 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004459 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004460 }
4461}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004462
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004463Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4464 Register temp) {
4465 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004466 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004467 if (!invoke->GetLocations()->Intrinsified()) {
4468 return location.AsRegister<Register>();
4469 }
4470 // For intrinsics we allow any location, so it may be on the stack.
4471 if (!location.IsRegister()) {
4472 __ movl(temp, Address(ESP, location.GetStackIndex()));
4473 return temp;
4474 }
4475 // For register locations, check if the register was saved. If so, get it from the stack.
4476 // Note: There is a chance that the register was saved but not overwritten, so we could
4477 // save one load. However, since this is just an intrinsic slow path we prefer this
4478 // simple and more robust approach rather that trying to determine if that's the case.
4479 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004480 if (slow_path != nullptr) {
4481 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4482 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4483 __ movl(temp, Address(ESP, stack_offset));
4484 return temp;
4485 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004486 }
4487 return location.AsRegister<Register>();
4488}
4489
Serguei Katkov288c7a82016-05-16 11:53:15 +06004490Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4491 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004492 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4493 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004494 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004495 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004496 uint32_t offset =
4497 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4498 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004499 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004500 }
Vladimir Marko58155012015-08-19 12:49:41 +00004501 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004502 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004503 break;
4504 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4505 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4506 break;
4507 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004508 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004509 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4510 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004511 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4512 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004513 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4514 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4515 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004516 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004517 // Bind a new fixup label at the end of the "movl" insn.
4518 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004519 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004520 break;
4521 }
Vladimir Marko58155012015-08-19 12:49:41 +00004522 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004523 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004524 Register method_reg;
4525 Register reg = temp.AsRegister<Register>();
4526 if (current_method.IsRegister()) {
4527 method_reg = current_method.AsRegister<Register>();
4528 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004529 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004530 DCHECK(!current_method.IsValid());
4531 method_reg = reg;
4532 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4533 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004534 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004535 __ movl(reg, Address(method_reg,
4536 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004537 // temp = temp[index_in_cache];
4538 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4539 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004540 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4541 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004542 }
Vladimir Marko58155012015-08-19 12:49:41 +00004543 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004544 return callee_method;
4545}
4546
4547void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4548 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004549
4550 switch (invoke->GetCodePtrLocation()) {
4551 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4552 __ call(GetFrameEntryLabel());
4553 break;
4554 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004555 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4556 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004557 Label* label = &relative_call_patches_.back().label;
4558 __ call(label); // Bind to the patch label, override at link time.
4559 __ Bind(label); // Bind the label at the end of the "call" insn.
4560 break;
4561 }
4562 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4563 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004564 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4565 LOG(FATAL) << "Unsupported";
4566 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004567 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4568 // (callee_method + offset_of_quick_compiled_code)()
4569 __ call(Address(callee_method.AsRegister<Register>(),
4570 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004571 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004572 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004573 }
4574
4575 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004576}
4577
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004578void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4579 Register temp = temp_in.AsRegister<Register>();
4580 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4581 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004582
4583 // Use the calling convention instead of the location of the receiver, as
4584 // intrinsics may have put the receiver in a different register. In the intrinsics
4585 // slow path, the arguments have been moved to the right place, so here we are
4586 // guaranteed that the receiver is the first register of the calling convention.
4587 InvokeDexCallingConvention calling_convention;
4588 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004589 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004590 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004591 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004592 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004593 // Instead of simply (possibly) unpoisoning `temp` here, we should
4594 // emit a read barrier for the previous class reference load.
4595 // However this is not required in practice, as this is an
4596 // intermediate/temporary reference and because the current
4597 // concurrent copying collector keeps the from-space memory
4598 // intact/accessible until the end of the marking phase (the
4599 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004600 __ MaybeUnpoisonHeapReference(temp);
4601 // temp = temp->GetMethodAt(method_offset);
4602 __ movl(temp, Address(temp, method_offset));
4603 // call temp->GetEntryPoint();
4604 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004605 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004606}
4607
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004608void CodeGeneratorX86::RecordSimplePatch() {
4609 if (GetCompilerOptions().GetIncludePatchInformation()) {
4610 simple_patches_.emplace_back();
4611 __ Bind(&simple_patches_.back());
4612 }
4613}
4614
Vladimir Markoaad75c62016-10-03 08:46:48 +00004615void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4616 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004617 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4618 __ Bind(&string_patches_.back().label);
4619}
4620
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004621void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4622 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4623 __ Bind(&type_patches_.back().label);
4624}
4625
Vladimir Markoaad75c62016-10-03 08:46:48 +00004626Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4627 DCHECK(!GetCompilerOptions().IsBootImage());
4628 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4629 return &string_patches_.back().label;
4630}
4631
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004632Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4633 uint32_t element_offset) {
4634 // Add the patch entry and bind its label at the end of the instruction.
4635 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4636 return &pc_relative_dex_cache_patches_.back().label;
4637}
4638
Vladimir Markoaad75c62016-10-03 08:46:48 +00004639// The label points to the end of the "movl" or another instruction but the literal offset
4640// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4641constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4642
4643template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4644inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
4645 const ArenaDeque<PatchInfo<Label>>& infos,
4646 ArenaVector<LinkerPatch>* linker_patches) {
4647 for (const PatchInfo<Label>& info : infos) {
4648 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4649 linker_patches->push_back(
4650 Factory(literal_offset, &info.dex_file, GetMethodAddressOffset(), info.index));
4651 }
4652}
4653
Vladimir Marko58155012015-08-19 12:49:41 +00004654void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4655 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004656 size_t size =
4657 method_patches_.size() +
4658 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004659 pc_relative_dex_cache_patches_.size() +
4660 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004661 string_patches_.size() +
4662 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004663 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004664 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004665 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004666 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004667 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004668 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004669 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004670 linker_patches->push_back(
4671 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004672 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004673 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4674 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004675 for (const Label& label : simple_patches_) {
4676 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4677 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4678 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004679 if (!GetCompilerOptions().IsBootImage()) {
4680 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4681 } else if (GetCompilerOptions().GetCompilePic()) {
4682 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004683 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004684 for (const PatchInfo<Label>& info : string_patches_) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004685 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004686 linker_patches->push_back(
4687 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004688 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004689 }
4690 if (GetCompilerOptions().GetCompilePic()) {
4691 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
4692 } else {
4693 for (const PatchInfo<Label>& info : type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004694 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004695 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004696 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004697 }
Vladimir Marko58155012015-08-19 12:49:41 +00004698}
4699
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004700void CodeGeneratorX86::MarkGCCard(Register temp,
4701 Register card,
4702 Register object,
4703 Register value,
4704 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004705 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004706 if (value_can_be_null) {
4707 __ testl(value, value);
4708 __ j(kEqual, &is_null);
4709 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004710 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004711 __ movl(temp, object);
4712 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004713 __ movb(Address(temp, card, TIMES_1, 0),
4714 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004715 if (value_can_be_null) {
4716 __ Bind(&is_null);
4717 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004718}
4719
Calin Juravle52c48962014-12-16 17:02:57 +00004720void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4721 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004722
4723 bool object_field_get_with_read_barrier =
4724 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004725 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004726 new (GetGraph()->GetArena()) LocationSummary(instruction,
4727 kEmitCompilerReadBarrier ?
4728 LocationSummary::kCallOnSlowPath :
4729 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004730 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004731 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004732 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004733 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004734
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004735 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4736 locations->SetOut(Location::RequiresFpuRegister());
4737 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004738 // The output overlaps in case of long: we don't want the low move
4739 // to overwrite the object's location. Likewise, in the case of
4740 // an object field get with read barriers enabled, we do not want
4741 // the move to overwrite the object's location, as we need it to emit
4742 // the read barrier.
4743 locations->SetOut(
4744 Location::RequiresRegister(),
4745 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4746 Location::kOutputOverlap :
4747 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004748 }
Calin Juravle52c48962014-12-16 17:02:57 +00004749
4750 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4751 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004752 // So we use an XMM register as a temp to achieve atomicity (first
4753 // load the temp into the XMM and then copy the XMM into the
4754 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004755 locations->AddTemp(Location::RequiresFpuRegister());
4756 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004757}
4758
Calin Juravle52c48962014-12-16 17:02:57 +00004759void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4760 const FieldInfo& field_info) {
4761 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004762
Calin Juravle52c48962014-12-16 17:02:57 +00004763 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004764 Location base_loc = locations->InAt(0);
4765 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004766 Location out = locations->Out();
4767 bool is_volatile = field_info.IsVolatile();
4768 Primitive::Type field_type = field_info.GetFieldType();
4769 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4770
4771 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004772 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004773 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004774 break;
4775 }
4776
4777 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004778 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004779 break;
4780 }
4781
4782 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004783 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004784 break;
4785 }
4786
4787 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004788 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004789 break;
4790 }
4791
4792 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004793 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004794 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004795
4796 case Primitive::kPrimNot: {
4797 // /* HeapReference<Object> */ out = *(base + offset)
4798 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004799 // Note that a potential implicit null check is handled in this
4800 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4801 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004802 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004803 if (is_volatile) {
4804 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4805 }
4806 } else {
4807 __ movl(out.AsRegister<Register>(), Address(base, offset));
4808 codegen_->MaybeRecordImplicitNullCheck(instruction);
4809 if (is_volatile) {
4810 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4811 }
4812 // If read barriers are enabled, emit read barriers other than
4813 // Baker's using a slow path (and also unpoison the loaded
4814 // reference, if heap poisoning is enabled).
4815 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4816 }
4817 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004818 }
4819
4820 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004821 if (is_volatile) {
4822 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4823 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004824 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004825 __ movd(out.AsRegisterPairLow<Register>(), temp);
4826 __ psrlq(temp, Immediate(32));
4827 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4828 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004829 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004830 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004831 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004832 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4833 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004834 break;
4835 }
4836
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004837 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004838 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004839 break;
4840 }
4841
4842 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004843 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004844 break;
4845 }
4846
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004847 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004848 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004849 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004850 }
Calin Juravle52c48962014-12-16 17:02:57 +00004851
Roland Levillain7c1559a2015-12-15 10:55:36 +00004852 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4853 // Potential implicit null checks, in the case of reference or
4854 // long fields, are handled in the previous switch statement.
4855 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004856 codegen_->MaybeRecordImplicitNullCheck(instruction);
4857 }
4858
Calin Juravle52c48962014-12-16 17:02:57 +00004859 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004860 if (field_type == Primitive::kPrimNot) {
4861 // Memory barriers, in the case of references, are also handled
4862 // in the previous switch statement.
4863 } else {
4864 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4865 }
Roland Levillain4d027112015-07-01 15:41:14 +01004866 }
Calin Juravle52c48962014-12-16 17:02:57 +00004867}
4868
4869void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4870 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4871
4872 LocationSummary* locations =
4873 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4874 locations->SetInAt(0, Location::RequiresRegister());
4875 bool is_volatile = field_info.IsVolatile();
4876 Primitive::Type field_type = field_info.GetFieldType();
4877 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4878 || (field_type == Primitive::kPrimByte);
4879
4880 // The register allocator does not support multiple
4881 // inputs that die at entry with one in a specific register.
4882 if (is_byte_type) {
4883 // Ensure the value is in a byte register.
4884 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004885 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004886 if (is_volatile && field_type == Primitive::kPrimDouble) {
4887 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4888 locations->SetInAt(1, Location::RequiresFpuRegister());
4889 } else {
4890 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4891 }
4892 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4893 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004894 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004895
Calin Juravle52c48962014-12-16 17:02:57 +00004896 // 64bits value can be atomically written to an address with movsd and an XMM register.
4897 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4898 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4899 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4900 // isolated cases when we need this it isn't worth adding the extra complexity.
4901 locations->AddTemp(Location::RequiresFpuRegister());
4902 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004903 } else {
4904 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4905
4906 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4907 // Temporary registers for the write barrier.
4908 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4909 // Ensure the card is in a byte register.
4910 locations->AddTemp(Location::RegisterLocation(ECX));
4911 }
Calin Juravle52c48962014-12-16 17:02:57 +00004912 }
4913}
4914
4915void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004916 const FieldInfo& field_info,
4917 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004918 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4919
4920 LocationSummary* locations = instruction->GetLocations();
4921 Register base = locations->InAt(0).AsRegister<Register>();
4922 Location value = locations->InAt(1);
4923 bool is_volatile = field_info.IsVolatile();
4924 Primitive::Type field_type = field_info.GetFieldType();
4925 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004926 bool needs_write_barrier =
4927 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004928
4929 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004930 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004931 }
4932
Mark Mendell81489372015-11-04 11:30:41 -05004933 bool maybe_record_implicit_null_check_done = false;
4934
Calin Juravle52c48962014-12-16 17:02:57 +00004935 switch (field_type) {
4936 case Primitive::kPrimBoolean:
4937 case Primitive::kPrimByte: {
4938 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4939 break;
4940 }
4941
4942 case Primitive::kPrimShort:
4943 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004944 if (value.IsConstant()) {
4945 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4946 __ movw(Address(base, offset), Immediate(v));
4947 } else {
4948 __ movw(Address(base, offset), value.AsRegister<Register>());
4949 }
Calin Juravle52c48962014-12-16 17:02:57 +00004950 break;
4951 }
4952
4953 case Primitive::kPrimInt:
4954 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004955 if (kPoisonHeapReferences && needs_write_barrier) {
4956 // Note that in the case where `value` is a null reference,
4957 // we do not enter this block, as the reference does not
4958 // need poisoning.
4959 DCHECK_EQ(field_type, Primitive::kPrimNot);
4960 Register temp = locations->GetTemp(0).AsRegister<Register>();
4961 __ movl(temp, value.AsRegister<Register>());
4962 __ PoisonHeapReference(temp);
4963 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004964 } else if (value.IsConstant()) {
4965 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4966 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004967 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004968 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004969 __ movl(Address(base, offset), value.AsRegister<Register>());
4970 }
Calin Juravle52c48962014-12-16 17:02:57 +00004971 break;
4972 }
4973
4974 case Primitive::kPrimLong: {
4975 if (is_volatile) {
4976 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4977 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4978 __ movd(temp1, value.AsRegisterPairLow<Register>());
4979 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4980 __ punpckldq(temp1, temp2);
4981 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004982 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004983 } else if (value.IsConstant()) {
4984 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4985 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4986 codegen_->MaybeRecordImplicitNullCheck(instruction);
4987 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004988 } else {
4989 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004990 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004991 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4992 }
Mark Mendell81489372015-11-04 11:30:41 -05004993 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004994 break;
4995 }
4996
4997 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004998 if (value.IsConstant()) {
4999 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5000 __ movl(Address(base, offset), Immediate(v));
5001 } else {
5002 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5003 }
Calin Juravle52c48962014-12-16 17:02:57 +00005004 break;
5005 }
5006
5007 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005008 if (value.IsConstant()) {
5009 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5010 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5011 codegen_->MaybeRecordImplicitNullCheck(instruction);
5012 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5013 maybe_record_implicit_null_check_done = true;
5014 } else {
5015 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5016 }
Calin Juravle52c48962014-12-16 17:02:57 +00005017 break;
5018 }
5019
5020 case Primitive::kPrimVoid:
5021 LOG(FATAL) << "Unreachable type " << field_type;
5022 UNREACHABLE();
5023 }
5024
Mark Mendell81489372015-11-04 11:30:41 -05005025 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005026 codegen_->MaybeRecordImplicitNullCheck(instruction);
5027 }
5028
Roland Levillain4d027112015-07-01 15:41:14 +01005029 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005030 Register temp = locations->GetTemp(0).AsRegister<Register>();
5031 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005032 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005033 }
5034
Calin Juravle52c48962014-12-16 17:02:57 +00005035 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005036 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005037 }
5038}
5039
5040void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5041 HandleFieldGet(instruction, instruction->GetFieldInfo());
5042}
5043
5044void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5045 HandleFieldGet(instruction, instruction->GetFieldInfo());
5046}
5047
5048void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5049 HandleFieldSet(instruction, instruction->GetFieldInfo());
5050}
5051
5052void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005053 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005054}
5055
5056void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5057 HandleFieldSet(instruction, instruction->GetFieldInfo());
5058}
5059
5060void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005061 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005062}
5063
5064void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5065 HandleFieldGet(instruction, instruction->GetFieldInfo());
5066}
5067
5068void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5069 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005070}
5071
Calin Juravlee460d1d2015-09-29 04:52:17 +01005072void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5073 HUnresolvedInstanceFieldGet* instruction) {
5074 FieldAccessCallingConventionX86 calling_convention;
5075 codegen_->CreateUnresolvedFieldLocationSummary(
5076 instruction, instruction->GetFieldType(), calling_convention);
5077}
5078
5079void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5080 HUnresolvedInstanceFieldGet* instruction) {
5081 FieldAccessCallingConventionX86 calling_convention;
5082 codegen_->GenerateUnresolvedFieldAccess(instruction,
5083 instruction->GetFieldType(),
5084 instruction->GetFieldIndex(),
5085 instruction->GetDexPc(),
5086 calling_convention);
5087}
5088
5089void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5090 HUnresolvedInstanceFieldSet* instruction) {
5091 FieldAccessCallingConventionX86 calling_convention;
5092 codegen_->CreateUnresolvedFieldLocationSummary(
5093 instruction, instruction->GetFieldType(), calling_convention);
5094}
5095
5096void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5097 HUnresolvedInstanceFieldSet* instruction) {
5098 FieldAccessCallingConventionX86 calling_convention;
5099 codegen_->GenerateUnresolvedFieldAccess(instruction,
5100 instruction->GetFieldType(),
5101 instruction->GetFieldIndex(),
5102 instruction->GetDexPc(),
5103 calling_convention);
5104}
5105
5106void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5107 HUnresolvedStaticFieldGet* instruction) {
5108 FieldAccessCallingConventionX86 calling_convention;
5109 codegen_->CreateUnresolvedFieldLocationSummary(
5110 instruction, instruction->GetFieldType(), calling_convention);
5111}
5112
5113void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5114 HUnresolvedStaticFieldGet* instruction) {
5115 FieldAccessCallingConventionX86 calling_convention;
5116 codegen_->GenerateUnresolvedFieldAccess(instruction,
5117 instruction->GetFieldType(),
5118 instruction->GetFieldIndex(),
5119 instruction->GetDexPc(),
5120 calling_convention);
5121}
5122
5123void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5124 HUnresolvedStaticFieldSet* instruction) {
5125 FieldAccessCallingConventionX86 calling_convention;
5126 codegen_->CreateUnresolvedFieldLocationSummary(
5127 instruction, instruction->GetFieldType(), calling_convention);
5128}
5129
5130void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5131 HUnresolvedStaticFieldSet* instruction) {
5132 FieldAccessCallingConventionX86 calling_convention;
5133 codegen_->GenerateUnresolvedFieldAccess(instruction,
5134 instruction->GetFieldType(),
5135 instruction->GetFieldIndex(),
5136 instruction->GetDexPc(),
5137 calling_convention);
5138}
5139
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005140void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005141 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5142 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5143 ? Location::RequiresRegister()
5144 : Location::Any();
5145 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005146}
5147
Calin Juravle2ae48182016-03-16 14:05:09 +00005148void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5149 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005150 return;
5151 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005152 LocationSummary* locations = instruction->GetLocations();
5153 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005154
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005155 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005156 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005157}
5158
Calin Juravle2ae48182016-03-16 14:05:09 +00005159void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005160 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005161 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005162
5163 LocationSummary* locations = instruction->GetLocations();
5164 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005165
5166 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005167 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005168 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005169 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005170 } else {
5171 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005172 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005173 __ jmp(slow_path->GetEntryLabel());
5174 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005175 }
5176 __ j(kEqual, slow_path->GetEntryLabel());
5177}
5178
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005179void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005180 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005181}
5182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005183void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005184 bool object_array_get_with_read_barrier =
5185 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005186 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005187 new (GetGraph()->GetArena()) LocationSummary(instruction,
5188 object_array_get_with_read_barrier ?
5189 LocationSummary::kCallOnSlowPath :
5190 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005191 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005192 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005193 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005194 locations->SetInAt(0, Location::RequiresRegister());
5195 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005196 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5197 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5198 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005199 // The output overlaps in case of long: we don't want the low move
5200 // to overwrite the array's location. Likewise, in the case of an
5201 // object array get with read barriers enabled, we do not want the
5202 // move to overwrite the array's location, as we need it to emit
5203 // the read barrier.
5204 locations->SetOut(
5205 Location::RequiresRegister(),
5206 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5207 Location::kOutputOverlap :
5208 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005209 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210}
5211
5212void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5213 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005214 Location obj_loc = locations->InAt(0);
5215 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005217 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005218 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005219
Calin Juravle77520bc2015-01-12 18:45:46 +00005220 Primitive::Type type = instruction->GetType();
5221 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005222 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005223 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005224 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005225 break;
5226 }
5227
5228 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005229 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005230 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 break;
5232 }
5233
5234 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005235 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005236 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237 break;
5238 }
5239
5240 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005241 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005242 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5243 // Branch cases into compressed and uncompressed for each index's type.
5244 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5245 NearLabel done, not_compressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005246 __ testl(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005247 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005248 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5249 "Expecting 0=compressed, 1=uncompressed");
5250 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005251 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5252 __ jmp(&done);
5253 __ Bind(&not_compressed);
5254 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5255 __ Bind(&done);
5256 } else {
5257 // Common case for charAt of array of char or when string compression's
5258 // feature is turned off.
5259 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5260 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005261 break;
5262 }
5263
Roland Levillain7c1559a2015-12-15 10:55:36 +00005264 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005265 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005266 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005267 break;
5268 }
5269
Roland Levillain7c1559a2015-12-15 10:55:36 +00005270 case Primitive::kPrimNot: {
5271 static_assert(
5272 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5273 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005274 // /* HeapReference<Object> */ out =
5275 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5276 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005277 // Note that a potential implicit null check is handled in this
5278 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5279 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005280 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005281 } else {
5282 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005283 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5284 codegen_->MaybeRecordImplicitNullCheck(instruction);
5285 // If read barriers are enabled, emit read barriers other than
5286 // Baker's using a slow path (and also unpoison the loaded
5287 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005288 if (index.IsConstant()) {
5289 uint32_t offset =
5290 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005291 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5292 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005293 codegen_->MaybeGenerateReadBarrierSlow(
5294 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5295 }
5296 }
5297 break;
5298 }
5299
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005301 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005302 __ movl(out_loc.AsRegisterPairLow<Register>(),
5303 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5304 codegen_->MaybeRecordImplicitNullCheck(instruction);
5305 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5306 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005307 break;
5308 }
5309
Mark Mendell7c8d0092015-01-26 11:21:33 -05005310 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005311 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005312 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005313 break;
5314 }
5315
5316 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005317 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005318 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005319 break;
5320 }
5321
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005322 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005323 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005324 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005325 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005326
Roland Levillain7c1559a2015-12-15 10:55:36 +00005327 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5328 // Potential implicit null checks, in the case of reference or
5329 // long arrays, are handled in the previous switch statement.
5330 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005331 codegen_->MaybeRecordImplicitNullCheck(instruction);
5332 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005333}
5334
5335void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005336 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005337
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005338 bool needs_write_barrier =
5339 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005340 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005341
Nicolas Geoffray39468442014-09-02 15:17:15 +01005342 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5343 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005344 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005345 LocationSummary::kCallOnSlowPath :
5346 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005347
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005348 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5349 || (value_type == Primitive::kPrimByte);
5350 // We need the inputs to be different than the output in case of long operation.
5351 // In case of a byte operation, the register allocator does not support multiple
5352 // inputs that die at entry with one in a specific register.
5353 locations->SetInAt(0, Location::RequiresRegister());
5354 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5355 if (is_byte_type) {
5356 // Ensure the value is in a byte register.
5357 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5358 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005359 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005360 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005361 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5362 }
5363 if (needs_write_barrier) {
5364 // Temporary registers for the write barrier.
5365 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5366 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005367 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005368 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005369}
5370
5371void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5372 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005373 Location array_loc = locations->InAt(0);
5374 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005375 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005376 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005377 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005378 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5379 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5380 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005381 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005382 bool needs_write_barrier =
5383 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005384
5385 switch (value_type) {
5386 case Primitive::kPrimBoolean:
5387 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005388 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005389 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005390 if (value.IsRegister()) {
5391 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005392 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005393 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005394 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005395 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005396 break;
5397 }
5398
5399 case Primitive::kPrimShort:
5400 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005401 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005402 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005403 if (value.IsRegister()) {
5404 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005405 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005406 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005408 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005409 break;
5410 }
5411
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005412 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005413 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005414 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005415
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005416 if (!value.IsRegister()) {
5417 // Just setting null.
5418 DCHECK(instruction->InputAt(2)->IsNullConstant());
5419 DCHECK(value.IsConstant()) << value;
5420 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005421 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005422 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005423 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005424 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005425 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005426
5427 DCHECK(needs_write_barrier);
5428 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005429 // We cannot use a NearLabel for `done`, as its range may be too
5430 // short when Baker read barriers are enabled.
5431 Label done;
5432 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005433 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005434 Location temp_loc = locations->GetTemp(0);
5435 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005436 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005437 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5438 codegen_->AddSlowPath(slow_path);
5439 if (instruction->GetValueCanBeNull()) {
5440 __ testl(register_value, register_value);
5441 __ j(kNotEqual, &not_null);
5442 __ movl(address, Immediate(0));
5443 codegen_->MaybeRecordImplicitNullCheck(instruction);
5444 __ jmp(&done);
5445 __ Bind(&not_null);
5446 }
5447
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005448 // Note that when Baker read barriers are enabled, the type
5449 // checks are performed without read barriers. This is fine,
5450 // even in the case where a class object is in the from-space
5451 // after the flip, as a comparison involving such a type would
5452 // not produce a false positive; it may of course produce a
5453 // false negative, in which case we would take the ArraySet
5454 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005455
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005456 // /* HeapReference<Class> */ temp = array->klass_
5457 __ movl(temp, Address(array, class_offset));
5458 codegen_->MaybeRecordImplicitNullCheck(instruction);
5459 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005460
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005461 // /* HeapReference<Class> */ temp = temp->component_type_
5462 __ movl(temp, Address(temp, component_offset));
5463 // If heap poisoning is enabled, no need to unpoison `temp`
5464 // nor the object reference in `register_value->klass`, as
5465 // we are comparing two poisoned references.
5466 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005467
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005468 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5469 __ j(kEqual, &do_put);
5470 // If heap poisoning is enabled, the `temp` reference has
5471 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005472 __ MaybeUnpoisonHeapReference(temp);
5473
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005474 // If heap poisoning is enabled, no need to unpoison the
5475 // heap reference loaded below, as it is only used for a
5476 // comparison with null.
5477 __ cmpl(Address(temp, super_offset), Immediate(0));
5478 __ j(kNotEqual, slow_path->GetEntryLabel());
5479 __ Bind(&do_put);
5480 } else {
5481 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005482 }
5483 }
5484
5485 if (kPoisonHeapReferences) {
5486 __ movl(temp, register_value);
5487 __ PoisonHeapReference(temp);
5488 __ movl(address, temp);
5489 } else {
5490 __ movl(address, register_value);
5491 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005492 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005493 codegen_->MaybeRecordImplicitNullCheck(instruction);
5494 }
5495
5496 Register card = locations->GetTemp(1).AsRegister<Register>();
5497 codegen_->MarkGCCard(
5498 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5499 __ Bind(&done);
5500
5501 if (slow_path != nullptr) {
5502 __ Bind(slow_path->GetExitLabel());
5503 }
5504
5505 break;
5506 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005507
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005508 case Primitive::kPrimInt: {
5509 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005510 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005511 if (value.IsRegister()) {
5512 __ movl(address, value.AsRegister<Register>());
5513 } else {
5514 DCHECK(value.IsConstant()) << value;
5515 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5516 __ movl(address, Immediate(v));
5517 }
5518 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005519 break;
5520 }
5521
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005522 case Primitive::kPrimLong: {
5523 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005524 if (value.IsRegisterPair()) {
5525 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5526 value.AsRegisterPairLow<Register>());
5527 codegen_->MaybeRecordImplicitNullCheck(instruction);
5528 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5529 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005530 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005531 DCHECK(value.IsConstant());
5532 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5533 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5534 Immediate(Low32Bits(val)));
5535 codegen_->MaybeRecordImplicitNullCheck(instruction);
5536 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5537 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005538 }
5539 break;
5540 }
5541
Mark Mendell7c8d0092015-01-26 11:21:33 -05005542 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005543 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005544 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005545 if (value.IsFpuRegister()) {
5546 __ movss(address, value.AsFpuRegister<XmmRegister>());
5547 } else {
5548 DCHECK(value.IsConstant());
5549 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5550 __ movl(address, Immediate(v));
5551 }
5552 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005553 break;
5554 }
5555
5556 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005557 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005558 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005559 if (value.IsFpuRegister()) {
5560 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5561 } else {
5562 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005563 Address address_hi =
5564 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005565 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5566 __ movl(address, Immediate(Low32Bits(v)));
5567 codegen_->MaybeRecordImplicitNullCheck(instruction);
5568 __ movl(address_hi, Immediate(High32Bits(v)));
5569 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005570 break;
5571 }
5572
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005573 case Primitive::kPrimVoid:
5574 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005575 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005576 }
5577}
5578
5579void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5580 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005581 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005582 if (!instruction->IsEmittedAtUseSite()) {
5583 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5584 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005585}
5586
5587void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005588 if (instruction->IsEmittedAtUseSite()) {
5589 return;
5590 }
5591
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005592 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005593 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005594 Register obj = locations->InAt(0).AsRegister<Register>();
5595 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005596 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005597 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005598 // Mask out most significant bit in case the array is String's array of char.
5599 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005600 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005601 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005602}
5603
5604void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005605 RegisterSet caller_saves = RegisterSet::Empty();
5606 InvokeRuntimeCallingConvention calling_convention;
5607 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5608 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5609 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005610 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005611 HInstruction* length = instruction->InputAt(1);
5612 if (!length->IsEmittedAtUseSite()) {
5613 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5614 }
jessicahandojo4877b792016-09-08 19:49:13 -07005615 // Need register to see array's length.
5616 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5617 locations->AddTemp(Location::RequiresRegister());
5618 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005619}
5620
5621void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005622 const bool is_string_compressed_char_at =
5623 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005624 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005625 Location index_loc = locations->InAt(0);
5626 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005627 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005628 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005629
Mark Mendell99dbd682015-04-22 16:18:52 -04005630 if (length_loc.IsConstant()) {
5631 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5632 if (index_loc.IsConstant()) {
5633 // BCE will remove the bounds check if we are guarenteed to pass.
5634 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5635 if (index < 0 || index >= length) {
5636 codegen_->AddSlowPath(slow_path);
5637 __ jmp(slow_path->GetEntryLabel());
5638 } else {
5639 // Some optimization after BCE may have generated this, and we should not
5640 // generate a bounds check if it is a valid range.
5641 }
5642 return;
5643 }
5644
5645 // We have to reverse the jump condition because the length is the constant.
5646 Register index_reg = index_loc.AsRegister<Register>();
5647 __ cmpl(index_reg, Immediate(length));
5648 codegen_->AddSlowPath(slow_path);
5649 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005650 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005651 HInstruction* array_length = instruction->InputAt(1);
5652 if (array_length->IsEmittedAtUseSite()) {
5653 // Address the length field in the array.
5654 DCHECK(array_length->IsArrayLength());
5655 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5656 Location array_loc = array_length->GetLocations()->InAt(0);
5657 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005658 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005659 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5660 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005661 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5662 __ movl(length_reg, array_len);
5663 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005664 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005665 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005666 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005667 // Checking bounds for general case:
5668 // Array of char or string's array with feature compression off.
5669 if (index_loc.IsConstant()) {
5670 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5671 __ cmpl(array_len, Immediate(value));
5672 } else {
5673 __ cmpl(array_len, index_loc.AsRegister<Register>());
5674 }
5675 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005676 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005677 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005678 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005679 }
5680 codegen_->AddSlowPath(slow_path);
5681 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005682 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005683}
5684
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005685void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005686 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005687}
5688
5689void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005690 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5691}
5692
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005693void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005694 LocationSummary* locations =
5695 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005696 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005697}
5698
5699void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005700 HBasicBlock* block = instruction->GetBlock();
5701 if (block->GetLoopInformation() != nullptr) {
5702 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5703 // The back edge will generate the suspend check.
5704 return;
5705 }
5706 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5707 // The goto will generate the suspend check.
5708 return;
5709 }
5710 GenerateSuspendCheck(instruction, nullptr);
5711}
5712
5713void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5714 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005715 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005716 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5717 if (slow_path == nullptr) {
5718 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5719 instruction->SetSlowPath(slow_path);
5720 codegen_->AddSlowPath(slow_path);
5721 if (successor != nullptr) {
5722 DCHECK(successor->IsLoopHeader());
5723 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5724 }
5725 } else {
5726 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5727 }
5728
Andreas Gampe542451c2016-07-26 09:02:02 -07005729 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005730 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005731 if (successor == nullptr) {
5732 __ j(kNotEqual, slow_path->GetEntryLabel());
5733 __ Bind(slow_path->GetReturnLabel());
5734 } else {
5735 __ j(kEqual, codegen_->GetLabelOf(successor));
5736 __ jmp(slow_path->GetEntryLabel());
5737 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005738}
5739
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005740X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5741 return codegen_->GetAssembler();
5742}
5743
Mark Mendell7c8d0092015-01-26 11:21:33 -05005744void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005745 ScratchRegisterScope ensure_scratch(
5746 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5747 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5748 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5749 __ movl(temp_reg, Address(ESP, src + stack_offset));
5750 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005751}
5752
5753void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005754 ScratchRegisterScope ensure_scratch(
5755 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5756 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5757 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5758 __ movl(temp_reg, Address(ESP, src + stack_offset));
5759 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5760 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5761 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005762}
5763
5764void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005765 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005766 Location source = move->GetSource();
5767 Location destination = move->GetDestination();
5768
5769 if (source.IsRegister()) {
5770 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005771 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005772 } else if (destination.IsFpuRegister()) {
5773 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005774 } else {
5775 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005776 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005777 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005778 } else if (source.IsRegisterPair()) {
5779 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5780 // Create stack space for 2 elements.
5781 __ subl(ESP, Immediate(2 * elem_size));
5782 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5783 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5784 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5785 // And remove the temporary stack space we allocated.
5786 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005787 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005788 if (destination.IsRegister()) {
5789 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5790 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005791 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005792 } else if (destination.IsRegisterPair()) {
5793 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5794 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5795 __ psrlq(src_reg, Immediate(32));
5796 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005797 } else if (destination.IsStackSlot()) {
5798 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5799 } else {
5800 DCHECK(destination.IsDoubleStackSlot());
5801 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5802 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005803 } else if (source.IsStackSlot()) {
5804 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005805 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005806 } else if (destination.IsFpuRegister()) {
5807 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005808 } else {
5809 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005810 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5811 }
5812 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005813 if (destination.IsRegisterPair()) {
5814 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5815 __ movl(destination.AsRegisterPairHigh<Register>(),
5816 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5817 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005818 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5819 } else {
5820 DCHECK(destination.IsDoubleStackSlot()) << destination;
5821 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005822 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005823 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005824 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005825 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005826 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005827 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005828 if (value == 0) {
5829 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5830 } else {
5831 __ movl(destination.AsRegister<Register>(), Immediate(value));
5832 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005833 } else {
5834 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005835 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005836 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005837 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005838 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005839 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005840 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005841 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005842 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5843 if (value == 0) {
5844 // Easy handling of 0.0.
5845 __ xorps(dest, dest);
5846 } else {
5847 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005848 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5849 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5850 __ movl(temp, Immediate(value));
5851 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005852 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005853 } else {
5854 DCHECK(destination.IsStackSlot()) << destination;
5855 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5856 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005857 } else if (constant->IsLongConstant()) {
5858 int64_t value = constant->AsLongConstant()->GetValue();
5859 int32_t low_value = Low32Bits(value);
5860 int32_t high_value = High32Bits(value);
5861 Immediate low(low_value);
5862 Immediate high(high_value);
5863 if (destination.IsDoubleStackSlot()) {
5864 __ movl(Address(ESP, destination.GetStackIndex()), low);
5865 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5866 } else {
5867 __ movl(destination.AsRegisterPairLow<Register>(), low);
5868 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5869 }
5870 } else {
5871 DCHECK(constant->IsDoubleConstant());
5872 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005873 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005874 int32_t low_value = Low32Bits(value);
5875 int32_t high_value = High32Bits(value);
5876 Immediate low(low_value);
5877 Immediate high(high_value);
5878 if (destination.IsFpuRegister()) {
5879 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5880 if (value == 0) {
5881 // Easy handling of 0.0.
5882 __ xorpd(dest, dest);
5883 } else {
5884 __ pushl(high);
5885 __ pushl(low);
5886 __ movsd(dest, Address(ESP, 0));
5887 __ addl(ESP, Immediate(8));
5888 }
5889 } else {
5890 DCHECK(destination.IsDoubleStackSlot()) << destination;
5891 __ movl(Address(ESP, destination.GetStackIndex()), low);
5892 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5893 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005894 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005895 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005896 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005897 }
5898}
5899
Mark Mendella5c19ce2015-04-01 12:51:05 -04005900void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005901 Register suggested_scratch = reg == EAX ? EBX : EAX;
5902 ScratchRegisterScope ensure_scratch(
5903 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5904
5905 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5906 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5907 __ movl(Address(ESP, mem + stack_offset), reg);
5908 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005909}
5910
Mark Mendell7c8d0092015-01-26 11:21:33 -05005911void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005912 ScratchRegisterScope ensure_scratch(
5913 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5914
5915 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5916 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5917 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5918 __ movss(Address(ESP, mem + stack_offset), reg);
5919 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005920}
5921
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005922void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005923 ScratchRegisterScope ensure_scratch1(
5924 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005925
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005926 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5927 ScratchRegisterScope ensure_scratch2(
5928 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005929
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005930 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5931 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5932 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5933 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5934 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5935 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005936}
5937
5938void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005939 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005940 Location source = move->GetSource();
5941 Location destination = move->GetDestination();
5942
5943 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005944 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5945 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5946 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5947 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5948 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005949 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005950 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005951 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005952 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005953 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5954 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005955 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5956 // Use XOR Swap algorithm to avoid a temporary.
5957 DCHECK_NE(source.reg(), destination.reg());
5958 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5959 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5960 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5961 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5962 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5963 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5964 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005965 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5966 // Take advantage of the 16 bytes in the XMM register.
5967 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5968 Address stack(ESP, destination.GetStackIndex());
5969 // Load the double into the high doubleword.
5970 __ movhpd(reg, stack);
5971
5972 // Store the low double into the destination.
5973 __ movsd(stack, reg);
5974
5975 // Move the high double to the low double.
5976 __ psrldq(reg, Immediate(8));
5977 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5978 // Take advantage of the 16 bytes in the XMM register.
5979 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5980 Address stack(ESP, source.GetStackIndex());
5981 // Load the double into the high doubleword.
5982 __ movhpd(reg, stack);
5983
5984 // Store the low double into the destination.
5985 __ movsd(stack, reg);
5986
5987 // Move the high double to the low double.
5988 __ psrldq(reg, Immediate(8));
5989 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5990 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5991 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005992 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005993 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005994 }
5995}
5996
5997void ParallelMoveResolverX86::SpillScratch(int reg) {
5998 __ pushl(static_cast<Register>(reg));
5999}
6000
6001void ParallelMoveResolverX86::RestoreScratch(int reg) {
6002 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006003}
6004
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006005HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6006 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006007 switch (desired_class_load_kind) {
6008 case HLoadClass::LoadKind::kReferrersClass:
6009 break;
6010 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6011 DCHECK(!GetCompilerOptions().GetCompilePic());
6012 break;
6013 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6014 DCHECK(GetCompilerOptions().GetCompilePic());
6015 FALLTHROUGH_INTENDED;
6016 case HLoadClass::LoadKind::kDexCachePcRelative:
6017 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
6018 // We disable pc-relative load when there is an irreducible loop, as the optimization
6019 // is incompatible with it.
6020 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6021 // with irreducible loops.
6022 if (GetGraph()->HasIrreducibleLoops()) {
6023 return HLoadClass::LoadKind::kDexCacheViaMethod;
6024 }
6025 break;
6026 case HLoadClass::LoadKind::kBootImageAddress:
6027 break;
6028 case HLoadClass::LoadKind::kDexCacheAddress:
6029 DCHECK(Runtime::Current()->UseJitCompilation());
6030 break;
6031 case HLoadClass::LoadKind::kDexCacheViaMethod:
6032 break;
6033 }
6034 return desired_class_load_kind;
6035}
6036
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006037void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006038 if (cls->NeedsAccessCheck()) {
6039 InvokeRuntimeCallingConvention calling_convention;
6040 CodeGenerator::CreateLoadClassLocationSummary(
6041 cls,
6042 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6043 Location::RegisterLocation(EAX),
6044 /* code_generator_supports_read_barrier */ true);
6045 return;
6046 }
6047
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006048 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6049 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006050 ? LocationSummary::kCallOnSlowPath
6051 : LocationSummary::kNoCall;
6052 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006053 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006054 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006055 }
6056
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006057 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6058 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6059 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6060 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6061 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6062 locations->SetInAt(0, Location::RequiresRegister());
6063 }
6064 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006065}
6066
6067void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006068 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006069 if (cls->NeedsAccessCheck()) {
6070 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescuba45db02016-07-12 22:53:02 +01006071 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006072 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006073 return;
6074 }
6075
Roland Levillain0d5a2812015-11-13 10:07:31 +00006076 Location out_loc = locations->Out();
6077 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006078
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006079 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006080 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6081 ? kWithoutReadBarrier
6082 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006083 switch (cls->GetLoadKind()) {
6084 case HLoadClass::LoadKind::kReferrersClass: {
6085 DCHECK(!cls->CanCallRuntime());
6086 DCHECK(!cls->MustGenerateClinitCheck());
6087 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6088 Register current_method = locations->InAt(0).AsRegister<Register>();
6089 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006090 cls,
6091 out_loc,
6092 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006093 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006094 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006095 break;
6096 }
6097 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006098 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006099 __ movl(out, Immediate(/* placeholder */ 0));
6100 codegen_->RecordTypePatch(cls);
6101 break;
6102 }
6103 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006104 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006105 Register method_address = locations->InAt(0).AsRegister<Register>();
6106 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6107 codegen_->RecordTypePatch(cls);
6108 break;
6109 }
6110 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006111 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006112 DCHECK_NE(cls->GetAddress(), 0u);
6113 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6114 __ movl(out, Immediate(address));
6115 codegen_->RecordSimplePatch();
6116 break;
6117 }
6118 case HLoadClass::LoadKind::kDexCacheAddress: {
6119 DCHECK_NE(cls->GetAddress(), 0u);
6120 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6121 // /* GcRoot<mirror::Class> */ out = *address
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006122 GenerateGcRootFieldLoad(cls,
6123 out_loc,
6124 Address::Absolute(address),
Roland Levillain00468f32016-10-27 18:02:48 +01006125 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006126 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006127 generate_null_check = !cls->IsInDexCache();
6128 break;
6129 }
6130 case HLoadClass::LoadKind::kDexCachePcRelative: {
6131 Register base_reg = locations->InAt(0).AsRegister<Register>();
6132 uint32_t offset = cls->GetDexCacheElementOffset();
6133 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6134 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006135 GenerateGcRootFieldLoad(cls,
6136 out_loc,
6137 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
6138 fixup_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006139 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006140 generate_null_check = !cls->IsInDexCache();
6141 break;
6142 }
6143 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6144 // /* GcRoot<mirror::Class>[] */ out =
6145 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6146 Register current_method = locations->InAt(0).AsRegister<Register>();
6147 __ movl(out, Address(current_method,
6148 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6149 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006150 GenerateGcRootFieldLoad(cls,
6151 out_loc,
6152 Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())),
Roland Levillain00468f32016-10-27 18:02:48 +01006153 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006154 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006155 generate_null_check = !cls->IsInDexCache();
6156 break;
6157 }
6158 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006159
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006160 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6161 DCHECK(cls->CanCallRuntime());
6162 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6163 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6164 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006165
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006166 if (generate_null_check) {
6167 __ testl(out, out);
6168 __ j(kEqual, slow_path->GetEntryLabel());
6169 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006170
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006171 if (cls->MustGenerateClinitCheck()) {
6172 GenerateClassInitializationCheck(slow_path, out);
6173 } else {
6174 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006175 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006176 }
6177}
6178
6179void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6180 LocationSummary* locations =
6181 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6182 locations->SetInAt(0, Location::RequiresRegister());
6183 if (check->HasUses()) {
6184 locations->SetOut(Location::SameAsFirstInput());
6185 }
6186}
6187
6188void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006189 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006190 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006191 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006192 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006193 GenerateClassInitializationCheck(slow_path,
6194 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006195}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006196
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006197void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006198 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006199 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6200 Immediate(mirror::Class::kStatusInitialized));
6201 __ j(kLess, slow_path->GetEntryLabel());
6202 __ Bind(slow_path->GetExitLabel());
6203 // No need for memory fence, thanks to the X86 memory model.
6204}
6205
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006206HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6207 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006208 switch (desired_string_load_kind) {
6209 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6210 DCHECK(!GetCompilerOptions().GetCompilePic());
6211 break;
6212 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6213 DCHECK(GetCompilerOptions().GetCompilePic());
6214 FALLTHROUGH_INTENDED;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006215 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006216 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006217 // We disable pc-relative load when there is an irreducible loop, as the optimization
6218 // is incompatible with it.
6219 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6220 // with irreducible loops.
6221 if (GetGraph()->HasIrreducibleLoops()) {
6222 return HLoadString::LoadKind::kDexCacheViaMethod;
6223 }
6224 break;
6225 case HLoadString::LoadKind::kBootImageAddress:
6226 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006227 case HLoadString::LoadKind::kDexCacheViaMethod:
6228 break;
6229 }
6230 return desired_string_load_kind;
6231}
6232
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006233void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006234 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00006235 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
6236 ? LocationSummary::kCallOnMainOnly
6237 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006238 : LocationSummary::kNoCall;
6239 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006240 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006241 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006242 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006243 locations->SetInAt(0, Location::RequiresRegister());
6244 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006245 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6246 locations->SetOut(Location::RegisterLocation(EAX));
6247 } else {
6248 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006249 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6250 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6251 // Rely on the pResolveString and/or marking to save everything.
6252 RegisterSet caller_saves = RegisterSet::Empty();
6253 InvokeRuntimeCallingConvention calling_convention;
6254 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6255 locations->SetCustomSlowPathCallerSaves(caller_saves);
6256 } else {
6257 // For non-Baker read barrier we have a temp-clobbering call.
6258 }
6259 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006260 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006261}
6262
6263void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006264 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006265 Location out_loc = locations->Out();
6266 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006267
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006268 switch (load->GetLoadKind()) {
6269 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006270 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006271 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006272 return; // No dex cache slow path.
6273 }
6274 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006275 Register method_address = locations->InAt(0).AsRegister<Register>();
6276 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006277 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006278 return; // No dex cache slow path.
6279 }
6280 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006281 DCHECK_NE(load->GetAddress(), 0u);
6282 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6283 __ movl(out, Immediate(address));
6284 codegen_->RecordSimplePatch();
6285 return; // No dex cache slow path.
6286 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006287 case HLoadString::LoadKind::kBssEntry: {
6288 Register method_address = locations->InAt(0).AsRegister<Register>();
6289 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6290 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray07c919f2016-11-09 17:29:03 +00006291 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006292 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006293 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6294 codegen_->AddSlowPath(slow_path);
6295 __ testl(out, out);
6296 __ j(kEqual, slow_path->GetEntryLabel());
6297 __ Bind(slow_path->GetExitLabel());
6298 return;
6299 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006300 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006301 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006302 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006303
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006304 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006305 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006306 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006307 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex()));
6308 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6309 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006310}
6311
David Brazdilcb1c0552015-08-04 16:22:25 +01006312static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006313 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006314}
6315
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006316void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6317 LocationSummary* locations =
6318 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6319 locations->SetOut(Location::RequiresRegister());
6320}
6321
6322void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006323 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6324}
6325
6326void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6327 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6328}
6329
6330void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6331 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006332}
6333
6334void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6335 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006336 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006337 InvokeRuntimeCallingConvention calling_convention;
6338 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6339}
6340
6341void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006342 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006343 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006344}
6345
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006346// Temp is used for read barrier.
6347static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6348 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006349 !kUseBakerReadBarrier &&
6350 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006351 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006352 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6353 return 1;
6354 }
6355 return 0;
6356}
6357
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006358// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006359// interface pointer, one for loading the current interface.
6360// The other checks have one temp for loading the object's class.
6361static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6362 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6363 return 2;
6364 }
6365 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006366}
6367
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006368void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006369 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006370 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006371 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006372 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006373 case TypeCheckKind::kExactCheck:
6374 case TypeCheckKind::kAbstractClassCheck:
6375 case TypeCheckKind::kClassHierarchyCheck:
6376 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006377 call_kind =
6378 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006379 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006380 break;
6381 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006382 case TypeCheckKind::kUnresolvedCheck:
6383 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006384 call_kind = LocationSummary::kCallOnSlowPath;
6385 break;
6386 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006387
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006388 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006389 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006390 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006391 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006392 locations->SetInAt(0, Location::RequiresRegister());
6393 locations->SetInAt(1, Location::Any());
6394 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6395 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006396 // When read barriers are enabled, we need a temporary register for some cases.
6397 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006398}
6399
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006400void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006401 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006402 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006403 Location obj_loc = locations->InAt(0);
6404 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006405 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006406 Location out_loc = locations->Out();
6407 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006408 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6409 DCHECK_LE(num_temps, 1u);
6410 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006411 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006412 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6413 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6414 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006415 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006416 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006417
6418 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006419 // Avoid null check if we know obj is not null.
6420 if (instruction->MustDoNullCheck()) {
6421 __ testl(obj, obj);
6422 __ j(kEqual, &zero);
6423 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006424
Roland Levillain0d5a2812015-11-13 10:07:31 +00006425 // /* HeapReference<Class> */ out = obj->klass_
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006426 GenerateReferenceLoadTwoRegisters(instruction,
6427 out_loc,
6428 obj_loc,
6429 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006430 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006431
Roland Levillain7c1559a2015-12-15 10:55:36 +00006432 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006433 case TypeCheckKind::kExactCheck: {
6434 if (cls.IsRegister()) {
6435 __ cmpl(out, cls.AsRegister<Register>());
6436 } else {
6437 DCHECK(cls.IsStackSlot()) << cls;
6438 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6439 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006440
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006441 // Classes must be equal for the instanceof to succeed.
6442 __ j(kNotEqual, &zero);
6443 __ movl(out, Immediate(1));
6444 __ jmp(&done);
6445 break;
6446 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006447
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006448 case TypeCheckKind::kAbstractClassCheck: {
6449 // If the class is abstract, we eagerly fetch the super class of the
6450 // object to avoid doing a comparison we know will fail.
6451 NearLabel loop;
6452 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006453 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006454 GenerateReferenceLoadOneRegister(instruction,
6455 out_loc,
6456 super_offset,
6457 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006458 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006459 __ testl(out, out);
6460 // If `out` is null, we use it for the result, and jump to `done`.
6461 __ j(kEqual, &done);
6462 if (cls.IsRegister()) {
6463 __ cmpl(out, cls.AsRegister<Register>());
6464 } else {
6465 DCHECK(cls.IsStackSlot()) << cls;
6466 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6467 }
6468 __ j(kNotEqual, &loop);
6469 __ movl(out, Immediate(1));
6470 if (zero.IsLinked()) {
6471 __ jmp(&done);
6472 }
6473 break;
6474 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006475
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006476 case TypeCheckKind::kClassHierarchyCheck: {
6477 // Walk over the class hierarchy to find a match.
6478 NearLabel loop, success;
6479 __ Bind(&loop);
6480 if (cls.IsRegister()) {
6481 __ cmpl(out, cls.AsRegister<Register>());
6482 } else {
6483 DCHECK(cls.IsStackSlot()) << cls;
6484 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6485 }
6486 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006487 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006488 GenerateReferenceLoadOneRegister(instruction,
6489 out_loc,
6490 super_offset,
6491 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006492 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006493 __ testl(out, out);
6494 __ j(kNotEqual, &loop);
6495 // If `out` is null, we use it for the result, and jump to `done`.
6496 __ jmp(&done);
6497 __ Bind(&success);
6498 __ movl(out, Immediate(1));
6499 if (zero.IsLinked()) {
6500 __ jmp(&done);
6501 }
6502 break;
6503 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006504
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006505 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006506 // Do an exact check.
6507 NearLabel exact_check;
6508 if (cls.IsRegister()) {
6509 __ cmpl(out, cls.AsRegister<Register>());
6510 } else {
6511 DCHECK(cls.IsStackSlot()) << cls;
6512 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6513 }
6514 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006515 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006516 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006517 GenerateReferenceLoadOneRegister(instruction,
6518 out_loc,
6519 component_offset,
6520 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006521 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006522 __ testl(out, out);
6523 // If `out` is null, we use it for the result, and jump to `done`.
6524 __ j(kEqual, &done);
6525 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6526 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006527 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006528 __ movl(out, Immediate(1));
6529 __ jmp(&done);
6530 break;
6531 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006532
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006533 case TypeCheckKind::kArrayCheck: {
6534 if (cls.IsRegister()) {
6535 __ cmpl(out, cls.AsRegister<Register>());
6536 } else {
6537 DCHECK(cls.IsStackSlot()) << cls;
6538 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6539 }
6540 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006541 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6542 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006543 codegen_->AddSlowPath(slow_path);
6544 __ j(kNotEqual, slow_path->GetEntryLabel());
6545 __ movl(out, Immediate(1));
6546 if (zero.IsLinked()) {
6547 __ jmp(&done);
6548 }
6549 break;
6550 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006551
Calin Juravle98893e12015-10-02 21:05:03 +01006552 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006553 case TypeCheckKind::kInterfaceCheck: {
6554 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006555 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006556 // cases.
6557 //
6558 // We cannot directly call the InstanceofNonTrivial runtime
6559 // entry point without resorting to a type checking slow path
6560 // here (i.e. by calling InvokeRuntime directly), as it would
6561 // require to assign fixed registers for the inputs of this
6562 // HInstanceOf instruction (following the runtime calling
6563 // convention), which might be cluttered by the potential first
6564 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006565 //
6566 // TODO: Introduce a new runtime entry point taking the object
6567 // to test (instead of its class) as argument, and let it deal
6568 // with the read barrier issues. This will let us refactor this
6569 // case of the `switch` code as it was previously (with a direct
6570 // call to the runtime not using a type checking slow path).
6571 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006572 DCHECK(locations->OnlyCallsOnSlowPath());
6573 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6574 /* is_fatal */ false);
6575 codegen_->AddSlowPath(slow_path);
6576 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006577 if (zero.IsLinked()) {
6578 __ jmp(&done);
6579 }
6580 break;
6581 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006582 }
6583
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006584 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006585 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006586 __ xorl(out, out);
6587 }
6588
6589 if (done.IsLinked()) {
6590 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006591 }
6592
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006593 if (slow_path != nullptr) {
6594 __ Bind(slow_path->GetExitLabel());
6595 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006596}
6597
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006598static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6599 switch (type_check_kind) {
6600 case TypeCheckKind::kExactCheck:
6601 case TypeCheckKind::kAbstractClassCheck:
6602 case TypeCheckKind::kClassHierarchyCheck:
6603 case TypeCheckKind::kArrayObjectCheck:
6604 return !throws_into_catch && !kEmitCompilerReadBarrier;
6605 case TypeCheckKind::kInterfaceCheck:
6606 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6607 case TypeCheckKind::kArrayCheck:
6608 case TypeCheckKind::kUnresolvedCheck:
6609 return false;
6610 }
6611 LOG(FATAL) << "Unreachable";
6612 UNREACHABLE();
6613}
6614
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006615void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006616 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006617 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006618 LocationSummary::CallKind call_kind =
6619 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6620 ? LocationSummary::kNoCall
6621 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006622 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6623 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006624 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6625 // Require a register for the interface check since there is a loop that compares the class to
6626 // a memory address.
6627 locations->SetInAt(1, Location::RequiresRegister());
6628 } else {
6629 locations->SetInAt(1, Location::Any());
6630 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006631 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6632 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006633 // When read barriers are enabled, we need an additional temporary register for some cases.
6634 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6635}
6636
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006637void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006638 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006639 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006640 Location obj_loc = locations->InAt(0);
6641 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006642 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006643 Location temp_loc = locations->GetTemp(0);
6644 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006645 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6646 DCHECK_GE(num_temps, 1u);
6647 DCHECK_LE(num_temps, 2u);
6648 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6649 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6650 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6651 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6652 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6653 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6654 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6655 const uint32_t object_array_data_offset =
6656 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006657
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006658 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6659 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6660 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006661 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006662 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6663
Roland Levillain0d5a2812015-11-13 10:07:31 +00006664 SlowPathCode* type_check_slow_path =
6665 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6666 is_type_check_slow_path_fatal);
6667 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006668
Roland Levillain0d5a2812015-11-13 10:07:31 +00006669 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006670 // Avoid null check if we know obj is not null.
6671 if (instruction->MustDoNullCheck()) {
6672 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006673 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006674 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006675
Roland Levillain0d5a2812015-11-13 10:07:31 +00006676 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006677 case TypeCheckKind::kExactCheck:
6678 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006679 // /* HeapReference<Class> */ temp = obj->klass_
6680 GenerateReferenceLoadTwoRegisters(instruction,
6681 temp_loc,
6682 obj_loc,
6683 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006684 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006685
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006686 if (cls.IsRegister()) {
6687 __ cmpl(temp, cls.AsRegister<Register>());
6688 } else {
6689 DCHECK(cls.IsStackSlot()) << cls;
6690 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6691 }
6692 // Jump to slow path for throwing the exception or doing a
6693 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006694 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006695 break;
6696 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006697
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006698 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006699 // /* HeapReference<Class> */ temp = obj->klass_
6700 GenerateReferenceLoadTwoRegisters(instruction,
6701 temp_loc,
6702 obj_loc,
6703 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006704 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006705
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006706 // If the class is abstract, we eagerly fetch the super class of the
6707 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006708 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006709 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006710 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006711 GenerateReferenceLoadOneRegister(instruction,
6712 temp_loc,
6713 super_offset,
6714 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006715 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006716
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006717 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6718 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006719 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006720 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006721
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006722 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006723 if (cls.IsRegister()) {
6724 __ cmpl(temp, cls.AsRegister<Register>());
6725 } else {
6726 DCHECK(cls.IsStackSlot()) << cls;
6727 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6728 }
6729 __ j(kNotEqual, &loop);
6730 break;
6731 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006732
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006733 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006734 // /* HeapReference<Class> */ temp = obj->klass_
6735 GenerateReferenceLoadTwoRegisters(instruction,
6736 temp_loc,
6737 obj_loc,
6738 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006739 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006740
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006741 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006742 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006743 __ Bind(&loop);
6744 if (cls.IsRegister()) {
6745 __ cmpl(temp, cls.AsRegister<Register>());
6746 } else {
6747 DCHECK(cls.IsStackSlot()) << cls;
6748 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6749 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006750 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006751
Roland Levillain0d5a2812015-11-13 10:07:31 +00006752 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006753 GenerateReferenceLoadOneRegister(instruction,
6754 temp_loc,
6755 super_offset,
6756 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006757 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006758
6759 // If the class reference currently in `temp` is not null, jump
6760 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006761 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006762 __ j(kNotZero, &loop);
6763 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006764 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006765 break;
6766 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006767
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006768 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006769 // /* HeapReference<Class> */ temp = obj->klass_
6770 GenerateReferenceLoadTwoRegisters(instruction,
6771 temp_loc,
6772 obj_loc,
6773 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006774 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006775
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006776 // Do an exact check.
6777 if (cls.IsRegister()) {
6778 __ cmpl(temp, cls.AsRegister<Register>());
6779 } else {
6780 DCHECK(cls.IsStackSlot()) << cls;
6781 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6782 }
6783 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006784
6785 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006786 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006787 GenerateReferenceLoadOneRegister(instruction,
6788 temp_loc,
6789 component_offset,
6790 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006791 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006792
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006793 // If the component type is null (i.e. the object not an array), jump to the slow path to
6794 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006795 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006796 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006797
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006798 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006799 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006800 break;
6801 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006802
Calin Juravle98893e12015-10-02 21:05:03 +01006803 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006804 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006805 // We cannot directly call the CheckCast runtime entry point
6806 // without resorting to a type checking slow path here (i.e. by
6807 // calling InvokeRuntime directly), as it would require to
6808 // assign fixed registers for the inputs of this HInstanceOf
6809 // instruction (following the runtime calling convention), which
6810 // might be cluttered by the potential first read barrier
6811 // emission at the beginning of this method.
6812 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006813 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006814
6815 case TypeCheckKind::kInterfaceCheck: {
6816 // Fast path for the interface check. Since we compare with a memory location in the inner
6817 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6818 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006819 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006820 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006821 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6822 // doing this.
6823 // /* HeapReference<Class> */ temp = obj->klass_
6824 GenerateReferenceLoadTwoRegisters(instruction,
6825 temp_loc,
6826 obj_loc,
6827 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006828 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006829
6830 // /* HeapReference<Class> */ temp = temp->iftable_
6831 GenerateReferenceLoadTwoRegisters(instruction,
6832 temp_loc,
6833 temp_loc,
6834 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006835 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006836 NearLabel is_null;
6837 // Null iftable means it is empty.
6838 __ testl(temp, temp);
6839 __ j(kZero, &is_null);
6840
6841 // Loop through the iftable and check if any class matches.
6842 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
6843
6844 NearLabel start_loop;
6845 __ Bind(&start_loop);
6846 __ cmpl(cls.AsRegister<Register>(), Address(temp, object_array_data_offset));
6847 __ j(kEqual, &done); // Return if same class.
6848 // Go to next interface.
6849 __ addl(temp, Immediate(2 * kHeapReferenceSize));
6850 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
6851 __ j(kNotZero, &start_loop);
6852 __ Bind(&is_null);
6853 }
6854
6855 __ jmp(type_check_slow_path->GetEntryLabel());
6856 break;
6857 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006858 }
6859 __ Bind(&done);
6860
Roland Levillain0d5a2812015-11-13 10:07:31 +00006861 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006862}
6863
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006864void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6865 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006866 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006867 InvokeRuntimeCallingConvention calling_convention;
6868 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6869}
6870
6871void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006872 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6873 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006874 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006875 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006876 if (instruction->IsEnter()) {
6877 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6878 } else {
6879 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6880 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006881}
6882
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006883void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6884void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6885void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6886
6887void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6888 LocationSummary* locations =
6889 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6890 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6891 || instruction->GetResultType() == Primitive::kPrimLong);
6892 locations->SetInAt(0, Location::RequiresRegister());
6893 locations->SetInAt(1, Location::Any());
6894 locations->SetOut(Location::SameAsFirstInput());
6895}
6896
6897void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6898 HandleBitwiseOperation(instruction);
6899}
6900
6901void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6902 HandleBitwiseOperation(instruction);
6903}
6904
6905void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6906 HandleBitwiseOperation(instruction);
6907}
6908
6909void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6910 LocationSummary* locations = instruction->GetLocations();
6911 Location first = locations->InAt(0);
6912 Location second = locations->InAt(1);
6913 DCHECK(first.Equals(locations->Out()));
6914
6915 if (instruction->GetResultType() == Primitive::kPrimInt) {
6916 if (second.IsRegister()) {
6917 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006918 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006919 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006920 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006921 } else {
6922 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006923 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006924 }
6925 } else if (second.IsConstant()) {
6926 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006927 __ andl(first.AsRegister<Register>(),
6928 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006929 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006930 __ orl(first.AsRegister<Register>(),
6931 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006932 } else {
6933 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006934 __ xorl(first.AsRegister<Register>(),
6935 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006936 }
6937 } else {
6938 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006939 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006940 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006941 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006942 } else {
6943 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006944 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006945 }
6946 }
6947 } else {
6948 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6949 if (second.IsRegisterPair()) {
6950 if (instruction->IsAnd()) {
6951 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6952 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6953 } else if (instruction->IsOr()) {
6954 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6955 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6956 } else {
6957 DCHECK(instruction->IsXor());
6958 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6959 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6960 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006961 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006962 if (instruction->IsAnd()) {
6963 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6964 __ andl(first.AsRegisterPairHigh<Register>(),
6965 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6966 } else if (instruction->IsOr()) {
6967 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6968 __ orl(first.AsRegisterPairHigh<Register>(),
6969 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6970 } else {
6971 DCHECK(instruction->IsXor());
6972 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6973 __ xorl(first.AsRegisterPairHigh<Register>(),
6974 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6975 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006976 } else {
6977 DCHECK(second.IsConstant()) << second;
6978 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006979 int32_t low_value = Low32Bits(value);
6980 int32_t high_value = High32Bits(value);
6981 Immediate low(low_value);
6982 Immediate high(high_value);
6983 Register first_low = first.AsRegisterPairLow<Register>();
6984 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006985 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006986 if (low_value == 0) {
6987 __ xorl(first_low, first_low);
6988 } else if (low_value != -1) {
6989 __ andl(first_low, low);
6990 }
6991 if (high_value == 0) {
6992 __ xorl(first_high, first_high);
6993 } else if (high_value != -1) {
6994 __ andl(first_high, high);
6995 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006996 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006997 if (low_value != 0) {
6998 __ orl(first_low, low);
6999 }
7000 if (high_value != 0) {
7001 __ orl(first_high, high);
7002 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007003 } else {
7004 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007005 if (low_value != 0) {
7006 __ xorl(first_low, low);
7007 }
7008 if (high_value != 0) {
7009 __ xorl(first_high, high);
7010 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007011 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007012 }
7013 }
7014}
7015
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007016void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7017 HInstruction* instruction,
7018 Location out,
7019 uint32_t offset,
7020 Location maybe_temp,
7021 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007022 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007023 if (read_barrier_option == kWithReadBarrier) {
7024 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007025 if (kUseBakerReadBarrier) {
7026 // Load with fast path based Baker's read barrier.
7027 // /* HeapReference<Object> */ out = *(out + offset)
7028 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007029 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007030 } else {
7031 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007032 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007033 // in the following move operation, as we will need it for the
7034 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007035 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007036 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007037 // /* HeapReference<Object> */ out = *(out + offset)
7038 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007039 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007040 }
7041 } else {
7042 // Plain load with no read barrier.
7043 // /* HeapReference<Object> */ out = *(out + offset)
7044 __ movl(out_reg, Address(out_reg, offset));
7045 __ MaybeUnpoisonHeapReference(out_reg);
7046 }
7047}
7048
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007049void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7050 HInstruction* instruction,
7051 Location out,
7052 Location obj,
7053 uint32_t offset,
7054 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007055 Register out_reg = out.AsRegister<Register>();
7056 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007057 if (read_barrier_option == kWithReadBarrier) {
7058 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007059 if (kUseBakerReadBarrier) {
7060 // Load with fast path based Baker's read barrier.
7061 // /* HeapReference<Object> */ out = *(obj + offset)
7062 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007063 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007064 } else {
7065 // Load with slow path based read barrier.
7066 // /* HeapReference<Object> */ out = *(obj + offset)
7067 __ movl(out_reg, Address(obj_reg, offset));
7068 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7069 }
7070 } else {
7071 // Plain load with no read barrier.
7072 // /* HeapReference<Object> */ out = *(obj + offset)
7073 __ movl(out_reg, Address(obj_reg, offset));
7074 __ MaybeUnpoisonHeapReference(out_reg);
7075 }
7076}
7077
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007078void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7079 HInstruction* instruction,
7080 Location root,
7081 const Address& address,
7082 Label* fixup_label,
7083 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007084 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007085 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007086 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007087 if (kUseBakerReadBarrier) {
7088 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7089 // Baker's read barrier are used:
7090 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007091 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00007092 // if (Thread::Current()->GetIsGcMarking()) {
7093 // root = ReadBarrier::Mark(root)
7094 // }
7095
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007096 // /* GcRoot<mirror::Object> */ root = *address
7097 __ movl(root_reg, address);
7098 if (fixup_label != nullptr) {
7099 __ Bind(fixup_label);
7100 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007101 static_assert(
7102 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7103 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7104 "have different sizes.");
7105 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7106 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7107 "have different sizes.");
7108
Vladimir Marko953437b2016-08-24 08:30:46 +00007109 // Slow path marking the GC root `root`.
7110 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007111 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007112 codegen_->AddSlowPath(slow_path);
7113
Andreas Gampe542451c2016-07-26 09:02:02 -07007114 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00007115 Immediate(0));
7116 __ j(kNotEqual, slow_path->GetEntryLabel());
7117 __ Bind(slow_path->GetExitLabel());
7118 } else {
7119 // GC root loaded through a slow path for read barriers other
7120 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007121 // /* GcRoot<mirror::Object>* */ root = address
7122 __ leal(root_reg, address);
7123 if (fixup_label != nullptr) {
7124 __ Bind(fixup_label);
7125 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007126 // /* mirror::Object* */ root = root->Read()
7127 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7128 }
7129 } else {
7130 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007131 // /* GcRoot<mirror::Object> */ root = *address
7132 __ movl(root_reg, address);
7133 if (fixup_label != nullptr) {
7134 __ Bind(fixup_label);
7135 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007136 // Note that GC roots are not affected by heap poisoning, thus we
7137 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007138 }
7139}
7140
7141void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7142 Location ref,
7143 Register obj,
7144 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007145 bool needs_null_check) {
7146 DCHECK(kEmitCompilerReadBarrier);
7147 DCHECK(kUseBakerReadBarrier);
7148
7149 // /* HeapReference<Object> */ ref = *(obj + offset)
7150 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007151 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007152}
7153
7154void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7155 Location ref,
7156 Register obj,
7157 uint32_t data_offset,
7158 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007159 bool needs_null_check) {
7160 DCHECK(kEmitCompilerReadBarrier);
7161 DCHECK(kUseBakerReadBarrier);
7162
Roland Levillain3d312422016-06-23 13:53:42 +01007163 static_assert(
7164 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7165 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007166 // /* HeapReference<Object> */ ref =
7167 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007168 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007169 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007170}
7171
7172void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7173 Location ref,
7174 Register obj,
7175 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007176 bool needs_null_check,
7177 bool always_update_field,
7178 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007179 DCHECK(kEmitCompilerReadBarrier);
7180 DCHECK(kUseBakerReadBarrier);
7181
7182 // In slow path based read barriers, the read barrier call is
7183 // inserted after the original load. However, in fast path based
7184 // Baker's read barriers, we need to perform the load of
7185 // mirror::Object::monitor_ *before* the original reference load.
7186 // This load-load ordering is required by the read barrier.
7187 // The fast path/slow path (for Baker's algorithm) should look like:
7188 //
7189 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7190 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7191 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007192 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007193 // if (is_gray) {
7194 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7195 // }
7196 //
7197 // Note: the original implementation in ReadBarrier::Barrier is
7198 // slightly more complex as:
7199 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007200 // the high-bits of rb_state, which are expected to be all zeroes
7201 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7202 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007203 // - it performs additional checks that we do not do here for
7204 // performance reasons.
7205
7206 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007207 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7208
Vladimir Marko953437b2016-08-24 08:30:46 +00007209 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007210 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7211 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007212 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7213 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7214 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7215
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007216 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007217 // ref = ReadBarrier::Mark(ref);
7218 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7219 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007220 if (needs_null_check) {
7221 MaybeRecordImplicitNullCheck(instruction);
7222 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007223
7224 // Load fence to prevent load-load reordering.
7225 // Note that this is a no-op, thanks to the x86 memory model.
7226 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7227
7228 // The actual reference load.
7229 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007230 __ movl(ref_reg, src); // Flags are unaffected.
7231
7232 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7233 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007234 SlowPathCode* slow_path;
7235 if (always_update_field) {
7236 DCHECK(temp != nullptr);
7237 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7238 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7239 } else {
7240 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7241 instruction, ref, /* unpoison_ref_before_marking */ true);
7242 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007243 AddSlowPath(slow_path);
7244
7245 // We have done the "if" of the gray bit check above, now branch based on the flags.
7246 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007247
7248 // Object* ref = ref_addr->AsMirrorPtr()
7249 __ MaybeUnpoisonHeapReference(ref_reg);
7250
Roland Levillain7c1559a2015-12-15 10:55:36 +00007251 __ Bind(slow_path->GetExitLabel());
7252}
7253
7254void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7255 Location out,
7256 Location ref,
7257 Location obj,
7258 uint32_t offset,
7259 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007260 DCHECK(kEmitCompilerReadBarrier);
7261
Roland Levillain7c1559a2015-12-15 10:55:36 +00007262 // Insert a slow path based read barrier *after* the reference load.
7263 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007264 // If heap poisoning is enabled, the unpoisoning of the loaded
7265 // reference will be carried out by the runtime within the slow
7266 // path.
7267 //
7268 // Note that `ref` currently does not get unpoisoned (when heap
7269 // poisoning is enabled), which is alright as the `ref` argument is
7270 // not used by the artReadBarrierSlow entry point.
7271 //
7272 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7273 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7274 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7275 AddSlowPath(slow_path);
7276
Roland Levillain0d5a2812015-11-13 10:07:31 +00007277 __ jmp(slow_path->GetEntryLabel());
7278 __ Bind(slow_path->GetExitLabel());
7279}
7280
Roland Levillain7c1559a2015-12-15 10:55:36 +00007281void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7282 Location out,
7283 Location ref,
7284 Location obj,
7285 uint32_t offset,
7286 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007287 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007288 // Baker's read barriers shall be handled by the fast path
7289 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7290 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007291 // If heap poisoning is enabled, unpoisoning will be taken care of
7292 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007293 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007294 } else if (kPoisonHeapReferences) {
7295 __ UnpoisonHeapReference(out.AsRegister<Register>());
7296 }
7297}
7298
Roland Levillain7c1559a2015-12-15 10:55:36 +00007299void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7300 Location out,
7301 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007302 DCHECK(kEmitCompilerReadBarrier);
7303
Roland Levillain7c1559a2015-12-15 10:55:36 +00007304 // Insert a slow path based read barrier *after* the GC root load.
7305 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007306 // Note that GC roots are not affected by heap poisoning, so we do
7307 // not need to do anything special for this here.
7308 SlowPathCode* slow_path =
7309 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7310 AddSlowPath(slow_path);
7311
Roland Levillain0d5a2812015-11-13 10:07:31 +00007312 __ jmp(slow_path->GetEntryLabel());
7313 __ Bind(slow_path->GetExitLabel());
7314}
7315
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007316void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007317 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007318 LOG(FATAL) << "Unreachable";
7319}
7320
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007321void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007322 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007323 LOG(FATAL) << "Unreachable";
7324}
7325
Mark Mendellfe57faa2015-09-18 09:26:15 -04007326// Simple implementation of packed switch - generate cascaded compare/jumps.
7327void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7328 LocationSummary* locations =
7329 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7330 locations->SetInAt(0, Location::RequiresRegister());
7331}
7332
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007333void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7334 int32_t lower_bound,
7335 uint32_t num_entries,
7336 HBasicBlock* switch_block,
7337 HBasicBlock* default_block) {
7338 // Figure out the correct compare values and jump conditions.
7339 // Handle the first compare/branch as a special case because it might
7340 // jump to the default case.
7341 DCHECK_GT(num_entries, 2u);
7342 Condition first_condition;
7343 uint32_t index;
7344 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7345 if (lower_bound != 0) {
7346 first_condition = kLess;
7347 __ cmpl(value_reg, Immediate(lower_bound));
7348 __ j(first_condition, codegen_->GetLabelOf(default_block));
7349 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007350
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007351 index = 1;
7352 } else {
7353 // Handle all the compare/jumps below.
7354 first_condition = kBelow;
7355 index = 0;
7356 }
7357
7358 // Handle the rest of the compare/jumps.
7359 for (; index + 1 < num_entries; index += 2) {
7360 int32_t compare_to_value = lower_bound + index + 1;
7361 __ cmpl(value_reg, Immediate(compare_to_value));
7362 // Jump to successors[index] if value < case_value[index].
7363 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7364 // Jump to successors[index + 1] if value == case_value[index + 1].
7365 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7366 }
7367
7368 if (index != num_entries) {
7369 // There are an odd number of entries. Handle the last one.
7370 DCHECK_EQ(index + 1, num_entries);
7371 __ cmpl(value_reg, Immediate(lower_bound + index));
7372 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007373 }
7374
7375 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007376 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7377 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007378 }
7379}
7380
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007381void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7382 int32_t lower_bound = switch_instr->GetStartValue();
7383 uint32_t num_entries = switch_instr->GetNumEntries();
7384 LocationSummary* locations = switch_instr->GetLocations();
7385 Register value_reg = locations->InAt(0).AsRegister<Register>();
7386
7387 GenPackedSwitchWithCompares(value_reg,
7388 lower_bound,
7389 num_entries,
7390 switch_instr->GetBlock(),
7391 switch_instr->GetDefaultBlock());
7392}
7393
Mark Mendell805b3b52015-09-18 14:10:29 -04007394void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7395 LocationSummary* locations =
7396 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7397 locations->SetInAt(0, Location::RequiresRegister());
7398
7399 // Constant area pointer.
7400 locations->SetInAt(1, Location::RequiresRegister());
7401
7402 // And the temporary we need.
7403 locations->AddTemp(Location::RequiresRegister());
7404}
7405
7406void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7407 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007408 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007409 LocationSummary* locations = switch_instr->GetLocations();
7410 Register value_reg = locations->InAt(0).AsRegister<Register>();
7411 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7412
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007413 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7414 GenPackedSwitchWithCompares(value_reg,
7415 lower_bound,
7416 num_entries,
7417 switch_instr->GetBlock(),
7418 default_block);
7419 return;
7420 }
7421
Mark Mendell805b3b52015-09-18 14:10:29 -04007422 // Optimizing has a jump area.
7423 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7424 Register constant_area = locations->InAt(1).AsRegister<Register>();
7425
7426 // Remove the bias, if needed.
7427 if (lower_bound != 0) {
7428 __ leal(temp_reg, Address(value_reg, -lower_bound));
7429 value_reg = temp_reg;
7430 }
7431
7432 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007433 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007434 __ cmpl(value_reg, Immediate(num_entries - 1));
7435 __ j(kAbove, codegen_->GetLabelOf(default_block));
7436
7437 // We are in the range of the table.
7438 // Load (target-constant_area) from the jump table, indexing by the value.
7439 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7440
7441 // Compute the actual target address by adding in constant_area.
7442 __ addl(temp_reg, constant_area);
7443
7444 // And jump.
7445 __ jmp(temp_reg);
7446}
7447
Mark Mendell0616ae02015-04-17 12:49:27 -04007448void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7449 HX86ComputeBaseMethodAddress* insn) {
7450 LocationSummary* locations =
7451 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7452 locations->SetOut(Location::RequiresRegister());
7453}
7454
7455void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7456 HX86ComputeBaseMethodAddress* insn) {
7457 LocationSummary* locations = insn->GetLocations();
7458 Register reg = locations->Out().AsRegister<Register>();
7459
7460 // Generate call to next instruction.
7461 Label next_instruction;
7462 __ call(&next_instruction);
7463 __ Bind(&next_instruction);
7464
7465 // Remember this offset for later use with constant area.
7466 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7467
7468 // Grab the return address off the stack.
7469 __ popl(reg);
7470}
7471
7472void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7473 HX86LoadFromConstantTable* insn) {
7474 LocationSummary* locations =
7475 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7476
7477 locations->SetInAt(0, Location::RequiresRegister());
7478 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7479
7480 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007481 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007482 return;
7483 }
7484
7485 switch (insn->GetType()) {
7486 case Primitive::kPrimFloat:
7487 case Primitive::kPrimDouble:
7488 locations->SetOut(Location::RequiresFpuRegister());
7489 break;
7490
7491 case Primitive::kPrimInt:
7492 locations->SetOut(Location::RequiresRegister());
7493 break;
7494
7495 default:
7496 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7497 }
7498}
7499
7500void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007501 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007502 return;
7503 }
7504
7505 LocationSummary* locations = insn->GetLocations();
7506 Location out = locations->Out();
7507 Register const_area = locations->InAt(0).AsRegister<Register>();
7508 HConstant *value = insn->GetConstant();
7509
7510 switch (insn->GetType()) {
7511 case Primitive::kPrimFloat:
7512 __ movss(out.AsFpuRegister<XmmRegister>(),
7513 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7514 break;
7515
7516 case Primitive::kPrimDouble:
7517 __ movsd(out.AsFpuRegister<XmmRegister>(),
7518 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7519 break;
7520
7521 case Primitive::kPrimInt:
7522 __ movl(out.AsRegister<Register>(),
7523 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7524 break;
7525
7526 default:
7527 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7528 }
7529}
7530
Mark Mendell0616ae02015-04-17 12:49:27 -04007531/**
7532 * Class to handle late fixup of offsets into constant area.
7533 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007534class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007535 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007536 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7537 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7538
7539 protected:
7540 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7541
7542 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007543
7544 private:
7545 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7546 // Patch the correct offset for the instruction. The place to patch is the
7547 // last 4 bytes of the instruction.
7548 // The value to patch is the distance from the offset in the constant area
7549 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007550 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7551 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007552
7553 // Patch in the right value.
7554 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7555 }
7556
Mark Mendell0616ae02015-04-17 12:49:27 -04007557 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007558 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007559};
7560
Mark Mendell805b3b52015-09-18 14:10:29 -04007561/**
7562 * Class to handle late fixup of offsets to a jump table that will be created in the
7563 * constant area.
7564 */
7565class JumpTableRIPFixup : public RIPFixup {
7566 public:
7567 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7568 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7569
7570 void CreateJumpTable() {
7571 X86Assembler* assembler = codegen_->GetAssembler();
7572
7573 // Ensure that the reference to the jump table has the correct offset.
7574 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7575 SetOffset(offset_in_constant_table);
7576
7577 // The label values in the jump table are computed relative to the
7578 // instruction addressing the constant area.
7579 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7580
7581 // Populate the jump table with the correct values for the jump table.
7582 int32_t num_entries = switch_instr_->GetNumEntries();
7583 HBasicBlock* block = switch_instr_->GetBlock();
7584 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7585 // The value that we want is the target offset - the position of the table.
7586 for (int32_t i = 0; i < num_entries; i++) {
7587 HBasicBlock* b = successors[i];
7588 Label* l = codegen_->GetLabelOf(b);
7589 DCHECK(l->IsBound());
7590 int32_t offset_to_block = l->Position() - relative_offset;
7591 assembler->AppendInt32(offset_to_block);
7592 }
7593 }
7594
7595 private:
7596 const HX86PackedSwitch* switch_instr_;
7597};
7598
7599void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7600 // Generate the constant area if needed.
7601 X86Assembler* assembler = GetAssembler();
7602 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7603 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7604 // byte values.
7605 assembler->Align(4, 0);
7606 constant_area_start_ = assembler->CodeSize();
7607
7608 // Populate any jump tables.
7609 for (auto jump_table : fixups_to_jump_tables_) {
7610 jump_table->CreateJumpTable();
7611 }
7612
7613 // And now add the constant area to the generated code.
7614 assembler->AddConstantArea();
7615 }
7616
7617 // And finish up.
7618 CodeGenerator::Finalize(allocator);
7619}
7620
Mark Mendell0616ae02015-04-17 12:49:27 -04007621Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7622 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7623 return Address(reg, kDummy32BitOffset, fixup);
7624}
7625
7626Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7627 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7628 return Address(reg, kDummy32BitOffset, fixup);
7629}
7630
7631Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7632 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7633 return Address(reg, kDummy32BitOffset, fixup);
7634}
7635
7636Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7637 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7638 return Address(reg, kDummy32BitOffset, fixup);
7639}
7640
Aart Bika19616e2016-02-01 18:57:58 -08007641void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7642 if (value == 0) {
7643 __ xorl(dest, dest);
7644 } else {
7645 __ movl(dest, Immediate(value));
7646 }
7647}
7648
7649void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7650 if (value == 0) {
7651 __ testl(dest, dest);
7652 } else {
7653 __ cmpl(dest, Immediate(value));
7654 }
7655}
7656
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007657void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7658 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007659 GenerateIntCompare(lhs_reg, rhs);
7660}
7661
7662void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007663 if (rhs.IsConstant()) {
7664 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007665 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007666 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007667 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007668 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007669 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007670 }
7671}
7672
7673Address CodeGeneratorX86::ArrayAddress(Register obj,
7674 Location index,
7675 ScaleFactor scale,
7676 uint32_t data_offset) {
7677 return index.IsConstant() ?
7678 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7679 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7680}
7681
Mark Mendell805b3b52015-09-18 14:10:29 -04007682Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7683 Register reg,
7684 Register value) {
7685 // Create a fixup to be used to create and address the jump table.
7686 JumpTableRIPFixup* table_fixup =
7687 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7688
7689 // We have to populate the jump tables.
7690 fixups_to_jump_tables_.push_back(table_fixup);
7691
7692 // We want a scaled address, as we are extracting the correct offset from the table.
7693 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7694}
7695
Andreas Gampe85b62f22015-09-09 13:15:38 -07007696// TODO: target as memory.
7697void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7698 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007699 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007700 return;
7701 }
7702
7703 DCHECK_NE(type, Primitive::kPrimVoid);
7704
7705 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7706 if (target.Equals(return_loc)) {
7707 return;
7708 }
7709
7710 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7711 // with the else branch.
7712 if (type == Primitive::kPrimLong) {
7713 HParallelMove parallel_move(GetGraph()->GetArena());
7714 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7715 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7716 GetMoveResolver()->EmitNativeCode(&parallel_move);
7717 } else {
7718 // Let the parallel move resolver take care of all of this.
7719 HParallelMove parallel_move(GetGraph()->GetArena());
7720 parallel_move.AddMove(return_loc, target, type, nullptr);
7721 GetMoveResolver()->EmitNativeCode(&parallel_move);
7722 }
7723}
7724
Roland Levillain4d027112015-07-01 15:41:14 +01007725#undef __
7726
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007727} // namespace x86
7728} // namespace art