blob: 715b5be2c8cc78925a6b170050a701dc0ce2e1ab [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 Levillain62a46b22015-06-01 18:24:13 +010050#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010051#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052
Andreas Gampe85b62f22015-09-09 13:15:38 -070053class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000055 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056
Alexandre Rames2ed20af2015-03-06 13:55:35 +000057 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010058 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000060 if (instruction_->CanThrowIntoCatchBlock()) {
61 // Live registers will be restored in the catch block if caught.
62 SaveLiveRegisters(codegen, instruction_->GetLocations());
63 }
Alexandre Rames8158f282015-08-07 10:26:17 +010064 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
65 instruction_,
66 instruction_->GetDexPc(),
67 this);
Roland Levillain888d0672015-11-23 18:53:50 +000068 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069 }
70
Alexandre Rames8158f282015-08-07 10:26:17 +010071 bool IsFatal() const OVERRIDE { return true; }
72
Alexandre Rames9931f312015-06-19 14:47:01 +010073 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
74
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
77};
78
Andreas Gampe85b62f22015-09-09 13:15:38 -070079class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000080 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000081 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000082
Alexandre Rames2ed20af2015-03-06 13:55:35 +000083 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010084 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000085 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000086 if (instruction_->CanThrowIntoCatchBlock()) {
87 // Live registers will be restored in the catch block if caught.
88 SaveLiveRegisters(codegen, instruction_->GetLocations());
89 }
Alexandre Rames8158f282015-08-07 10:26:17 +010090 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
91 instruction_,
92 instruction_->GetDexPc(),
93 this);
Roland Levillain888d0672015-11-23 18:53:50 +000094 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000095 }
96
Alexandre Rames8158f282015-08-07 10:26:17 +010097 bool IsFatal() const OVERRIDE { return true; }
98
Alexandre Rames9931f312015-06-19 14:47:01 +010099 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
100
Calin Juravled0d48522014-11-04 16:40:20 +0000101 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000102 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
103};
104
Andreas Gampe85b62f22015-09-09 13:15:38 -0700105class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000106 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000107 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
108 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000109
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000110 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000111 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000112 if (is_div_) {
113 __ negl(reg_);
114 } else {
115 __ movl(reg_, Immediate(0));
116 }
Calin Juravled0d48522014-11-04 16:40:20 +0000117 __ jmp(GetExitLabel());
118 }
119
Alexandre Rames9931f312015-06-19 14:47:01 +0100120 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
121
Calin Juravled0d48522014-11-04 16:40:20 +0000122 private:
123 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000124 bool is_div_;
125 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000126};
127
Andreas Gampe85b62f22015-09-09 13:15:38 -0700128class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000130 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100131
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000132 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100133 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100134 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100135 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000136 // We're moving two locations to locations that could overlap, so we need a parallel
137 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000138 if (instruction_->CanThrowIntoCatchBlock()) {
139 // Live registers will be restored in the catch block if caught.
140 SaveLiveRegisters(codegen, instruction_->GetLocations());
141 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000143 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100144 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000145 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100146 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100147 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100148 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
149 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100150 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
151 instruction_,
152 instruction_->GetDexPc(),
153 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000154 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155 }
156
Alexandre Rames8158f282015-08-07 10:26:17 +0100157 bool IsFatal() const OVERRIDE { return true; }
158
Alexandre Rames9931f312015-06-19 14:47:01 +0100159 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
160
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100161 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100162 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
163};
164
Andreas Gampe85b62f22015-09-09 13:15:38 -0700165class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000166 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000167 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000168 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000169
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000170 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100171 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000172 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000173 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100174 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
175 instruction_,
176 instruction_->GetDexPc(),
177 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000178 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000179 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100180 if (successor_ == nullptr) {
181 __ jmp(GetReturnLabel());
182 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100183 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100184 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000185 }
186
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100187 Label* GetReturnLabel() {
188 DCHECK(successor_ == nullptr);
189 return &return_label_;
190 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000191
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100192 HBasicBlock* GetSuccessor() const {
193 return successor_;
194 }
195
Alexandre Rames9931f312015-06-19 14:47:01 +0100196 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
197
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000198 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100199 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000200 Label return_label_;
201
202 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
203};
204
Andreas Gampe85b62f22015-09-09 13:15:38 -0700205class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000206 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000207 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000208
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000209 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000210 LocationSummary* locations = instruction_->GetLocations();
211 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
212
213 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
214 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000215 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000216
217 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000218 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
219 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
Alexandre Rames8158f282015-08-07 10:26:17 +0100220 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
221 instruction_,
222 instruction_->GetDexPc(),
223 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000224 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000225 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000226 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000227
228 __ jmp(GetExitLabel());
229 }
230
Alexandre Rames9931f312015-06-19 14:47:01 +0100231 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
232
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000233 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000234 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
235};
236
Andreas Gampe85b62f22015-09-09 13:15:38 -0700237class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000238 public:
239 LoadClassSlowPathX86(HLoadClass* cls,
240 HInstruction* at,
241 uint32_t dex_pc,
242 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000243 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
245 }
246
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000247 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000248 LocationSummary* locations = at_->GetLocations();
249 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
250 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000251 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000252
253 InvokeRuntimeCallingConvention calling_convention;
254 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100255 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
256 : QUICK_ENTRY_POINT(pInitializeType),
257 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000258 if (do_clinit_) {
259 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
260 } else {
261 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
262 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263
264 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000265 Location out = locations->Out();
266 if (out.IsValid()) {
267 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
268 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000270
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000271 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000272 __ jmp(GetExitLabel());
273 }
274
Alexandre Rames9931f312015-06-19 14:47:01 +0100275 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
276
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277 private:
278 // The class this slow path will load.
279 HLoadClass* const cls_;
280
281 // The instruction where this slow path is happening.
282 // (Might be the load class or an initialization check).
283 HInstruction* const at_;
284
285 // The dex PC of `at_`.
286 const uint32_t dex_pc_;
287
288 // Whether to initialize the class.
289 const bool do_clinit_;
290
291 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
292};
293
Andreas Gampe85b62f22015-09-09 13:15:38 -0700294class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000295 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000296 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000297 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000299 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100301 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
302 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000303 DCHECK(instruction_->IsCheckCast()
304 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305
306 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
307 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000308
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000309 if (!is_fatal_) {
310 SaveLiveRegisters(codegen, locations);
311 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312
313 // We're moving two locations to locations that could overlap, so we need a parallel
314 // move resolver.
315 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000316 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100317 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000318 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100319 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100320 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100321 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
322 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000324 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100325 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
326 instruction_,
327 instruction_->GetDexPc(),
328 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000329 CheckEntrypointTypes<
330 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000331 } else {
332 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100333 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
334 instruction_,
335 instruction_->GetDexPc(),
336 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000337 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000338 }
339
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000340 if (!is_fatal_) {
341 if (instruction_->IsInstanceOf()) {
342 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
343 }
344 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000345
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000346 __ jmp(GetExitLabel());
347 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000348 }
349
Alexandre Rames9931f312015-06-19 14:47:01 +0100350 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000351 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100352
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000354 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000355
356 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
357};
358
Andreas Gampe85b62f22015-09-09 13:15:38 -0700359class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700360 public:
Aart Bik42249c32016-01-07 15:33:50 -0800361 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000362 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700363
364 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100365 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700366 __ Bind(GetEntryLabel());
367 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100368 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
369 instruction_,
370 instruction_->GetDexPc(),
371 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000372 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700373 }
374
Alexandre Rames9931f312015-06-19 14:47:01 +0100375 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
376
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700377 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
379};
380
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100381class ArraySetSlowPathX86 : public SlowPathCode {
382 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000383 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100384
385 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
386 LocationSummary* locations = instruction_->GetLocations();
387 __ Bind(GetEntryLabel());
388 SaveLiveRegisters(codegen, locations);
389
390 InvokeRuntimeCallingConvention calling_convention;
391 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
392 parallel_move.AddMove(
393 locations->InAt(0),
394 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
395 Primitive::kPrimNot,
396 nullptr);
397 parallel_move.AddMove(
398 locations->InAt(1),
399 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
400 Primitive::kPrimInt,
401 nullptr);
402 parallel_move.AddMove(
403 locations->InAt(2),
404 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
405 Primitive::kPrimNot,
406 nullptr);
407 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
408
409 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
410 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
411 instruction_,
412 instruction_->GetDexPc(),
413 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000414 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100415 RestoreLiveRegisters(codegen, locations);
416 __ jmp(GetExitLabel());
417 }
418
419 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
420
421 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100422 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
423};
424
Roland Levillain7c1559a2015-12-15 10:55:36 +0000425// Slow path marking an object during a read barrier.
426class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
427 public:
428 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000429 : SlowPathCode(instruction), out_(out), obj_(obj) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000430 DCHECK(kEmitCompilerReadBarrier);
431 }
432
433 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
434
435 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
436 LocationSummary* locations = instruction_->GetLocations();
437 Register reg_out = out_.AsRegister<Register>();
438 DCHECK(locations->CanCall());
439 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
440 DCHECK(instruction_->IsInstanceFieldGet() ||
441 instruction_->IsStaticFieldGet() ||
442 instruction_->IsArrayGet() ||
443 instruction_->IsLoadClass() ||
444 instruction_->IsLoadString() ||
445 instruction_->IsInstanceOf() ||
446 instruction_->IsCheckCast())
447 << "Unexpected instruction in read barrier marking slow path: "
448 << instruction_->DebugName();
449
450 __ Bind(GetEntryLabel());
451 SaveLiveRegisters(codegen, locations);
452
453 InvokeRuntimeCallingConvention calling_convention;
454 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
455 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
456 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
457 instruction_,
458 instruction_->GetDexPc(),
459 this);
460 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
461 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
462
463 RestoreLiveRegisters(codegen, locations);
464 __ jmp(GetExitLabel());
465 }
466
467 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000468 const Location out_;
469 const Location obj_;
470
471 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
472};
473
Roland Levillain0d5a2812015-11-13 10:07:31 +0000474// Slow path generating a read barrier for a heap reference.
475class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
476 public:
477 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
478 Location out,
479 Location ref,
480 Location obj,
481 uint32_t offset,
482 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000483 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000484 out_(out),
485 ref_(ref),
486 obj_(obj),
487 offset_(offset),
488 index_(index) {
489 DCHECK(kEmitCompilerReadBarrier);
490 // If `obj` is equal to `out` or `ref`, it means the initial object
491 // has been overwritten by (or after) the heap object reference load
492 // to be instrumented, e.g.:
493 //
494 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000495 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000496 //
497 // In that case, we have lost the information about the original
498 // object, and the emitted read barrier cannot work properly.
499 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
500 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
501 }
502
503 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
504 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
505 LocationSummary* locations = instruction_->GetLocations();
506 Register reg_out = out_.AsRegister<Register>();
507 DCHECK(locations->CanCall());
508 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
509 DCHECK(!instruction_->IsInvoke() ||
510 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000511 instruction_->GetLocations()->Intrinsified()))
512 << "Unexpected instruction in read barrier for heap reference slow path: "
513 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000514
515 __ Bind(GetEntryLabel());
516 SaveLiveRegisters(codegen, locations);
517
518 // We may have to change the index's value, but as `index_` is a
519 // constant member (like other "inputs" of this slow path),
520 // introduce a copy of it, `index`.
521 Location index = index_;
522 if (index_.IsValid()) {
523 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
524 if (instruction_->IsArrayGet()) {
525 // Compute the actual memory offset and store it in `index`.
526 Register index_reg = index_.AsRegister<Register>();
527 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
528 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
529 // We are about to change the value of `index_reg` (see the
530 // calls to art::x86::X86Assembler::shll and
531 // art::x86::X86Assembler::AddImmediate below), but it has
532 // not been saved by the previous call to
533 // art::SlowPathCode::SaveLiveRegisters, as it is a
534 // callee-save register --
535 // art::SlowPathCode::SaveLiveRegisters does not consider
536 // callee-save registers, as it has been designed with the
537 // assumption that callee-save registers are supposed to be
538 // handled by the called function. So, as a callee-save
539 // register, `index_reg` _would_ eventually be saved onto
540 // the stack, but it would be too late: we would have
541 // changed its value earlier. Therefore, we manually save
542 // it here into another freely available register,
543 // `free_reg`, chosen of course among the caller-save
544 // registers (as a callee-save `free_reg` register would
545 // exhibit the same problem).
546 //
547 // Note we could have requested a temporary register from
548 // the register allocator instead; but we prefer not to, as
549 // this is a slow path, and we know we can find a
550 // caller-save register that is available.
551 Register free_reg = FindAvailableCallerSaveRegister(codegen);
552 __ movl(free_reg, index_reg);
553 index_reg = free_reg;
554 index = Location::RegisterLocation(index_reg);
555 } else {
556 // The initial register stored in `index_` has already been
557 // saved in the call to art::SlowPathCode::SaveLiveRegisters
558 // (as it is not a callee-save register), so we can freely
559 // use it.
560 }
561 // Shifting the index value contained in `index_reg` by the scale
562 // factor (2) cannot overflow in practice, as the runtime is
563 // unable to allocate object arrays with a size larger than
564 // 2^26 - 1 (that is, 2^28 - 4 bytes).
565 __ shll(index_reg, Immediate(TIMES_4));
566 static_assert(
567 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
568 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
569 __ AddImmediate(index_reg, Immediate(offset_));
570 } else {
571 DCHECK(instruction_->IsInvoke());
572 DCHECK(instruction_->GetLocations()->Intrinsified());
573 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
574 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
575 << instruction_->AsInvoke()->GetIntrinsic();
576 DCHECK_EQ(offset_, 0U);
577 DCHECK(index_.IsRegisterPair());
578 // UnsafeGet's offset location is a register pair, the low
579 // part contains the correct offset.
580 index = index_.ToLow();
581 }
582 }
583
584 // We're moving two or three locations to locations that could
585 // overlap, so we need a parallel move resolver.
586 InvokeRuntimeCallingConvention calling_convention;
587 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
588 parallel_move.AddMove(ref_,
589 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
590 Primitive::kPrimNot,
591 nullptr);
592 parallel_move.AddMove(obj_,
593 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
594 Primitive::kPrimNot,
595 nullptr);
596 if (index.IsValid()) {
597 parallel_move.AddMove(index,
598 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
599 Primitive::kPrimInt,
600 nullptr);
601 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
602 } else {
603 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
604 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
605 }
606 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
607 instruction_,
608 instruction_->GetDexPc(),
609 this);
610 CheckEntrypointTypes<
611 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
612 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
613
614 RestoreLiveRegisters(codegen, locations);
615 __ jmp(GetExitLabel());
616 }
617
618 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
619
620 private:
621 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
622 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
623 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
624 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
625 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
626 return static_cast<Register>(i);
627 }
628 }
629 // We shall never fail to find a free caller-save register, as
630 // there are more than two core caller-save registers on x86
631 // (meaning it is possible to find one which is different from
632 // `ref` and `obj`).
633 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
634 LOG(FATAL) << "Could not find a free caller-save register";
635 UNREACHABLE();
636 }
637
Roland Levillain0d5a2812015-11-13 10:07:31 +0000638 const Location out_;
639 const Location ref_;
640 const Location obj_;
641 const uint32_t offset_;
642 // An additional location containing an index to an array.
643 // Only used for HArrayGet and the UnsafeGetObject &
644 // UnsafeGetObjectVolatile intrinsics.
645 const Location index_;
646
647 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
648};
649
650// Slow path generating a read barrier for a GC root.
651class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
652 public:
653 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000654 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000655 DCHECK(kEmitCompilerReadBarrier);
656 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000657
658 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
659 LocationSummary* locations = instruction_->GetLocations();
660 Register reg_out = out_.AsRegister<Register>();
661 DCHECK(locations->CanCall());
662 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000663 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
664 << "Unexpected instruction in read barrier for GC root slow path: "
665 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000666
667 __ Bind(GetEntryLabel());
668 SaveLiveRegisters(codegen, locations);
669
670 InvokeRuntimeCallingConvention calling_convention;
671 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
672 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
673 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
674 instruction_,
675 instruction_->GetDexPc(),
676 this);
677 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
678 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
679
680 RestoreLiveRegisters(codegen, locations);
681 __ jmp(GetExitLabel());
682 }
683
684 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
685
686 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000687 const Location out_;
688 const Location root_;
689
690 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
691};
692
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100693#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100694#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100695
Aart Bike9f37602015-10-09 11:15:55 -0700696inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700697 switch (cond) {
698 case kCondEQ: return kEqual;
699 case kCondNE: return kNotEqual;
700 case kCondLT: return kLess;
701 case kCondLE: return kLessEqual;
702 case kCondGT: return kGreater;
703 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700704 case kCondB: return kBelow;
705 case kCondBE: return kBelowEqual;
706 case kCondA: return kAbove;
707 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700708 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100709 LOG(FATAL) << "Unreachable";
710 UNREACHABLE();
711}
712
Aart Bike9f37602015-10-09 11:15:55 -0700713// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100714inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
715 switch (cond) {
716 case kCondEQ: return kEqual;
717 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700718 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100719 case kCondLT: return kBelow;
720 case kCondLE: return kBelowEqual;
721 case kCondGT: return kAbove;
722 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700723 // Unsigned remain unchanged.
724 case kCondB: return kBelow;
725 case kCondBE: return kBelowEqual;
726 case kCondA: return kAbove;
727 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100728 }
729 LOG(FATAL) << "Unreachable";
730 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700731}
732
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100733void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100734 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100735}
736
737void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100738 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100739}
740
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100741size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
742 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
743 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100744}
745
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100746size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
747 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
748 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100749}
750
Mark Mendell7c8d0092015-01-26 11:21:33 -0500751size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
752 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
753 return GetFloatingPointSpillSlotSize();
754}
755
756size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
757 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
758 return GetFloatingPointSpillSlotSize();
759}
760
Calin Juravle175dc732015-08-25 15:42:32 +0100761void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
762 HInstruction* instruction,
763 uint32_t dex_pc,
764 SlowPathCode* slow_path) {
765 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
766 instruction,
767 dex_pc,
768 slow_path);
769}
770
771void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100772 HInstruction* instruction,
773 uint32_t dex_pc,
774 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100775 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100776 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100777 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100778}
779
Mark Mendellfb8d2792015-03-31 22:16:59 -0400780CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000781 const X86InstructionSetFeatures& isa_features,
782 const CompilerOptions& compiler_options,
783 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500784 : CodeGenerator(graph,
785 kNumberOfCpuRegisters,
786 kNumberOfXmmRegisters,
787 kNumberOfRegisterPairs,
788 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
789 arraysize(kCoreCalleeSaves))
790 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100791 0,
792 compiler_options,
793 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100794 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100795 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100796 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400797 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000798 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100799 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400800 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000801 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000802 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
803 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400804 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000805 // Use a fake return address register to mimic Quick.
806 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100807}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100808
David Brazdil58282f42016-01-14 12:45:10 +0000809void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100810 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100811 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100812
813 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100814 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100815
Calin Juravle34bacdf2014-10-07 20:23:36 +0100816 UpdateBlockedPairRegisters();
817}
818
819void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
820 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
821 X86ManagedRegister current =
822 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
823 if (blocked_core_registers_[current.AsRegisterPairLow()]
824 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
825 blocked_register_pairs_[i] = true;
826 }
827 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100828}
829
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100830InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800831 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100832 assembler_(codegen->GetAssembler()),
833 codegen_(codegen) {}
834
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100835static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100836 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100837}
838
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000839void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100840 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000841 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000842 bool skip_overflow_check =
843 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000844 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000845
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000846 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100847 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100848 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100849 }
850
Mark Mendell5f874182015-03-04 15:42:45 -0500851 if (HasEmptyFrame()) {
852 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000853 }
Mark Mendell5f874182015-03-04 15:42:45 -0500854
855 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
856 Register reg = kCoreCalleeSaves[i];
857 if (allocated_registers_.ContainsCoreRegister(reg)) {
858 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100859 __ cfi().AdjustCFAOffset(kX86WordSize);
860 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500861 }
862 }
863
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100864 int adjust = GetFrameSize() - FrameEntrySpillSize();
865 __ subl(ESP, Immediate(adjust));
866 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100867 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000868}
869
870void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100871 __ cfi().RememberState();
872 if (!HasEmptyFrame()) {
873 int adjust = GetFrameSize() - FrameEntrySpillSize();
874 __ addl(ESP, Immediate(adjust));
875 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500876
David Srbeckyc34dc932015-04-12 09:27:43 +0100877 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
878 Register reg = kCoreCalleeSaves[i];
879 if (allocated_registers_.ContainsCoreRegister(reg)) {
880 __ popl(reg);
881 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
882 __ cfi().Restore(DWARFReg(reg));
883 }
Mark Mendell5f874182015-03-04 15:42:45 -0500884 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000885 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100886 __ ret();
887 __ cfi().RestoreState();
888 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000889}
890
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100891void CodeGeneratorX86::Bind(HBasicBlock* block) {
892 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000893}
894
David Brazdil60328912016-04-04 17:47:42 +0000895Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
896 switch (load->GetType()) {
897 case Primitive::kPrimLong:
898 case Primitive::kPrimDouble:
899 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
900
901 case Primitive::kPrimInt:
902 case Primitive::kPrimNot:
903 case Primitive::kPrimFloat:
904 return Location::StackSlot(GetStackSlot(load->GetLocal()));
905
906 case Primitive::kPrimBoolean:
907 case Primitive::kPrimByte:
908 case Primitive::kPrimChar:
909 case Primitive::kPrimShort:
910 case Primitive::kPrimVoid:
911 LOG(FATAL) << "Unexpected type " << load->GetType();
912 UNREACHABLE();
913 }
914
915 LOG(FATAL) << "Unreachable";
916 UNREACHABLE();
917}
918
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100919Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
920 switch (type) {
921 case Primitive::kPrimBoolean:
922 case Primitive::kPrimByte:
923 case Primitive::kPrimChar:
924 case Primitive::kPrimShort:
925 case Primitive::kPrimInt:
926 case Primitive::kPrimNot:
927 return Location::RegisterLocation(EAX);
928
929 case Primitive::kPrimLong:
930 return Location::RegisterPairLocation(EAX, EDX);
931
932 case Primitive::kPrimVoid:
933 return Location::NoLocation();
934
935 case Primitive::kPrimDouble:
936 case Primitive::kPrimFloat:
937 return Location::FpuRegisterLocation(XMM0);
938 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100939
940 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100941}
942
943Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
944 return Location::RegisterLocation(kMethodRegisterArgument);
945}
946
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100947Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100948 switch (type) {
949 case Primitive::kPrimBoolean:
950 case Primitive::kPrimByte:
951 case Primitive::kPrimChar:
952 case Primitive::kPrimShort:
953 case Primitive::kPrimInt:
954 case Primitive::kPrimNot: {
955 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000956 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100957 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100958 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100959 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000960 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100961 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100962 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100963
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000964 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100965 uint32_t index = gp_index_;
966 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000967 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100968 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100969 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
970 calling_convention.GetRegisterPairAt(index));
971 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100972 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000973 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
974 }
975 }
976
977 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100978 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000979 stack_index_++;
980 if (index < calling_convention.GetNumberOfFpuRegisters()) {
981 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
982 } else {
983 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
984 }
985 }
986
987 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100988 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000989 stack_index_ += 2;
990 if (index < calling_convention.GetNumberOfFpuRegisters()) {
991 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
992 } else {
993 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100994 }
995 }
996
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100997 case Primitive::kPrimVoid:
998 LOG(FATAL) << "Unexpected parameter type " << type;
999 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001000 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001001 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001002}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001003
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001004void CodeGeneratorX86::Move32(Location destination, Location source) {
1005 if (source.Equals(destination)) {
1006 return;
1007 }
1008 if (destination.IsRegister()) {
1009 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001010 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001011 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001012 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001013 } else {
1014 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001015 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001016 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001017 } else if (destination.IsFpuRegister()) {
1018 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001019 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001020 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001021 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001022 } else {
1023 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001024 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001025 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001026 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001027 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001028 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001029 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001030 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001031 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001032 } else if (source.IsConstant()) {
1033 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001034 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001035 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036 } else {
1037 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001038 __ pushl(Address(ESP, source.GetStackIndex()));
1039 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001040 }
1041 }
1042}
1043
1044void CodeGeneratorX86::Move64(Location destination, Location source) {
1045 if (source.Equals(destination)) {
1046 return;
1047 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001048 if (destination.IsRegisterPair()) {
1049 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001050 EmitParallelMoves(
1051 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1052 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001053 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001054 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001055 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1056 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001057 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001058 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1059 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1060 __ psrlq(src_reg, Immediate(32));
1061 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001062 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001063 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001064 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001065 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1066 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1068 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001069 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001070 if (source.IsFpuRegister()) {
1071 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1072 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001073 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001074 } else if (source.IsRegisterPair()) {
1075 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1076 // Create stack space for 2 elements.
1077 __ subl(ESP, Immediate(2 * elem_size));
1078 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1079 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1080 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1081 // And remove the temporary stack space we allocated.
1082 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001083 } else {
1084 LOG(FATAL) << "Unimplemented";
1085 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001086 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001087 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001088 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001089 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001090 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001092 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001093 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001094 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001095 } else if (source.IsConstant()) {
1096 HConstant* constant = source.GetConstant();
1097 int64_t value;
1098 if (constant->IsLongConstant()) {
1099 value = constant->AsLongConstant()->GetValue();
1100 } else {
1101 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001102 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001103 }
1104 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1105 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001107 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001108 EmitParallelMoves(
1109 Location::StackSlot(source.GetStackIndex()),
1110 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001111 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001112 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001113 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1114 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001115 }
1116 }
1117}
1118
Calin Juravle175dc732015-08-25 15:42:32 +01001119void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1120 DCHECK(location.IsRegister());
1121 __ movl(location.AsRegister<Register>(), Immediate(value));
1122}
1123
Calin Juravlee460d1d2015-09-29 04:52:17 +01001124void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001125 HParallelMove move(GetGraph()->GetArena());
1126 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1127 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1128 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001129 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001130 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001131 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001132 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001133}
1134
1135void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1136 if (location.IsRegister()) {
1137 locations->AddTemp(location);
1138 } else if (location.IsRegisterPair()) {
1139 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1140 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1141 } else {
1142 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1143 }
1144}
1145
David Brazdilfc6a86a2015-06-26 10:33:45 +00001146void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001147 DCHECK(!successor->IsExitBlock());
1148
1149 HBasicBlock* block = got->GetBlock();
1150 HInstruction* previous = got->GetPrevious();
1151
1152 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001153 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001154 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1155 return;
1156 }
1157
1158 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1159 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1160 }
1161 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001162 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001163 }
1164}
1165
David Brazdilfc6a86a2015-06-26 10:33:45 +00001166void LocationsBuilderX86::VisitGoto(HGoto* got) {
1167 got->SetLocations(nullptr);
1168}
1169
1170void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1171 HandleGoto(got, got->GetSuccessor());
1172}
1173
1174void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1175 try_boundary->SetLocations(nullptr);
1176}
1177
1178void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1179 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1180 if (!successor->IsExitBlock()) {
1181 HandleGoto(try_boundary, successor);
1182 }
1183}
1184
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001185void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001186 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001187}
1188
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001189void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001190}
1191
Mark Mendell152408f2015-12-31 12:28:50 -05001192template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001193void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001194 LabelType* true_label,
1195 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001196 if (cond->IsFPConditionTrueIfNaN()) {
1197 __ j(kUnordered, true_label);
1198 } else if (cond->IsFPConditionFalseIfNaN()) {
1199 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001200 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001201 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001202}
1203
Mark Mendell152408f2015-12-31 12:28:50 -05001204template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001205void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001206 LabelType* true_label,
1207 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001208 LocationSummary* locations = cond->GetLocations();
1209 Location left = locations->InAt(0);
1210 Location right = locations->InAt(1);
1211 IfCondition if_cond = cond->GetCondition();
1212
Mark Mendellc4701932015-04-10 13:18:51 -04001213 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001214 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001215 IfCondition true_high_cond = if_cond;
1216 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001217 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001218
1219 // Set the conditions for the test, remembering that == needs to be
1220 // decided using the low words.
1221 switch (if_cond) {
1222 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001223 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001224 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001225 break;
1226 case kCondLT:
1227 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001228 break;
1229 case kCondLE:
1230 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001231 break;
1232 case kCondGT:
1233 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001234 break;
1235 case kCondGE:
1236 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001237 break;
Aart Bike9f37602015-10-09 11:15:55 -07001238 case kCondB:
1239 false_high_cond = kCondA;
1240 break;
1241 case kCondBE:
1242 true_high_cond = kCondB;
1243 break;
1244 case kCondA:
1245 false_high_cond = kCondB;
1246 break;
1247 case kCondAE:
1248 true_high_cond = kCondA;
1249 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001250 }
1251
1252 if (right.IsConstant()) {
1253 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001254 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001255 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001256
Aart Bika19616e2016-02-01 18:57:58 -08001257 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001258 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001259 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001260 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001261 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001262 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001263 __ j(X86Condition(true_high_cond), true_label);
1264 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001265 }
1266 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001267 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001268 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001269 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001270 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001271
1272 __ cmpl(left_high, right_high);
1273 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001274 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001275 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001276 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001277 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001278 __ j(X86Condition(true_high_cond), true_label);
1279 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001280 }
1281 // Must be equal high, so compare the lows.
1282 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001283 } else {
1284 DCHECK(right.IsDoubleStackSlot());
1285 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1286 if (if_cond == kCondNE) {
1287 __ j(X86Condition(true_high_cond), true_label);
1288 } else if (if_cond == kCondEQ) {
1289 __ j(X86Condition(false_high_cond), false_label);
1290 } else {
1291 __ j(X86Condition(true_high_cond), true_label);
1292 __ j(X86Condition(false_high_cond), false_label);
1293 }
1294 // Must be equal high, so compare the lows.
1295 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001296 }
1297 // The last comparison might be unsigned.
1298 __ j(final_condition, true_label);
1299}
1300
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001301void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1302 Location rhs,
1303 HInstruction* insn,
1304 bool is_double) {
1305 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1306 if (is_double) {
1307 if (rhs.IsFpuRegister()) {
1308 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1309 } else if (const_area != nullptr) {
1310 DCHECK(const_area->IsEmittedAtUseSite());
1311 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1312 codegen_->LiteralDoubleAddress(
1313 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1314 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1315 } else {
1316 DCHECK(rhs.IsDoubleStackSlot());
1317 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1318 }
1319 } else {
1320 if (rhs.IsFpuRegister()) {
1321 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1322 } else if (const_area != nullptr) {
1323 DCHECK(const_area->IsEmittedAtUseSite());
1324 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1325 codegen_->LiteralFloatAddress(
1326 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1327 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1328 } else {
1329 DCHECK(rhs.IsStackSlot());
1330 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1331 }
1332 }
1333}
1334
Mark Mendell152408f2015-12-31 12:28:50 -05001335template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001336void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001337 LabelType* true_target_in,
1338 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001339 // Generated branching requires both targets to be explicit. If either of the
1340 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001341 LabelType fallthrough_target;
1342 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1343 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001344
Mark Mendellc4701932015-04-10 13:18:51 -04001345 LocationSummary* locations = condition->GetLocations();
1346 Location left = locations->InAt(0);
1347 Location right = locations->InAt(1);
1348
Mark Mendellc4701932015-04-10 13:18:51 -04001349 Primitive::Type type = condition->InputAt(0)->GetType();
1350 switch (type) {
1351 case Primitive::kPrimLong:
1352 GenerateLongComparesAndJumps(condition, true_target, false_target);
1353 break;
1354 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001355 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001356 GenerateFPJumps(condition, true_target, false_target);
1357 break;
1358 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001359 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001360 GenerateFPJumps(condition, true_target, false_target);
1361 break;
1362 default:
1363 LOG(FATAL) << "Unexpected compare type " << type;
1364 }
1365
David Brazdil0debae72015-11-12 18:37:00 +00001366 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001367 __ jmp(false_target);
1368 }
David Brazdil0debae72015-11-12 18:37:00 +00001369
1370 if (fallthrough_target.IsLinked()) {
1371 __ Bind(&fallthrough_target);
1372 }
Mark Mendellc4701932015-04-10 13:18:51 -04001373}
1374
David Brazdil0debae72015-11-12 18:37:00 +00001375static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1376 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1377 // are set only strictly before `branch`. We can't use the eflags on long/FP
1378 // conditions if they are materialized due to the complex branching.
1379 return cond->IsCondition() &&
1380 cond->GetNext() == branch &&
1381 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1382 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1383}
1384
Mark Mendell152408f2015-12-31 12:28:50 -05001385template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001386void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001387 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001388 LabelType* true_target,
1389 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001390 HInstruction* cond = instruction->InputAt(condition_input_index);
1391
1392 if (true_target == nullptr && false_target == nullptr) {
1393 // Nothing to do. The code always falls through.
1394 return;
1395 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001396 // Constant condition, statically compared against "true" (integer value 1).
1397 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001398 if (true_target != nullptr) {
1399 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001400 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001401 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001402 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001403 if (false_target != nullptr) {
1404 __ jmp(false_target);
1405 }
1406 }
1407 return;
1408 }
1409
1410 // The following code generates these patterns:
1411 // (1) true_target == nullptr && false_target != nullptr
1412 // - opposite condition true => branch to false_target
1413 // (2) true_target != nullptr && false_target == nullptr
1414 // - condition true => branch to true_target
1415 // (3) true_target != nullptr && false_target != nullptr
1416 // - condition true => branch to true_target
1417 // - branch to false_target
1418 if (IsBooleanValueOrMaterializedCondition(cond)) {
1419 if (AreEflagsSetFrom(cond, instruction)) {
1420 if (true_target == nullptr) {
1421 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1422 } else {
1423 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1424 }
1425 } else {
1426 // Materialized condition, compare against 0.
1427 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1428 if (lhs.IsRegister()) {
1429 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1430 } else {
1431 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1432 }
1433 if (true_target == nullptr) {
1434 __ j(kEqual, false_target);
1435 } else {
1436 __ j(kNotEqual, true_target);
1437 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001438 }
1439 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001440 // Condition has not been materialized, use its inputs as the comparison and
1441 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001442 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001443
1444 // If this is a long or FP comparison that has been folded into
1445 // the HCondition, generate the comparison directly.
1446 Primitive::Type type = condition->InputAt(0)->GetType();
1447 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1448 GenerateCompareTestAndBranch(condition, true_target, false_target);
1449 return;
1450 }
1451
1452 Location lhs = condition->GetLocations()->InAt(0);
1453 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001454 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001455 if (rhs.IsRegister()) {
1456 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1457 } else if (rhs.IsConstant()) {
1458 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001459 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001460 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001461 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1462 }
1463 if (true_target == nullptr) {
1464 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1465 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001466 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001467 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001468 }
David Brazdil0debae72015-11-12 18:37:00 +00001469
1470 // If neither branch falls through (case 3), the conditional branch to `true_target`
1471 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1472 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001473 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001474 }
1475}
1476
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001477void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001478 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1479 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001480 locations->SetInAt(0, Location::Any());
1481 }
1482}
1483
1484void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001485 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1486 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1487 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1488 nullptr : codegen_->GetLabelOf(true_successor);
1489 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1490 nullptr : codegen_->GetLabelOf(false_successor);
1491 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001492}
1493
1494void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1495 LocationSummary* locations = new (GetGraph()->GetArena())
1496 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001497 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001498 locations->SetInAt(0, Location::Any());
1499 }
1500}
1501
1502void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001503 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001504 GenerateTestAndBranch<Label>(deoptimize,
1505 /* condition_input_index */ 0,
1506 slow_path->GetEntryLabel(),
1507 /* false_target */ nullptr);
1508}
1509
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001510static bool SelectCanUseCMOV(HSelect* select) {
1511 // There are no conditional move instructions for XMMs.
1512 if (Primitive::IsFloatingPointType(select->GetType())) {
1513 return false;
1514 }
1515
1516 // A FP condition doesn't generate the single CC that we need.
1517 // In 32 bit mode, a long condition doesn't generate a single CC either.
1518 HInstruction* condition = select->GetCondition();
1519 if (condition->IsCondition()) {
1520 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1521 if (compare_type == Primitive::kPrimLong ||
1522 Primitive::IsFloatingPointType(compare_type)) {
1523 return false;
1524 }
1525 }
1526
1527 // We can generate a CMOV for this Select.
1528 return true;
1529}
1530
David Brazdil74eb1b22015-12-14 11:44:01 +00001531void LocationsBuilderX86::VisitSelect(HSelect* select) {
1532 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001533 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001534 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001535 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001536 } else {
1537 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001538 if (SelectCanUseCMOV(select)) {
1539 if (select->InputAt(1)->IsConstant()) {
1540 // Cmov can't handle a constant value.
1541 locations->SetInAt(1, Location::RequiresRegister());
1542 } else {
1543 locations->SetInAt(1, Location::Any());
1544 }
1545 } else {
1546 locations->SetInAt(1, Location::Any());
1547 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001548 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001549 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1550 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001551 }
1552 locations->SetOut(Location::SameAsFirstInput());
1553}
1554
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001555void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1556 Register lhs_reg = lhs.AsRegister<Register>();
1557 if (rhs.IsConstant()) {
1558 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1559 codegen_->Compare32BitValue(lhs_reg, value);
1560 } else if (rhs.IsStackSlot()) {
1561 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1562 } else {
1563 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1564 }
1565}
1566
David Brazdil74eb1b22015-12-14 11:44:01 +00001567void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1568 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001569 DCHECK(locations->InAt(0).Equals(locations->Out()));
1570 if (SelectCanUseCMOV(select)) {
1571 // If both the condition and the source types are integer, we can generate
1572 // a CMOV to implement Select.
1573
1574 HInstruction* select_condition = select->GetCondition();
1575 Condition cond = kNotEqual;
1576
1577 // Figure out how to test the 'condition'.
1578 if (select_condition->IsCondition()) {
1579 HCondition* condition = select_condition->AsCondition();
1580 if (!condition->IsEmittedAtUseSite()) {
1581 // This was a previously materialized condition.
1582 // Can we use the existing condition code?
1583 if (AreEflagsSetFrom(condition, select)) {
1584 // Materialization was the previous instruction. Condition codes are right.
1585 cond = X86Condition(condition->GetCondition());
1586 } else {
1587 // No, we have to recreate the condition code.
1588 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1589 __ testl(cond_reg, cond_reg);
1590 }
1591 } else {
1592 // We can't handle FP or long here.
1593 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1594 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1595 LocationSummary* cond_locations = condition->GetLocations();
1596 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1597 cond = X86Condition(condition->GetCondition());
1598 }
1599 } else {
1600 // Must be a boolean condition, which needs to be compared to 0.
1601 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1602 __ testl(cond_reg, cond_reg);
1603 }
1604
1605 // If the condition is true, overwrite the output, which already contains false.
1606 Location false_loc = locations->InAt(0);
1607 Location true_loc = locations->InAt(1);
1608 if (select->GetType() == Primitive::kPrimLong) {
1609 // 64 bit conditional move.
1610 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1611 Register false_low = false_loc.AsRegisterPairLow<Register>();
1612 if (true_loc.IsRegisterPair()) {
1613 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1614 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1615 } else {
1616 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1617 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1618 }
1619 } else {
1620 // 32 bit conditional move.
1621 Register false_reg = false_loc.AsRegister<Register>();
1622 if (true_loc.IsRegister()) {
1623 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1624 } else {
1625 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1626 }
1627 }
1628 } else {
1629 NearLabel false_target;
1630 GenerateTestAndBranch<NearLabel>(
1631 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1632 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1633 __ Bind(&false_target);
1634 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001635}
1636
David Srbecky0cf44932015-12-09 14:09:59 +00001637void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1638 new (GetGraph()->GetArena()) LocationSummary(info);
1639}
1640
David Srbeckyd28f4a02016-03-14 17:14:24 +00001641void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1642 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001643}
1644
1645void CodeGeneratorX86::GenerateNop() {
1646 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001647}
1648
David Brazdil60328912016-04-04 17:47:42 +00001649void LocationsBuilderX86::VisitLocal(HLocal* local) {
1650 local->SetLocations(nullptr);
1651}
1652
1653void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1654 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1655}
1656
1657void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
1658 local->SetLocations(nullptr);
1659}
1660
1661void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
1662 // Nothing to do, this is driven by the code generator.
1663}
1664
1665void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
1666 LocationSummary* locations =
1667 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
1668 switch (store->InputAt(1)->GetType()) {
1669 case Primitive::kPrimBoolean:
1670 case Primitive::kPrimByte:
1671 case Primitive::kPrimChar:
1672 case Primitive::kPrimShort:
1673 case Primitive::kPrimInt:
1674 case Primitive::kPrimNot:
1675 case Primitive::kPrimFloat:
1676 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1677 break;
1678
1679 case Primitive::kPrimLong:
1680 case Primitive::kPrimDouble:
1681 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1682 break;
1683
1684 default:
1685 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
1686 }
1687}
1688
1689void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
1690}
1691
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001692void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001693 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001694 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001695 // Handle the long/FP comparisons made in instruction simplification.
1696 switch (cond->InputAt(0)->GetType()) {
1697 case Primitive::kPrimLong: {
1698 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001699 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001700 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001701 locations->SetOut(Location::RequiresRegister());
1702 }
1703 break;
1704 }
1705 case Primitive::kPrimFloat:
1706 case Primitive::kPrimDouble: {
1707 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001708 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1709 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1710 } else if (cond->InputAt(1)->IsConstant()) {
1711 locations->SetInAt(1, Location::RequiresFpuRegister());
1712 } else {
1713 locations->SetInAt(1, Location::Any());
1714 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001715 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001716 locations->SetOut(Location::RequiresRegister());
1717 }
1718 break;
1719 }
1720 default:
1721 locations->SetInAt(0, Location::RequiresRegister());
1722 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001723 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001724 // We need a byte register.
1725 locations->SetOut(Location::RegisterLocation(ECX));
1726 }
1727 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001728 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001729}
1730
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001731void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001732 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001733 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001734 }
Mark Mendellc4701932015-04-10 13:18:51 -04001735
1736 LocationSummary* locations = cond->GetLocations();
1737 Location lhs = locations->InAt(0);
1738 Location rhs = locations->InAt(1);
1739 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001740 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001741
1742 switch (cond->InputAt(0)->GetType()) {
1743 default: {
1744 // Integer case.
1745
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001746 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001747 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001748 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001749 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001750 return;
1751 }
1752 case Primitive::kPrimLong:
1753 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1754 break;
1755 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001756 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001757 GenerateFPJumps(cond, &true_label, &false_label);
1758 break;
1759 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001760 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001761 GenerateFPJumps(cond, &true_label, &false_label);
1762 break;
1763 }
1764
1765 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001766 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001767
Roland Levillain4fa13f62015-07-06 18:11:54 +01001768 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001769 __ Bind(&false_label);
1770 __ xorl(reg, reg);
1771 __ jmp(&done_label);
1772
Roland Levillain4fa13f62015-07-06 18:11:54 +01001773 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001774 __ Bind(&true_label);
1775 __ movl(reg, Immediate(1));
1776 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001777}
1778
1779void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001781}
1782
1783void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001784 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001785}
1786
1787void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001788 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001789}
1790
1791void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001793}
1794
1795void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001796 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001797}
1798
1799void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001801}
1802
1803void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001804 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001805}
1806
1807void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001808 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001809}
1810
1811void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001812 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001813}
1814
1815void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001816 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001817}
1818
1819void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001821}
1822
1823void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001825}
1826
Aart Bike9f37602015-10-09 11:15:55 -07001827void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001829}
1830
1831void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001833}
1834
1835void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001836 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001837}
1838
1839void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001840 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001841}
1842
1843void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001844 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001845}
1846
1847void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001848 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001849}
1850
1851void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001852 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001853}
1854
1855void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001856 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001857}
1858
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001859void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001860 LocationSummary* locations =
1861 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001862 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001863}
1864
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001865void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001866 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001867}
1868
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001869void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1870 LocationSummary* locations =
1871 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1872 locations->SetOut(Location::ConstantLocation(constant));
1873}
1874
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001875void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001876 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001877}
1878
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001879void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001880 LocationSummary* locations =
1881 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001882 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001883}
1884
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001885void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001886 // Will be generated at use site.
1887}
1888
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001889void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1890 LocationSummary* locations =
1891 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1892 locations->SetOut(Location::ConstantLocation(constant));
1893}
1894
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001895void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001896 // Will be generated at use site.
1897}
1898
1899void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1900 LocationSummary* locations =
1901 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1902 locations->SetOut(Location::ConstantLocation(constant));
1903}
1904
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001905void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001906 // Will be generated at use site.
1907}
1908
Calin Juravle27df7582015-04-17 19:12:31 +01001909void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1910 memory_barrier->SetLocations(nullptr);
1911}
1912
1913void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001914 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001915}
1916
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001917void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001918 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001919}
1920
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001921void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001922 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001923}
1924
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001925void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001926 LocationSummary* locations =
1927 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001928 switch (ret->InputAt(0)->GetType()) {
1929 case Primitive::kPrimBoolean:
1930 case Primitive::kPrimByte:
1931 case Primitive::kPrimChar:
1932 case Primitive::kPrimShort:
1933 case Primitive::kPrimInt:
1934 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001935 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001936 break;
1937
1938 case Primitive::kPrimLong:
1939 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001940 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001941 break;
1942
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001943 case Primitive::kPrimFloat:
1944 case Primitive::kPrimDouble:
1945 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001946 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001947 break;
1948
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001949 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001950 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001951 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001952}
1953
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001954void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001955 if (kIsDebugBuild) {
1956 switch (ret->InputAt(0)->GetType()) {
1957 case Primitive::kPrimBoolean:
1958 case Primitive::kPrimByte:
1959 case Primitive::kPrimChar:
1960 case Primitive::kPrimShort:
1961 case Primitive::kPrimInt:
1962 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001963 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001964 break;
1965
1966 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001967 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1968 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001969 break;
1970
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001971 case Primitive::kPrimFloat:
1972 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001973 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001974 break;
1975
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001976 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001977 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001978 }
1979 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001980 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001981}
1982
Calin Juravle175dc732015-08-25 15:42:32 +01001983void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1984 // The trampoline uses the same calling convention as dex calling conventions,
1985 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1986 // the method_idx.
1987 HandleInvoke(invoke);
1988}
1989
1990void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1991 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1992}
1993
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001994void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001995 // Explicit clinit checks triggered by static invokes must have been pruned by
1996 // art::PrepareForRegisterAllocation.
1997 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001998
Mark Mendellfb8d2792015-03-31 22:16:59 -04001999 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002000 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002001 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002002 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002003 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002004 return;
2005 }
2006
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002007 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002008
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002009 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2010 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002011 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002012 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002013}
2014
Mark Mendell09ed1a32015-03-25 08:30:06 -04002015static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2016 if (invoke->GetLocations()->Intrinsified()) {
2017 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2018 intrinsic.Dispatch(invoke);
2019 return true;
2020 }
2021 return false;
2022}
2023
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002024void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002025 // Explicit clinit checks triggered by static invokes must have been pruned by
2026 // art::PrepareForRegisterAllocation.
2027 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002028
Mark Mendell09ed1a32015-03-25 08:30:06 -04002029 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2030 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002031 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002032
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002033 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002034 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002035 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002036 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002037}
2038
2039void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002040 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2041 if (intrinsic.TryDispatch(invoke)) {
2042 return;
2043 }
2044
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002045 HandleInvoke(invoke);
2046}
2047
2048void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002049 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002050 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002051}
2052
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002053void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002054 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2055 return;
2056 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002057
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002058 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002059 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002060 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002061}
2062
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002063void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002064 // This call to HandleInvoke allocates a temporary (core) register
2065 // which is also used to transfer the hidden argument from FP to
2066 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002067 HandleInvoke(invoke);
2068 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002069 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002070}
2071
2072void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2073 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002074 LocationSummary* locations = invoke->GetLocations();
2075 Register temp = locations->GetTemp(0).AsRegister<Register>();
2076 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002077 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2078 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002079 Location receiver = locations->InAt(0);
2080 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2081
Roland Levillain0d5a2812015-11-13 10:07:31 +00002082 // Set the hidden argument. This is safe to do this here, as XMM7
2083 // won't be modified thereafter, before the `call` instruction.
2084 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002085 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002086 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002087
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002088 if (receiver.IsStackSlot()) {
2089 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002090 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002091 __ movl(temp, Address(temp, class_offset));
2092 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002093 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002094 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002095 }
Roland Levillain4d027112015-07-01 15:41:14 +01002096 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002097 // Instead of simply (possibly) unpoisoning `temp` here, we should
2098 // emit a read barrier for the previous class reference load.
2099 // However this is not required in practice, as this is an
2100 // intermediate/temporary reference and because the current
2101 // concurrent copying collector keeps the from-space memory
2102 // intact/accessible until the end of the marking phase (the
2103 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002104 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002105 // temp = temp->GetImtEntryAt(method_offset);
2106 __ movl(temp, Address(temp, method_offset));
2107 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002108 __ call(Address(temp,
2109 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002110
2111 DCHECK(!codegen_->IsLeafMethod());
2112 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2113}
2114
Roland Levillain88cb1752014-10-20 16:36:47 +01002115void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2116 LocationSummary* locations =
2117 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2118 switch (neg->GetResultType()) {
2119 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002120 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002121 locations->SetInAt(0, Location::RequiresRegister());
2122 locations->SetOut(Location::SameAsFirstInput());
2123 break;
2124
Roland Levillain88cb1752014-10-20 16:36:47 +01002125 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002126 locations->SetInAt(0, Location::RequiresFpuRegister());
2127 locations->SetOut(Location::SameAsFirstInput());
2128 locations->AddTemp(Location::RequiresRegister());
2129 locations->AddTemp(Location::RequiresFpuRegister());
2130 break;
2131
Roland Levillain88cb1752014-10-20 16:36:47 +01002132 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002133 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002134 locations->SetOut(Location::SameAsFirstInput());
2135 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002136 break;
2137
2138 default:
2139 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2140 }
2141}
2142
2143void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2144 LocationSummary* locations = neg->GetLocations();
2145 Location out = locations->Out();
2146 Location in = locations->InAt(0);
2147 switch (neg->GetResultType()) {
2148 case Primitive::kPrimInt:
2149 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002150 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002151 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002152 break;
2153
2154 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002155 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002156 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002157 __ negl(out.AsRegisterPairLow<Register>());
2158 // Negation is similar to subtraction from zero. The least
2159 // significant byte triggers a borrow when it is different from
2160 // zero; to take it into account, add 1 to the most significant
2161 // byte if the carry flag (CF) is set to 1 after the first NEGL
2162 // operation.
2163 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2164 __ negl(out.AsRegisterPairHigh<Register>());
2165 break;
2166
Roland Levillain5368c212014-11-27 15:03:41 +00002167 case Primitive::kPrimFloat: {
2168 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002169 Register constant = locations->GetTemp(0).AsRegister<Register>();
2170 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002171 // Implement float negation with an exclusive or with value
2172 // 0x80000000 (mask for bit 31, representing the sign of a
2173 // single-precision floating-point number).
2174 __ movl(constant, Immediate(INT32_C(0x80000000)));
2175 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002176 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002177 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002178 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002179
Roland Levillain5368c212014-11-27 15:03:41 +00002180 case Primitive::kPrimDouble: {
2181 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002182 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002183 // Implement double negation with an exclusive or with value
2184 // 0x8000000000000000 (mask for bit 63, representing the sign of
2185 // a double-precision floating-point number).
2186 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002187 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002188 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002189 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002190
2191 default:
2192 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2193 }
2194}
2195
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002196void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2197 LocationSummary* locations =
2198 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2199 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2200 locations->SetInAt(0, Location::RequiresFpuRegister());
2201 locations->SetInAt(1, Location::RequiresRegister());
2202 locations->SetOut(Location::SameAsFirstInput());
2203 locations->AddTemp(Location::RequiresFpuRegister());
2204}
2205
2206void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2207 LocationSummary* locations = neg->GetLocations();
2208 Location out = locations->Out();
2209 DCHECK(locations->InAt(0).Equals(out));
2210
2211 Register constant_area = locations->InAt(1).AsRegister<Register>();
2212 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2213 if (neg->GetType() == Primitive::kPrimFloat) {
2214 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2215 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2216 } else {
2217 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2218 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2219 }
2220}
2221
Roland Levillaindff1f282014-11-05 14:15:05 +00002222void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002223 Primitive::Type result_type = conversion->GetResultType();
2224 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002225 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002226
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002227 // The float-to-long and double-to-long type conversions rely on a
2228 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002229 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002230 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2231 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002232 ? LocationSummary::kCall
2233 : LocationSummary::kNoCall;
2234 LocationSummary* locations =
2235 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2236
David Brazdilb2bd1c52015-03-25 11:17:37 +00002237 // The Java language does not allow treating boolean as an integral type but
2238 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002239
Roland Levillaindff1f282014-11-05 14:15:05 +00002240 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002241 case Primitive::kPrimByte:
2242 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002243 case Primitive::kPrimLong: {
2244 // Type conversion from long to byte is a result of code transformations.
2245 HInstruction* input = conversion->InputAt(0);
2246 Location input_location = input->IsConstant()
2247 ? Location::ConstantLocation(input->AsConstant())
2248 : Location::RegisterPairLocation(EAX, EDX);
2249 locations->SetInAt(0, input_location);
2250 // Make the output overlap to please the register allocator. This greatly simplifies
2251 // the validation of the linear scan implementation
2252 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2253 break;
2254 }
David Brazdil46e2a392015-03-16 17:31:52 +00002255 case Primitive::kPrimBoolean:
2256 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002257 case Primitive::kPrimShort:
2258 case Primitive::kPrimInt:
2259 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002260 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002261 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2262 // Make the output overlap to please the register allocator. This greatly simplifies
2263 // the validation of the linear scan implementation
2264 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002265 break;
2266
2267 default:
2268 LOG(FATAL) << "Unexpected type conversion from " << input_type
2269 << " to " << result_type;
2270 }
2271 break;
2272
Roland Levillain01a8d712014-11-14 16:27:39 +00002273 case Primitive::kPrimShort:
2274 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002275 case Primitive::kPrimLong:
2276 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002277 case Primitive::kPrimBoolean:
2278 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002279 case Primitive::kPrimByte:
2280 case Primitive::kPrimInt:
2281 case Primitive::kPrimChar:
2282 // Processing a Dex `int-to-short' instruction.
2283 locations->SetInAt(0, Location::Any());
2284 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2285 break;
2286
2287 default:
2288 LOG(FATAL) << "Unexpected type conversion from " << input_type
2289 << " to " << result_type;
2290 }
2291 break;
2292
Roland Levillain946e1432014-11-11 17:35:19 +00002293 case Primitive::kPrimInt:
2294 switch (input_type) {
2295 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002296 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002297 locations->SetInAt(0, Location::Any());
2298 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2299 break;
2300
2301 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002302 // Processing a Dex `float-to-int' instruction.
2303 locations->SetInAt(0, Location::RequiresFpuRegister());
2304 locations->SetOut(Location::RequiresRegister());
2305 locations->AddTemp(Location::RequiresFpuRegister());
2306 break;
2307
Roland Levillain946e1432014-11-11 17:35:19 +00002308 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002309 // Processing a Dex `double-to-int' instruction.
2310 locations->SetInAt(0, Location::RequiresFpuRegister());
2311 locations->SetOut(Location::RequiresRegister());
2312 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002313 break;
2314
2315 default:
2316 LOG(FATAL) << "Unexpected type conversion from " << input_type
2317 << " to " << result_type;
2318 }
2319 break;
2320
Roland Levillaindff1f282014-11-05 14:15:05 +00002321 case Primitive::kPrimLong:
2322 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002323 case Primitive::kPrimBoolean:
2324 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002325 case Primitive::kPrimByte:
2326 case Primitive::kPrimShort:
2327 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002328 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002329 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002330 locations->SetInAt(0, Location::RegisterLocation(EAX));
2331 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2332 break;
2333
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002334 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002335 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002336 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002337 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002338 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2339 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2340
Vladimir Marko949c91f2015-01-27 10:48:44 +00002341 // The runtime helper puts the result in EAX, EDX.
2342 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002343 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002344 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002345
2346 default:
2347 LOG(FATAL) << "Unexpected type conversion from " << input_type
2348 << " to " << result_type;
2349 }
2350 break;
2351
Roland Levillain981e4542014-11-14 11:47:14 +00002352 case Primitive::kPrimChar:
2353 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002354 case Primitive::kPrimLong:
2355 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002356 case Primitive::kPrimBoolean:
2357 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002358 case Primitive::kPrimByte:
2359 case Primitive::kPrimShort:
2360 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002361 // Processing a Dex `int-to-char' instruction.
2362 locations->SetInAt(0, Location::Any());
2363 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2364 break;
2365
2366 default:
2367 LOG(FATAL) << "Unexpected type conversion from " << input_type
2368 << " to " << result_type;
2369 }
2370 break;
2371
Roland Levillaindff1f282014-11-05 14:15:05 +00002372 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002373 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002374 case Primitive::kPrimBoolean:
2375 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002376 case Primitive::kPrimByte:
2377 case Primitive::kPrimShort:
2378 case Primitive::kPrimInt:
2379 case Primitive::kPrimChar:
2380 // Processing a Dex `int-to-float' instruction.
2381 locations->SetInAt(0, Location::RequiresRegister());
2382 locations->SetOut(Location::RequiresFpuRegister());
2383 break;
2384
2385 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002386 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002387 locations->SetInAt(0, Location::Any());
2388 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002389 break;
2390
Roland Levillaincff13742014-11-17 14:32:17 +00002391 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002392 // Processing a Dex `double-to-float' instruction.
2393 locations->SetInAt(0, Location::RequiresFpuRegister());
2394 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002395 break;
2396
2397 default:
2398 LOG(FATAL) << "Unexpected type conversion from " << input_type
2399 << " to " << result_type;
2400 };
2401 break;
2402
Roland Levillaindff1f282014-11-05 14:15:05 +00002403 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002404 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002405 case Primitive::kPrimBoolean:
2406 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002407 case Primitive::kPrimByte:
2408 case Primitive::kPrimShort:
2409 case Primitive::kPrimInt:
2410 case Primitive::kPrimChar:
2411 // Processing a Dex `int-to-double' instruction.
2412 locations->SetInAt(0, Location::RequiresRegister());
2413 locations->SetOut(Location::RequiresFpuRegister());
2414 break;
2415
2416 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002417 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002418 locations->SetInAt(0, Location::Any());
2419 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002420 break;
2421
Roland Levillaincff13742014-11-17 14:32:17 +00002422 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002423 // Processing a Dex `float-to-double' instruction.
2424 locations->SetInAt(0, Location::RequiresFpuRegister());
2425 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002426 break;
2427
2428 default:
2429 LOG(FATAL) << "Unexpected type conversion from " << input_type
2430 << " to " << result_type;
2431 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002432 break;
2433
2434 default:
2435 LOG(FATAL) << "Unexpected type conversion from " << input_type
2436 << " to " << result_type;
2437 }
2438}
2439
2440void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2441 LocationSummary* locations = conversion->GetLocations();
2442 Location out = locations->Out();
2443 Location in = locations->InAt(0);
2444 Primitive::Type result_type = conversion->GetResultType();
2445 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002446 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002447 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002448 case Primitive::kPrimByte:
2449 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002450 case Primitive::kPrimLong:
2451 // Type conversion from long to byte is a result of code transformations.
2452 if (in.IsRegisterPair()) {
2453 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2454 } else {
2455 DCHECK(in.GetConstant()->IsLongConstant());
2456 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2457 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2458 }
2459 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002460 case Primitive::kPrimBoolean:
2461 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002462 case Primitive::kPrimShort:
2463 case Primitive::kPrimInt:
2464 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002465 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002466 if (in.IsRegister()) {
2467 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002468 } else {
2469 DCHECK(in.GetConstant()->IsIntConstant());
2470 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2471 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2472 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002473 break;
2474
2475 default:
2476 LOG(FATAL) << "Unexpected type conversion from " << input_type
2477 << " to " << result_type;
2478 }
2479 break;
2480
Roland Levillain01a8d712014-11-14 16:27:39 +00002481 case Primitive::kPrimShort:
2482 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002483 case Primitive::kPrimLong:
2484 // Type conversion from long to short is a result of code transformations.
2485 if (in.IsRegisterPair()) {
2486 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2487 } else if (in.IsDoubleStackSlot()) {
2488 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2489 } else {
2490 DCHECK(in.GetConstant()->IsLongConstant());
2491 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2492 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2493 }
2494 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002495 case Primitive::kPrimBoolean:
2496 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002497 case Primitive::kPrimByte:
2498 case Primitive::kPrimInt:
2499 case Primitive::kPrimChar:
2500 // Processing a Dex `int-to-short' instruction.
2501 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002502 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002503 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002504 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002505 } else {
2506 DCHECK(in.GetConstant()->IsIntConstant());
2507 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002508 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002509 }
2510 break;
2511
2512 default:
2513 LOG(FATAL) << "Unexpected type conversion from " << input_type
2514 << " to " << result_type;
2515 }
2516 break;
2517
Roland Levillain946e1432014-11-11 17:35:19 +00002518 case Primitive::kPrimInt:
2519 switch (input_type) {
2520 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002521 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002522 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002523 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002524 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002525 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002526 } else {
2527 DCHECK(in.IsConstant());
2528 DCHECK(in.GetConstant()->IsLongConstant());
2529 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002530 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002531 }
2532 break;
2533
Roland Levillain3f8f9362014-12-02 17:45:01 +00002534 case Primitive::kPrimFloat: {
2535 // Processing a Dex `float-to-int' instruction.
2536 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2537 Register output = out.AsRegister<Register>();
2538 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002539 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002540
2541 __ movl(output, Immediate(kPrimIntMax));
2542 // temp = int-to-float(output)
2543 __ cvtsi2ss(temp, output);
2544 // if input >= temp goto done
2545 __ comiss(input, temp);
2546 __ j(kAboveEqual, &done);
2547 // if input == NaN goto nan
2548 __ j(kUnordered, &nan);
2549 // output = float-to-int-truncate(input)
2550 __ cvttss2si(output, input);
2551 __ jmp(&done);
2552 __ Bind(&nan);
2553 // output = 0
2554 __ xorl(output, output);
2555 __ Bind(&done);
2556 break;
2557 }
2558
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002559 case Primitive::kPrimDouble: {
2560 // Processing a Dex `double-to-int' instruction.
2561 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2562 Register output = out.AsRegister<Register>();
2563 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002564 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002565
2566 __ movl(output, Immediate(kPrimIntMax));
2567 // temp = int-to-double(output)
2568 __ cvtsi2sd(temp, output);
2569 // if input >= temp goto done
2570 __ comisd(input, temp);
2571 __ j(kAboveEqual, &done);
2572 // if input == NaN goto nan
2573 __ j(kUnordered, &nan);
2574 // output = double-to-int-truncate(input)
2575 __ cvttsd2si(output, input);
2576 __ jmp(&done);
2577 __ Bind(&nan);
2578 // output = 0
2579 __ xorl(output, output);
2580 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002581 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002582 }
Roland Levillain946e1432014-11-11 17:35:19 +00002583
2584 default:
2585 LOG(FATAL) << "Unexpected type conversion from " << input_type
2586 << " to " << result_type;
2587 }
2588 break;
2589
Roland Levillaindff1f282014-11-05 14:15:05 +00002590 case Primitive::kPrimLong:
2591 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002592 case Primitive::kPrimBoolean:
2593 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002594 case Primitive::kPrimByte:
2595 case Primitive::kPrimShort:
2596 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002597 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002598 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002599 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2600 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002601 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002602 __ cdq();
2603 break;
2604
2605 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002606 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002607 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2608 conversion,
2609 conversion->GetDexPc(),
2610 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002611 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002612 break;
2613
Roland Levillaindff1f282014-11-05 14:15:05 +00002614 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002615 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002616 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2617 conversion,
2618 conversion->GetDexPc(),
2619 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002620 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002621 break;
2622
2623 default:
2624 LOG(FATAL) << "Unexpected type conversion from " << input_type
2625 << " to " << result_type;
2626 }
2627 break;
2628
Roland Levillain981e4542014-11-14 11:47:14 +00002629 case Primitive::kPrimChar:
2630 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002631 case Primitive::kPrimLong:
2632 // Type conversion from long to short is a result of code transformations.
2633 if (in.IsRegisterPair()) {
2634 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2635 } else if (in.IsDoubleStackSlot()) {
2636 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2637 } else {
2638 DCHECK(in.GetConstant()->IsLongConstant());
2639 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2640 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2641 }
2642 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002643 case Primitive::kPrimBoolean:
2644 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002645 case Primitive::kPrimByte:
2646 case Primitive::kPrimShort:
2647 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002648 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2649 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002650 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002651 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002652 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002653 } else {
2654 DCHECK(in.GetConstant()->IsIntConstant());
2655 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002656 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002657 }
2658 break;
2659
2660 default:
2661 LOG(FATAL) << "Unexpected type conversion from " << input_type
2662 << " to " << result_type;
2663 }
2664 break;
2665
Roland Levillaindff1f282014-11-05 14:15:05 +00002666 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002667 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002668 case Primitive::kPrimBoolean:
2669 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002670 case Primitive::kPrimByte:
2671 case Primitive::kPrimShort:
2672 case Primitive::kPrimInt:
2673 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002674 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002675 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002676 break;
2677
Roland Levillain6d0e4832014-11-27 18:31:21 +00002678 case Primitive::kPrimLong: {
2679 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002680 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002681
Roland Levillain232ade02015-04-20 15:14:36 +01002682 // Create stack space for the call to
2683 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2684 // TODO: enhance register allocator to ask for stack temporaries.
2685 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2686 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2687 __ subl(ESP, Immediate(adjustment));
2688 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002689
Roland Levillain232ade02015-04-20 15:14:36 +01002690 // Load the value to the FP stack, using temporaries if needed.
2691 PushOntoFPStack(in, 0, adjustment, false, true);
2692
2693 if (out.IsStackSlot()) {
2694 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2695 } else {
2696 __ fstps(Address(ESP, 0));
2697 Location stack_temp = Location::StackSlot(0);
2698 codegen_->Move32(out, stack_temp);
2699 }
2700
2701 // Remove the temporary stack space we allocated.
2702 if (adjustment != 0) {
2703 __ addl(ESP, Immediate(adjustment));
2704 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002705 break;
2706 }
2707
Roland Levillaincff13742014-11-17 14:32:17 +00002708 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002709 // Processing a Dex `double-to-float' instruction.
2710 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002711 break;
2712
2713 default:
2714 LOG(FATAL) << "Unexpected type conversion from " << input_type
2715 << " to " << result_type;
2716 };
2717 break;
2718
Roland Levillaindff1f282014-11-05 14:15:05 +00002719 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002720 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002721 case Primitive::kPrimBoolean:
2722 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002723 case Primitive::kPrimByte:
2724 case Primitive::kPrimShort:
2725 case Primitive::kPrimInt:
2726 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002727 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002728 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002729 break;
2730
Roland Levillain647b9ed2014-11-27 12:06:00 +00002731 case Primitive::kPrimLong: {
2732 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002733 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002734
Roland Levillain232ade02015-04-20 15:14:36 +01002735 // Create stack space for the call to
2736 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2737 // TODO: enhance register allocator to ask for stack temporaries.
2738 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2739 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2740 __ subl(ESP, Immediate(adjustment));
2741 }
2742
2743 // Load the value to the FP stack, using temporaries if needed.
2744 PushOntoFPStack(in, 0, adjustment, false, true);
2745
2746 if (out.IsDoubleStackSlot()) {
2747 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2748 } else {
2749 __ fstpl(Address(ESP, 0));
2750 Location stack_temp = Location::DoubleStackSlot(0);
2751 codegen_->Move64(out, stack_temp);
2752 }
2753
2754 // Remove the temporary stack space we allocated.
2755 if (adjustment != 0) {
2756 __ addl(ESP, Immediate(adjustment));
2757 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002758 break;
2759 }
2760
Roland Levillaincff13742014-11-17 14:32:17 +00002761 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002762 // Processing a Dex `float-to-double' instruction.
2763 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002764 break;
2765
2766 default:
2767 LOG(FATAL) << "Unexpected type conversion from " << input_type
2768 << " to " << result_type;
2769 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002770 break;
2771
2772 default:
2773 LOG(FATAL) << "Unexpected type conversion from " << input_type
2774 << " to " << result_type;
2775 }
2776}
2777
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002778void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002779 LocationSummary* locations =
2780 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002781 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002782 case Primitive::kPrimInt: {
2783 locations->SetInAt(0, Location::RequiresRegister());
2784 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2785 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2786 break;
2787 }
2788
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002789 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002790 locations->SetInAt(0, Location::RequiresRegister());
2791 locations->SetInAt(1, Location::Any());
2792 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002793 break;
2794 }
2795
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002796 case Primitive::kPrimFloat:
2797 case Primitive::kPrimDouble: {
2798 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002799 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2800 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002801 } else if (add->InputAt(1)->IsConstant()) {
2802 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002803 } else {
2804 locations->SetInAt(1, Location::Any());
2805 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002806 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002807 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002808 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002809
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002810 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002811 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2812 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002813 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002814}
2815
2816void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2817 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002818 Location first = locations->InAt(0);
2819 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002820 Location out = locations->Out();
2821
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002822 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002823 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002824 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002825 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2826 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002827 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2828 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002829 } else {
2830 __ leal(out.AsRegister<Register>(), Address(
2831 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2832 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002833 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002834 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2835 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2836 __ addl(out.AsRegister<Register>(), Immediate(value));
2837 } else {
2838 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2839 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002840 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002841 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002842 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002843 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002844 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002845 }
2846
2847 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002848 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002849 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2850 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002851 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002852 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2853 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002854 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002855 } else {
2856 DCHECK(second.IsConstant()) << second;
2857 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2858 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2859 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002860 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002861 break;
2862 }
2863
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002864 case Primitive::kPrimFloat: {
2865 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002866 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002867 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2868 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002869 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002870 __ addss(first.AsFpuRegister<XmmRegister>(),
2871 codegen_->LiteralFloatAddress(
2872 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2873 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2874 } else {
2875 DCHECK(second.IsStackSlot());
2876 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002877 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002878 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002879 }
2880
2881 case Primitive::kPrimDouble: {
2882 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002883 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002884 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2885 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002886 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002887 __ addsd(first.AsFpuRegister<XmmRegister>(),
2888 codegen_->LiteralDoubleAddress(
2889 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2890 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2891 } else {
2892 DCHECK(second.IsDoubleStackSlot());
2893 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002894 }
2895 break;
2896 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002897
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002898 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002899 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002900 }
2901}
2902
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002903void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002904 LocationSummary* locations =
2905 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002906 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002907 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002908 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002909 locations->SetInAt(0, Location::RequiresRegister());
2910 locations->SetInAt(1, Location::Any());
2911 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002912 break;
2913 }
Calin Juravle11351682014-10-23 15:38:15 +01002914 case Primitive::kPrimFloat:
2915 case Primitive::kPrimDouble: {
2916 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002917 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2918 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002919 } else if (sub->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 }
Calin Juravle11351682014-10-23 15:38:15 +01002924 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002925 break;
Calin Juravle11351682014-10-23 15:38:15 +01002926 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002927
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002928 default:
Calin Juravle11351682014-10-23 15:38:15 +01002929 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002930 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002931}
2932
2933void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2934 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002935 Location first = locations->InAt(0);
2936 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002937 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002938 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002939 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002940 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002941 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002942 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002943 __ subl(first.AsRegister<Register>(),
2944 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002945 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002946 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002947 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002948 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002949 }
2950
2951 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002952 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002953 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2954 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002955 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002956 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002957 __ sbbl(first.AsRegisterPairHigh<Register>(),
2958 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002959 } else {
2960 DCHECK(second.IsConstant()) << second;
2961 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2962 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2963 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002964 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002965 break;
2966 }
2967
Calin Juravle11351682014-10-23 15:38:15 +01002968 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002969 if (second.IsFpuRegister()) {
2970 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2971 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2972 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002973 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002974 __ subss(first.AsFpuRegister<XmmRegister>(),
2975 codegen_->LiteralFloatAddress(
2976 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2977 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2978 } else {
2979 DCHECK(second.IsStackSlot());
2980 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2981 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002982 break;
Calin Juravle11351682014-10-23 15:38:15 +01002983 }
2984
2985 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002986 if (second.IsFpuRegister()) {
2987 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2988 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2989 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002990 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002991 __ subsd(first.AsFpuRegister<XmmRegister>(),
2992 codegen_->LiteralDoubleAddress(
2993 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2994 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2995 } else {
2996 DCHECK(second.IsDoubleStackSlot());
2997 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2998 }
Calin Juravle11351682014-10-23 15:38:15 +01002999 break;
3000 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003001
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003002 default:
Calin Juravle11351682014-10-23 15:38:15 +01003003 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003004 }
3005}
3006
Calin Juravle34bacdf2014-10-07 20:23:36 +01003007void LocationsBuilderX86::VisitMul(HMul* mul) {
3008 LocationSummary* locations =
3009 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3010 switch (mul->GetResultType()) {
3011 case Primitive::kPrimInt:
3012 locations->SetInAt(0, Location::RequiresRegister());
3013 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003014 if (mul->InputAt(1)->IsIntConstant()) {
3015 // Can use 3 operand multiply.
3016 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3017 } else {
3018 locations->SetOut(Location::SameAsFirstInput());
3019 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003020 break;
3021 case Primitive::kPrimLong: {
3022 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003023 locations->SetInAt(1, Location::Any());
3024 locations->SetOut(Location::SameAsFirstInput());
3025 // Needed for imul on 32bits with 64bits output.
3026 locations->AddTemp(Location::RegisterLocation(EAX));
3027 locations->AddTemp(Location::RegisterLocation(EDX));
3028 break;
3029 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003030 case Primitive::kPrimFloat:
3031 case Primitive::kPrimDouble: {
3032 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003033 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3034 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003035 } else if (mul->InputAt(1)->IsConstant()) {
3036 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003037 } else {
3038 locations->SetInAt(1, Location::Any());
3039 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003040 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003041 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003042 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003043
3044 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003045 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003046 }
3047}
3048
3049void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3050 LocationSummary* locations = mul->GetLocations();
3051 Location first = locations->InAt(0);
3052 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003053 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003054
3055 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003056 case Primitive::kPrimInt:
3057 // The constant may have ended up in a register, so test explicitly to avoid
3058 // problems where the output may not be the same as the first operand.
3059 if (mul->InputAt(1)->IsIntConstant()) {
3060 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3061 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3062 } else if (second.IsRegister()) {
3063 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003064 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003065 } else {
3066 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003067 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003068 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003069 }
3070 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003071
3072 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003073 Register in1_hi = first.AsRegisterPairHigh<Register>();
3074 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003075 Register eax = locations->GetTemp(0).AsRegister<Register>();
3076 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003077
3078 DCHECK_EQ(EAX, eax);
3079 DCHECK_EQ(EDX, edx);
3080
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003081 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003082 // output: in1
3083 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3084 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3085 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003086 if (second.IsConstant()) {
3087 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003088
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003089 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3090 int32_t low_value = Low32Bits(value);
3091 int32_t high_value = High32Bits(value);
3092 Immediate low(low_value);
3093 Immediate high(high_value);
3094
3095 __ movl(eax, high);
3096 // eax <- in1.lo * in2.hi
3097 __ imull(eax, in1_lo);
3098 // in1.hi <- in1.hi * in2.lo
3099 __ imull(in1_hi, low);
3100 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3101 __ addl(in1_hi, eax);
3102 // move in2_lo to eax to prepare for double precision
3103 __ movl(eax, low);
3104 // edx:eax <- in1.lo * in2.lo
3105 __ mull(in1_lo);
3106 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3107 __ addl(in1_hi, edx);
3108 // in1.lo <- (in1.lo * in2.lo)[31:0];
3109 __ movl(in1_lo, eax);
3110 } else if (second.IsRegisterPair()) {
3111 Register in2_hi = second.AsRegisterPairHigh<Register>();
3112 Register in2_lo = second.AsRegisterPairLow<Register>();
3113
3114 __ movl(eax, in2_hi);
3115 // eax <- in1.lo * in2.hi
3116 __ imull(eax, in1_lo);
3117 // in1.hi <- in1.hi * in2.lo
3118 __ imull(in1_hi, in2_lo);
3119 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3120 __ addl(in1_hi, eax);
3121 // move in1_lo to eax to prepare for double precision
3122 __ movl(eax, in1_lo);
3123 // edx:eax <- in1.lo * in2.lo
3124 __ mull(in2_lo);
3125 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3126 __ addl(in1_hi, edx);
3127 // in1.lo <- (in1.lo * in2.lo)[31:0];
3128 __ movl(in1_lo, eax);
3129 } else {
3130 DCHECK(second.IsDoubleStackSlot()) << second;
3131 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3132 Address in2_lo(ESP, second.GetStackIndex());
3133
3134 __ movl(eax, in2_hi);
3135 // eax <- in1.lo * in2.hi
3136 __ imull(eax, in1_lo);
3137 // in1.hi <- in1.hi * in2.lo
3138 __ imull(in1_hi, in2_lo);
3139 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3140 __ addl(in1_hi, eax);
3141 // move in1_lo to eax to prepare for double precision
3142 __ movl(eax, in1_lo);
3143 // edx:eax <- in1.lo * in2.lo
3144 __ mull(in2_lo);
3145 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3146 __ addl(in1_hi, edx);
3147 // in1.lo <- (in1.lo * in2.lo)[31:0];
3148 __ movl(in1_lo, eax);
3149 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003150
3151 break;
3152 }
3153
Calin Juravleb5bfa962014-10-21 18:02:24 +01003154 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003155 DCHECK(first.Equals(locations->Out()));
3156 if (second.IsFpuRegister()) {
3157 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3158 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3159 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003160 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003161 __ mulss(first.AsFpuRegister<XmmRegister>(),
3162 codegen_->LiteralFloatAddress(
3163 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3164 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3165 } else {
3166 DCHECK(second.IsStackSlot());
3167 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3168 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003169 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003170 }
3171
3172 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003173 DCHECK(first.Equals(locations->Out()));
3174 if (second.IsFpuRegister()) {
3175 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3176 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3177 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003178 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003179 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3180 codegen_->LiteralDoubleAddress(
3181 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3182 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3183 } else {
3184 DCHECK(second.IsDoubleStackSlot());
3185 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3186 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003187 break;
3188 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003189
3190 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003191 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003192 }
3193}
3194
Roland Levillain232ade02015-04-20 15:14:36 +01003195void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3196 uint32_t temp_offset,
3197 uint32_t stack_adjustment,
3198 bool is_fp,
3199 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003200 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003201 DCHECK(!is_wide);
3202 if (is_fp) {
3203 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3204 } else {
3205 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3206 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003207 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003208 DCHECK(is_wide);
3209 if (is_fp) {
3210 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3211 } else {
3212 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3213 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003214 } else {
3215 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003216 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003217 Location stack_temp = Location::StackSlot(temp_offset);
3218 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003219 if (is_fp) {
3220 __ flds(Address(ESP, temp_offset));
3221 } else {
3222 __ filds(Address(ESP, temp_offset));
3223 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003224 } else {
3225 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3226 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003227 if (is_fp) {
3228 __ fldl(Address(ESP, temp_offset));
3229 } else {
3230 __ fildl(Address(ESP, temp_offset));
3231 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003232 }
3233 }
3234}
3235
3236void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3237 Primitive::Type type = rem->GetResultType();
3238 bool is_float = type == Primitive::kPrimFloat;
3239 size_t elem_size = Primitive::ComponentSize(type);
3240 LocationSummary* locations = rem->GetLocations();
3241 Location first = locations->InAt(0);
3242 Location second = locations->InAt(1);
3243 Location out = locations->Out();
3244
3245 // Create stack space for 2 elements.
3246 // TODO: enhance register allocator to ask for stack temporaries.
3247 __ subl(ESP, Immediate(2 * elem_size));
3248
3249 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003250 const bool is_wide = !is_float;
3251 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3252 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003253
3254 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003255 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003256 __ Bind(&retry);
3257 __ fprem();
3258
3259 // Move FP status to AX.
3260 __ fstsw();
3261
3262 // And see if the argument reduction is complete. This is signaled by the
3263 // C2 FPU flag bit set to 0.
3264 __ andl(EAX, Immediate(kC2ConditionMask));
3265 __ j(kNotEqual, &retry);
3266
3267 // We have settled on the final value. Retrieve it into an XMM register.
3268 // Store FP top of stack to real stack.
3269 if (is_float) {
3270 __ fsts(Address(ESP, 0));
3271 } else {
3272 __ fstl(Address(ESP, 0));
3273 }
3274
3275 // Pop the 2 items from the FP stack.
3276 __ fucompp();
3277
3278 // Load the value from the stack into an XMM register.
3279 DCHECK(out.IsFpuRegister()) << out;
3280 if (is_float) {
3281 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3282 } else {
3283 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3284 }
3285
3286 // And remove the temporary stack space we allocated.
3287 __ addl(ESP, Immediate(2 * elem_size));
3288}
3289
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003290
3291void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3292 DCHECK(instruction->IsDiv() || instruction->IsRem());
3293
3294 LocationSummary* locations = instruction->GetLocations();
3295 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003296 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003297
3298 Register out_register = locations->Out().AsRegister<Register>();
3299 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003300 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003301
3302 DCHECK(imm == 1 || imm == -1);
3303
3304 if (instruction->IsRem()) {
3305 __ xorl(out_register, out_register);
3306 } else {
3307 __ movl(out_register, input_register);
3308 if (imm == -1) {
3309 __ negl(out_register);
3310 }
3311 }
3312}
3313
3314
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003315void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003316 LocationSummary* locations = instruction->GetLocations();
3317
3318 Register out_register = locations->Out().AsRegister<Register>();
3319 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003320 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003321 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3322 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003323
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003324 Register num = locations->GetTemp(0).AsRegister<Register>();
3325
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003326 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003327 __ testl(input_register, input_register);
3328 __ cmovl(kGreaterEqual, num, input_register);
3329 int shift = CTZ(imm);
3330 __ sarl(num, Immediate(shift));
3331
3332 if (imm < 0) {
3333 __ negl(num);
3334 }
3335
3336 __ movl(out_register, num);
3337}
3338
3339void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3340 DCHECK(instruction->IsDiv() || instruction->IsRem());
3341
3342 LocationSummary* locations = instruction->GetLocations();
3343 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3344
3345 Register eax = locations->InAt(0).AsRegister<Register>();
3346 Register out = locations->Out().AsRegister<Register>();
3347 Register num;
3348 Register edx;
3349
3350 if (instruction->IsDiv()) {
3351 edx = locations->GetTemp(0).AsRegister<Register>();
3352 num = locations->GetTemp(1).AsRegister<Register>();
3353 } else {
3354 edx = locations->Out().AsRegister<Register>();
3355 num = locations->GetTemp(0).AsRegister<Register>();
3356 }
3357
3358 DCHECK_EQ(EAX, eax);
3359 DCHECK_EQ(EDX, edx);
3360 if (instruction->IsDiv()) {
3361 DCHECK_EQ(EAX, out);
3362 } else {
3363 DCHECK_EQ(EDX, out);
3364 }
3365
3366 int64_t magic;
3367 int shift;
3368 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3369
Mark Mendell0c9497d2015-08-21 09:30:05 -04003370 NearLabel ndiv;
3371 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003372 // If numerator is 0, the result is 0, no computation needed.
3373 __ testl(eax, eax);
3374 __ j(kNotEqual, &ndiv);
3375
3376 __ xorl(out, out);
3377 __ jmp(&end);
3378
3379 __ Bind(&ndiv);
3380
3381 // Save the numerator.
3382 __ movl(num, eax);
3383
3384 // EAX = magic
3385 __ movl(eax, Immediate(magic));
3386
3387 // EDX:EAX = magic * numerator
3388 __ imull(num);
3389
3390 if (imm > 0 && magic < 0) {
3391 // EDX += num
3392 __ addl(edx, num);
3393 } else if (imm < 0 && magic > 0) {
3394 __ subl(edx, num);
3395 }
3396
3397 // Shift if needed.
3398 if (shift != 0) {
3399 __ sarl(edx, Immediate(shift));
3400 }
3401
3402 // EDX += 1 if EDX < 0
3403 __ movl(eax, edx);
3404 __ shrl(edx, Immediate(31));
3405 __ addl(edx, eax);
3406
3407 if (instruction->IsRem()) {
3408 __ movl(eax, num);
3409 __ imull(edx, Immediate(imm));
3410 __ subl(eax, edx);
3411 __ movl(edx, eax);
3412 } else {
3413 __ movl(eax, edx);
3414 }
3415 __ Bind(&end);
3416}
3417
Calin Juravlebacfec32014-11-14 15:54:36 +00003418void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3419 DCHECK(instruction->IsDiv() || instruction->IsRem());
3420
3421 LocationSummary* locations = instruction->GetLocations();
3422 Location out = locations->Out();
3423 Location first = locations->InAt(0);
3424 Location second = locations->InAt(1);
3425 bool is_div = instruction->IsDiv();
3426
3427 switch (instruction->GetResultType()) {
3428 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003429 DCHECK_EQ(EAX, first.AsRegister<Register>());
3430 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003431
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003432 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003433 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003434
3435 if (imm == 0) {
3436 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3437 } else if (imm == 1 || imm == -1) {
3438 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003439 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003440 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003441 } else {
3442 DCHECK(imm <= -2 || imm >= 2);
3443 GenerateDivRemWithAnyConstant(instruction);
3444 }
3445 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003446 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3447 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003448 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003449
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003450 Register second_reg = second.AsRegister<Register>();
3451 // 0x80000000/-1 triggers an arithmetic exception!
3452 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3453 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003454
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003455 __ cmpl(second_reg, Immediate(-1));
3456 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003457
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003458 // edx:eax <- sign-extended of eax
3459 __ cdq();
3460 // eax = quotient, edx = remainder
3461 __ idivl(second_reg);
3462 __ Bind(slow_path->GetExitLabel());
3463 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003464 break;
3465 }
3466
3467 case Primitive::kPrimLong: {
3468 InvokeRuntimeCallingConvention calling_convention;
3469 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3470 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3471 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3472 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3473 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3474 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3475
3476 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003477 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3478 instruction,
3479 instruction->GetDexPc(),
3480 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003481 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003482 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003483 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3484 instruction,
3485 instruction->GetDexPc(),
3486 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003487 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003488 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003489 break;
3490 }
3491
3492 default:
3493 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3494 }
3495}
3496
Calin Juravle7c4954d2014-10-28 16:57:40 +00003497void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003498 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003499 ? LocationSummary::kCall
3500 : LocationSummary::kNoCall;
3501 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3502
Calin Juravle7c4954d2014-10-28 16:57:40 +00003503 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003504 case Primitive::kPrimInt: {
3505 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003506 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003507 locations->SetOut(Location::SameAsFirstInput());
3508 // Intel uses edx:eax as the dividend.
3509 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003510 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3511 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3512 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003513 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003514 locations->AddTemp(Location::RequiresRegister());
3515 }
Calin Juravled0d48522014-11-04 16:40:20 +00003516 break;
3517 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003518 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003519 InvokeRuntimeCallingConvention calling_convention;
3520 locations->SetInAt(0, Location::RegisterPairLocation(
3521 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3522 locations->SetInAt(1, Location::RegisterPairLocation(
3523 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3524 // Runtime helper puts the result in EAX, EDX.
3525 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003526 break;
3527 }
3528 case Primitive::kPrimFloat:
3529 case Primitive::kPrimDouble: {
3530 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003531 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3532 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003533 } else if (div->InputAt(1)->IsConstant()) {
3534 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003535 } else {
3536 locations->SetInAt(1, Location::Any());
3537 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003538 locations->SetOut(Location::SameAsFirstInput());
3539 break;
3540 }
3541
3542 default:
3543 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3544 }
3545}
3546
3547void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3548 LocationSummary* locations = div->GetLocations();
3549 Location first = locations->InAt(0);
3550 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003551
3552 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003553 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003554 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003555 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003556 break;
3557 }
3558
3559 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003560 if (second.IsFpuRegister()) {
3561 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3562 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3563 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003564 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003565 __ divss(first.AsFpuRegister<XmmRegister>(),
3566 codegen_->LiteralFloatAddress(
3567 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3568 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3569 } else {
3570 DCHECK(second.IsStackSlot());
3571 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3572 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003573 break;
3574 }
3575
3576 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003577 if (second.IsFpuRegister()) {
3578 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3579 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3580 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003581 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003582 __ divsd(first.AsFpuRegister<XmmRegister>(),
3583 codegen_->LiteralDoubleAddress(
3584 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3585 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3586 } else {
3587 DCHECK(second.IsDoubleStackSlot());
3588 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3589 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003590 break;
3591 }
3592
3593 default:
3594 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3595 }
3596}
3597
Calin Juravlebacfec32014-11-14 15:54:36 +00003598void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003599 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003600
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003601 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3602 ? LocationSummary::kCall
3603 : LocationSummary::kNoCall;
3604 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003605
Calin Juravled2ec87d2014-12-08 14:24:46 +00003606 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003607 case Primitive::kPrimInt: {
3608 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003609 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003610 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003611 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3612 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3613 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003614 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003615 locations->AddTemp(Location::RequiresRegister());
3616 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003617 break;
3618 }
3619 case Primitive::kPrimLong: {
3620 InvokeRuntimeCallingConvention calling_convention;
3621 locations->SetInAt(0, Location::RegisterPairLocation(
3622 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3623 locations->SetInAt(1, Location::RegisterPairLocation(
3624 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3625 // Runtime helper puts the result in EAX, EDX.
3626 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3627 break;
3628 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003629 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003630 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003631 locations->SetInAt(0, Location::Any());
3632 locations->SetInAt(1, Location::Any());
3633 locations->SetOut(Location::RequiresFpuRegister());
3634 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003635 break;
3636 }
3637
3638 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003639 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003640 }
3641}
3642
3643void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3644 Primitive::Type type = rem->GetResultType();
3645 switch (type) {
3646 case Primitive::kPrimInt:
3647 case Primitive::kPrimLong: {
3648 GenerateDivRemIntegral(rem);
3649 break;
3650 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003651 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003652 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003653 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003654 break;
3655 }
3656 default:
3657 LOG(FATAL) << "Unexpected rem type " << type;
3658 }
3659}
3660
Calin Juravled0d48522014-11-04 16:40:20 +00003661void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003662 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3663 ? LocationSummary::kCallOnSlowPath
3664 : LocationSummary::kNoCall;
3665 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003666 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003667 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003668 case Primitive::kPrimByte:
3669 case Primitive::kPrimChar:
3670 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003671 case Primitive::kPrimInt: {
3672 locations->SetInAt(0, Location::Any());
3673 break;
3674 }
3675 case Primitive::kPrimLong: {
3676 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3677 if (!instruction->IsConstant()) {
3678 locations->AddTemp(Location::RequiresRegister());
3679 }
3680 break;
3681 }
3682 default:
3683 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3684 }
Calin Juravled0d48522014-11-04 16:40:20 +00003685 if (instruction->HasUses()) {
3686 locations->SetOut(Location::SameAsFirstInput());
3687 }
3688}
3689
3690void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003691 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003692 codegen_->AddSlowPath(slow_path);
3693
3694 LocationSummary* locations = instruction->GetLocations();
3695 Location value = locations->InAt(0);
3696
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003697 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003698 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003699 case Primitive::kPrimByte:
3700 case Primitive::kPrimChar:
3701 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003702 case Primitive::kPrimInt: {
3703 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003704 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003705 __ j(kEqual, slow_path->GetEntryLabel());
3706 } else if (value.IsStackSlot()) {
3707 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3708 __ j(kEqual, slow_path->GetEntryLabel());
3709 } else {
3710 DCHECK(value.IsConstant()) << value;
3711 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3712 __ jmp(slow_path->GetEntryLabel());
3713 }
3714 }
3715 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003716 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003717 case Primitive::kPrimLong: {
3718 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003719 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003720 __ movl(temp, value.AsRegisterPairLow<Register>());
3721 __ orl(temp, value.AsRegisterPairHigh<Register>());
3722 __ j(kEqual, slow_path->GetEntryLabel());
3723 } else {
3724 DCHECK(value.IsConstant()) << value;
3725 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3726 __ jmp(slow_path->GetEntryLabel());
3727 }
3728 }
3729 break;
3730 }
3731 default:
3732 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003733 }
Calin Juravled0d48522014-11-04 16:40:20 +00003734}
3735
Calin Juravle9aec02f2014-11-18 23:06:35 +00003736void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3737 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3738
3739 LocationSummary* locations =
3740 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3741
3742 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003743 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003744 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003745 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003746 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003747 // The shift count needs to be in CL or a constant.
3748 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003749 locations->SetOut(Location::SameAsFirstInput());
3750 break;
3751 }
3752 default:
3753 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3754 }
3755}
3756
3757void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3758 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3759
3760 LocationSummary* locations = op->GetLocations();
3761 Location first = locations->InAt(0);
3762 Location second = locations->InAt(1);
3763 DCHECK(first.Equals(locations->Out()));
3764
3765 switch (op->GetResultType()) {
3766 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003767 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003768 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003769 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003770 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003771 DCHECK_EQ(ECX, second_reg);
3772 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003773 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003774 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003775 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003776 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003777 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003778 }
3779 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003780 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003781 if (shift == 0) {
3782 return;
3783 }
3784 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003785 if (op->IsShl()) {
3786 __ shll(first_reg, imm);
3787 } else if (op->IsShr()) {
3788 __ sarl(first_reg, imm);
3789 } else {
3790 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003791 }
3792 }
3793 break;
3794 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003795 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003796 if (second.IsRegister()) {
3797 Register second_reg = second.AsRegister<Register>();
3798 DCHECK_EQ(ECX, second_reg);
3799 if (op->IsShl()) {
3800 GenerateShlLong(first, second_reg);
3801 } else if (op->IsShr()) {
3802 GenerateShrLong(first, second_reg);
3803 } else {
3804 GenerateUShrLong(first, second_reg);
3805 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003806 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003807 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003808 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003809 // Nothing to do if the shift is 0, as the input is already the output.
3810 if (shift != 0) {
3811 if (op->IsShl()) {
3812 GenerateShlLong(first, shift);
3813 } else if (op->IsShr()) {
3814 GenerateShrLong(first, shift);
3815 } else {
3816 GenerateUShrLong(first, shift);
3817 }
3818 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003819 }
3820 break;
3821 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003822 default:
3823 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3824 }
3825}
3826
Mark P Mendell73945692015-04-29 14:56:17 +00003827void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3828 Register low = loc.AsRegisterPairLow<Register>();
3829 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003830 if (shift == 1) {
3831 // This is just an addition.
3832 __ addl(low, low);
3833 __ adcl(high, high);
3834 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003835 // Shift by 32 is easy. High gets low, and low gets 0.
3836 codegen_->EmitParallelMoves(
3837 loc.ToLow(),
3838 loc.ToHigh(),
3839 Primitive::kPrimInt,
3840 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3841 loc.ToLow(),
3842 Primitive::kPrimInt);
3843 } else if (shift > 32) {
3844 // Low part becomes 0. High part is low part << (shift-32).
3845 __ movl(high, low);
3846 __ shll(high, Immediate(shift - 32));
3847 __ xorl(low, low);
3848 } else {
3849 // Between 1 and 31.
3850 __ shld(high, low, Immediate(shift));
3851 __ shll(low, Immediate(shift));
3852 }
3853}
3854
Calin Juravle9aec02f2014-11-18 23:06:35 +00003855void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003856 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003857 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3858 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3859 __ testl(shifter, Immediate(32));
3860 __ j(kEqual, &done);
3861 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3862 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3863 __ Bind(&done);
3864}
3865
Mark P Mendell73945692015-04-29 14:56:17 +00003866void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3867 Register low = loc.AsRegisterPairLow<Register>();
3868 Register high = loc.AsRegisterPairHigh<Register>();
3869 if (shift == 32) {
3870 // Need to copy the sign.
3871 DCHECK_NE(low, high);
3872 __ movl(low, high);
3873 __ sarl(high, Immediate(31));
3874 } else if (shift > 32) {
3875 DCHECK_NE(low, high);
3876 // High part becomes sign. Low part is shifted by shift - 32.
3877 __ movl(low, high);
3878 __ sarl(high, Immediate(31));
3879 __ sarl(low, Immediate(shift - 32));
3880 } else {
3881 // Between 1 and 31.
3882 __ shrd(low, high, Immediate(shift));
3883 __ sarl(high, Immediate(shift));
3884 }
3885}
3886
Calin Juravle9aec02f2014-11-18 23:06:35 +00003887void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003888 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003889 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3890 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3891 __ testl(shifter, Immediate(32));
3892 __ j(kEqual, &done);
3893 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3894 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3895 __ Bind(&done);
3896}
3897
Mark P Mendell73945692015-04-29 14:56:17 +00003898void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3899 Register low = loc.AsRegisterPairLow<Register>();
3900 Register high = loc.AsRegisterPairHigh<Register>();
3901 if (shift == 32) {
3902 // Shift by 32 is easy. Low gets high, and high gets 0.
3903 codegen_->EmitParallelMoves(
3904 loc.ToHigh(),
3905 loc.ToLow(),
3906 Primitive::kPrimInt,
3907 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3908 loc.ToHigh(),
3909 Primitive::kPrimInt);
3910 } else if (shift > 32) {
3911 // Low part is high >> (shift - 32). High part becomes 0.
3912 __ movl(low, high);
3913 __ shrl(low, Immediate(shift - 32));
3914 __ xorl(high, high);
3915 } else {
3916 // Between 1 and 31.
3917 __ shrd(low, high, Immediate(shift));
3918 __ shrl(high, Immediate(shift));
3919 }
3920}
3921
Calin Juravle9aec02f2014-11-18 23:06:35 +00003922void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003923 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003924 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3925 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3926 __ testl(shifter, Immediate(32));
3927 __ j(kEqual, &done);
3928 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3929 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3930 __ Bind(&done);
3931}
3932
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003933void LocationsBuilderX86::VisitRor(HRor* ror) {
3934 LocationSummary* locations =
3935 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3936
3937 switch (ror->GetResultType()) {
3938 case Primitive::kPrimLong:
3939 // Add the temporary needed.
3940 locations->AddTemp(Location::RequiresRegister());
3941 FALLTHROUGH_INTENDED;
3942 case Primitive::kPrimInt:
3943 locations->SetInAt(0, Location::RequiresRegister());
3944 // The shift count needs to be in CL (unless it is a constant).
3945 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3946 locations->SetOut(Location::SameAsFirstInput());
3947 break;
3948 default:
3949 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3950 UNREACHABLE();
3951 }
3952}
3953
3954void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3955 LocationSummary* locations = ror->GetLocations();
3956 Location first = locations->InAt(0);
3957 Location second = locations->InAt(1);
3958
3959 if (ror->GetResultType() == Primitive::kPrimInt) {
3960 Register first_reg = first.AsRegister<Register>();
3961 if (second.IsRegister()) {
3962 Register second_reg = second.AsRegister<Register>();
3963 __ rorl(first_reg, second_reg);
3964 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003965 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003966 __ rorl(first_reg, imm);
3967 }
3968 return;
3969 }
3970
3971 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3972 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3973 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3974 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3975 if (second.IsRegister()) {
3976 Register second_reg = second.AsRegister<Register>();
3977 DCHECK_EQ(second_reg, ECX);
3978 __ movl(temp_reg, first_reg_hi);
3979 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3980 __ shrd(first_reg_lo, temp_reg, second_reg);
3981 __ movl(temp_reg, first_reg_hi);
3982 __ testl(second_reg, Immediate(32));
3983 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3984 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3985 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003986 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003987 if (shift_amt == 0) {
3988 // Already fine.
3989 return;
3990 }
3991 if (shift_amt == 32) {
3992 // Just swap.
3993 __ movl(temp_reg, first_reg_lo);
3994 __ movl(first_reg_lo, first_reg_hi);
3995 __ movl(first_reg_hi, temp_reg);
3996 return;
3997 }
3998
3999 Immediate imm(shift_amt);
4000 // Save the constents of the low value.
4001 __ movl(temp_reg, first_reg_lo);
4002
4003 // Shift right into low, feeding bits from high.
4004 __ shrd(first_reg_lo, first_reg_hi, imm);
4005
4006 // Shift right into high, feeding bits from the original low.
4007 __ shrd(first_reg_hi, temp_reg, imm);
4008
4009 // Swap if needed.
4010 if (shift_amt > 32) {
4011 __ movl(temp_reg, first_reg_lo);
4012 __ movl(first_reg_lo, first_reg_hi);
4013 __ movl(first_reg_hi, temp_reg);
4014 }
4015 }
4016}
4017
Calin Juravle9aec02f2014-11-18 23:06:35 +00004018void LocationsBuilderX86::VisitShl(HShl* shl) {
4019 HandleShift(shl);
4020}
4021
4022void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4023 HandleShift(shl);
4024}
4025
4026void LocationsBuilderX86::VisitShr(HShr* shr) {
4027 HandleShift(shr);
4028}
4029
4030void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4031 HandleShift(shr);
4032}
4033
4034void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4035 HandleShift(ushr);
4036}
4037
4038void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4039 HandleShift(ushr);
4040}
4041
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004042void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004043 LocationSummary* locations =
4044 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004045 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004046 if (instruction->IsStringAlloc()) {
4047 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4048 } else {
4049 InvokeRuntimeCallingConvention calling_convention;
4050 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4051 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4052 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004053}
4054
4055void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004056 // Note: if heap poisoning is enabled, the entry point takes cares
4057 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004058 if (instruction->IsStringAlloc()) {
4059 // String is allocated through StringFactory. Call NewEmptyString entry point.
4060 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4061 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
4062 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4063 __ call(Address(temp, code_offset.Int32Value()));
4064 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4065 } else {
4066 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4067 instruction,
4068 instruction->GetDexPc(),
4069 nullptr);
4070 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4071 DCHECK(!codegen_->IsLeafMethod());
4072 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004073}
4074
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004075void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4076 LocationSummary* locations =
4077 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4078 locations->SetOut(Location::RegisterLocation(EAX));
4079 InvokeRuntimeCallingConvention calling_convention;
4080 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004081 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004082 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004083}
4084
4085void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4086 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004087 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004088 // Note: if heap poisoning is enabled, the entry point takes cares
4089 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004090 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4091 instruction,
4092 instruction->GetDexPc(),
4093 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004094 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004095 DCHECK(!codegen_->IsLeafMethod());
4096}
4097
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004098void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004099 LocationSummary* locations =
4100 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004101 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4102 if (location.IsStackSlot()) {
4103 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4104 } else if (location.IsDoubleStackSlot()) {
4105 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004106 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004107 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004108}
4109
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004110void InstructionCodeGeneratorX86::VisitParameterValue(
4111 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4112}
4113
4114void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4115 LocationSummary* locations =
4116 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4117 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4118}
4119
4120void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004121}
4122
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004123void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4124 LocationSummary* locations =
4125 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4126 locations->SetInAt(0, Location::RequiresRegister());
4127 locations->SetOut(Location::RequiresRegister());
4128}
4129
4130void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4131 LocationSummary* locations = instruction->GetLocations();
4132 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004133 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004134 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4135 instruction->GetIndex(), kX86PointerSize).SizeValue();
4136 } else {
4137 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4138 instruction->GetIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
4139 }
4140 __ movl(locations->Out().AsRegister<Register>(),
4141 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4142}
4143
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004144void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004145 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004146 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004147 locations->SetInAt(0, Location::RequiresRegister());
4148 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004149}
4150
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004151void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4152 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004153 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004154 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004155 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004156 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004157 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004158 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004159 break;
4160
4161 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004162 __ notl(out.AsRegisterPairLow<Register>());
4163 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004164 break;
4165
4166 default:
4167 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4168 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004169}
4170
David Brazdil66d126e2015-04-03 16:02:44 +01004171void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4172 LocationSummary* locations =
4173 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4174 locations->SetInAt(0, Location::RequiresRegister());
4175 locations->SetOut(Location::SameAsFirstInput());
4176}
4177
4178void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004179 LocationSummary* locations = bool_not->GetLocations();
4180 Location in = locations->InAt(0);
4181 Location out = locations->Out();
4182 DCHECK(in.Equals(out));
4183 __ xorl(out.AsRegister<Register>(), Immediate(1));
4184}
4185
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004186void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004187 LocationSummary* locations =
4188 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004189 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004190 case Primitive::kPrimBoolean:
4191 case Primitive::kPrimByte:
4192 case Primitive::kPrimShort:
4193 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004194 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004195 case Primitive::kPrimLong: {
4196 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004197 locations->SetInAt(1, Location::Any());
4198 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4199 break;
4200 }
4201 case Primitive::kPrimFloat:
4202 case Primitive::kPrimDouble: {
4203 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004204 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4205 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4206 } else if (compare->InputAt(1)->IsConstant()) {
4207 locations->SetInAt(1, Location::RequiresFpuRegister());
4208 } else {
4209 locations->SetInAt(1, Location::Any());
4210 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004211 locations->SetOut(Location::RequiresRegister());
4212 break;
4213 }
4214 default:
4215 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4216 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004217}
4218
4219void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004220 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004221 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004222 Location left = locations->InAt(0);
4223 Location right = locations->InAt(1);
4224
Mark Mendell0c9497d2015-08-21 09:30:05 -04004225 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004226 Condition less_cond = kLess;
4227
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004228 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004229 case Primitive::kPrimBoolean:
4230 case Primitive::kPrimByte:
4231 case Primitive::kPrimShort:
4232 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004233 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004234 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004235 break;
4236 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004237 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004238 Register left_low = left.AsRegisterPairLow<Register>();
4239 Register left_high = left.AsRegisterPairHigh<Register>();
4240 int32_t val_low = 0;
4241 int32_t val_high = 0;
4242 bool right_is_const = false;
4243
4244 if (right.IsConstant()) {
4245 DCHECK(right.GetConstant()->IsLongConstant());
4246 right_is_const = true;
4247 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4248 val_low = Low32Bits(val);
4249 val_high = High32Bits(val);
4250 }
4251
Calin Juravleddb7df22014-11-25 20:56:51 +00004252 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004253 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004254 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004255 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004256 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004257 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004258 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004259 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004260 __ j(kLess, &less); // Signed compare.
4261 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004262 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004263 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004264 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004265 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004266 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004267 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004268 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004269 }
Aart Bika19616e2016-02-01 18:57:58 -08004270 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004271 break;
4272 }
4273 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004274 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004275 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004276 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004277 break;
4278 }
4279 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004280 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004281 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004282 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004283 break;
4284 }
4285 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004286 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004287 }
Aart Bika19616e2016-02-01 18:57:58 -08004288
Calin Juravleddb7df22014-11-25 20:56:51 +00004289 __ movl(out, Immediate(0));
4290 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004291 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004292
4293 __ Bind(&greater);
4294 __ movl(out, Immediate(1));
4295 __ jmp(&done);
4296
4297 __ Bind(&less);
4298 __ movl(out, Immediate(-1));
4299
4300 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004301}
4302
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004303void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004304 LocationSummary* locations =
4305 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004306 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4307 locations->SetInAt(i, Location::Any());
4308 }
4309 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004310}
4311
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004312void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004313 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004314}
4315
Roland Levillain7c1559a2015-12-15 10:55:36 +00004316void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004317 /*
4318 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4319 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4320 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4321 */
4322 switch (kind) {
4323 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004324 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004325 break;
4326 }
4327 case MemBarrierKind::kAnyStore:
4328 case MemBarrierKind::kLoadAny:
4329 case MemBarrierKind::kStoreStore: {
4330 // nop
4331 break;
4332 }
4333 default:
4334 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004335 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004336}
4337
Vladimir Markodc151b22015-10-15 18:02:30 +01004338HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4339 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4340 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004341 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4342
4343 // We disable pc-relative load when there is an irreducible loop, as the optimization
4344 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004345 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4346 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004347 if (GetGraph()->HasIrreducibleLoops() &&
4348 (dispatch_info.method_load_kind ==
4349 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4350 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4351 }
4352 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004353 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4354 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4355 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4356 // (Though the direct CALL ptr16:32 is available for consideration).
4357 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004358 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004359 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004360 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004361 0u
4362 };
4363 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004364 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004365 }
4366}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004367
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004368Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4369 Register temp) {
4370 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004371 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004372 if (!invoke->GetLocations()->Intrinsified()) {
4373 return location.AsRegister<Register>();
4374 }
4375 // For intrinsics we allow any location, so it may be on the stack.
4376 if (!location.IsRegister()) {
4377 __ movl(temp, Address(ESP, location.GetStackIndex()));
4378 return temp;
4379 }
4380 // For register locations, check if the register was saved. If so, get it from the stack.
4381 // Note: There is a chance that the register was saved but not overwritten, so we could
4382 // save one load. However, since this is just an intrinsic slow path we prefer this
4383 // simple and more robust approach rather that trying to determine if that's the case.
4384 SlowPathCode* slow_path = GetCurrentSlowPath();
4385 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4386 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4387 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4388 __ movl(temp, Address(ESP, stack_offset));
4389 return temp;
4390 }
4391 return location.AsRegister<Register>();
4392}
4393
Vladimir Marko58155012015-08-19 12:49:41 +00004394void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4395 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4396 switch (invoke->GetMethodLoadKind()) {
4397 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4398 // temp = thread->string_init_entrypoint
4399 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4400 break;
4401 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004402 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004403 break;
4404 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4405 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4406 break;
4407 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004408 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004409 method_patches_.emplace_back(invoke->GetTargetMethod());
4410 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4411 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004412 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4413 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4414 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004415 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004416 // Bind a new fixup label at the end of the "movl" insn.
4417 uint32_t offset = invoke->GetDexCacheArrayOffset();
4418 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004419 break;
4420 }
Vladimir Marko58155012015-08-19 12:49:41 +00004421 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004422 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004423 Register method_reg;
4424 Register reg = temp.AsRegister<Register>();
4425 if (current_method.IsRegister()) {
4426 method_reg = current_method.AsRegister<Register>();
4427 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004428 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004429 DCHECK(!current_method.IsValid());
4430 method_reg = reg;
4431 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4432 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004433 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004434 __ movl(reg, Address(method_reg,
4435 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004436 // temp = temp[index_in_cache]
4437 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4438 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4439 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004440 }
Vladimir Marko58155012015-08-19 12:49:41 +00004441 }
4442
4443 switch (invoke->GetCodePtrLocation()) {
4444 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4445 __ call(GetFrameEntryLabel());
4446 break;
4447 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4448 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4449 Label* label = &relative_call_patches_.back().label;
4450 __ call(label); // Bind to the patch label, override at link time.
4451 __ Bind(label); // Bind the label at the end of the "call" insn.
4452 break;
4453 }
4454 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4455 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004456 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4457 LOG(FATAL) << "Unsupported";
4458 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004459 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4460 // (callee_method + offset_of_quick_compiled_code)()
4461 __ call(Address(callee_method.AsRegister<Register>(),
4462 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4463 kX86WordSize).Int32Value()));
4464 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004465 }
4466
4467 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004468}
4469
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004470void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4471 Register temp = temp_in.AsRegister<Register>();
4472 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4473 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004474
4475 // Use the calling convention instead of the location of the receiver, as
4476 // intrinsics may have put the receiver in a different register. In the intrinsics
4477 // slow path, the arguments have been moved to the right place, so here we are
4478 // guaranteed that the receiver is the first register of the calling convention.
4479 InvokeDexCallingConvention calling_convention;
4480 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004481 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004482 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004483 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004484 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004485 // Instead of simply (possibly) unpoisoning `temp` here, we should
4486 // emit a read barrier for the previous class reference load.
4487 // However this is not required in practice, as this is an
4488 // intermediate/temporary reference and because the current
4489 // concurrent copying collector keeps the from-space memory
4490 // intact/accessible until the end of the marking phase (the
4491 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004492 __ MaybeUnpoisonHeapReference(temp);
4493 // temp = temp->GetMethodAt(method_offset);
4494 __ movl(temp, Address(temp, method_offset));
4495 // call temp->GetEntryPoint();
4496 __ call(Address(
4497 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4498}
4499
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004500void CodeGeneratorX86::RecordSimplePatch() {
4501 if (GetCompilerOptions().GetIncludePatchInformation()) {
4502 simple_patches_.emplace_back();
4503 __ Bind(&simple_patches_.back());
4504 }
4505}
4506
4507void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4508 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4509 __ Bind(&string_patches_.back().label);
4510}
4511
4512Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4513 uint32_t element_offset) {
4514 // Add the patch entry and bind its label at the end of the instruction.
4515 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4516 return &pc_relative_dex_cache_patches_.back().label;
4517}
4518
Vladimir Marko58155012015-08-19 12:49:41 +00004519void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4520 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004521 size_t size =
4522 method_patches_.size() +
4523 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004524 pc_relative_dex_cache_patches_.size() +
4525 simple_patches_.size() +
4526 string_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004527 linker_patches->reserve(size);
4528 // The label points to the end of the "movl" insn but the literal offset for method
4529 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4530 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004531 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004532 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004533 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4534 info.target_method.dex_file,
4535 info.target_method.dex_method_index));
4536 }
4537 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004538 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004539 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4540 info.target_method.dex_file,
4541 info.target_method.dex_method_index));
4542 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004543 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4544 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4545 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4546 &info.target_dex_file,
4547 GetMethodAddressOffset(),
4548 info.element_offset));
4549 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004550 for (const Label& label : simple_patches_) {
4551 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4552 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4553 }
4554 if (GetCompilerOptions().GetCompilePic()) {
4555 for (const StringPatchInfo<Label>& info : string_patches_) {
4556 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4557 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4558 &info.dex_file,
4559 GetMethodAddressOffset(),
4560 info.string_index));
4561 }
4562 } else {
4563 for (const StringPatchInfo<Label>& info : string_patches_) {
4564 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4565 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4566 &info.dex_file,
4567 info.string_index));
4568 }
4569 }
Vladimir Marko58155012015-08-19 12:49:41 +00004570}
4571
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004572void CodeGeneratorX86::MarkGCCard(Register temp,
4573 Register card,
4574 Register object,
4575 Register value,
4576 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004577 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004578 if (value_can_be_null) {
4579 __ testl(value, value);
4580 __ j(kEqual, &is_null);
4581 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004582 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4583 __ movl(temp, object);
4584 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004585 __ movb(Address(temp, card, TIMES_1, 0),
4586 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004587 if (value_can_be_null) {
4588 __ Bind(&is_null);
4589 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004590}
4591
Calin Juravle52c48962014-12-16 17:02:57 +00004592void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4593 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004594
4595 bool object_field_get_with_read_barrier =
4596 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004597 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004598 new (GetGraph()->GetArena()) LocationSummary(instruction,
4599 kEmitCompilerReadBarrier ?
4600 LocationSummary::kCallOnSlowPath :
4601 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004602 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004603
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004604 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4605 locations->SetOut(Location::RequiresFpuRegister());
4606 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004607 // The output overlaps in case of long: we don't want the low move
4608 // to overwrite the object's location. Likewise, in the case of
4609 // an object field get with read barriers enabled, we do not want
4610 // the move to overwrite the object's location, as we need it to emit
4611 // the read barrier.
4612 locations->SetOut(
4613 Location::RequiresRegister(),
4614 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4615 Location::kOutputOverlap :
4616 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004617 }
Calin Juravle52c48962014-12-16 17:02:57 +00004618
4619 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4620 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004621 // So we use an XMM register as a temp to achieve atomicity (first
4622 // load the temp into the XMM and then copy the XMM into the
4623 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004624 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004625 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4626 // We need a temporary register for the read barrier marking slow
4627 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4628 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004629 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004630}
4631
Calin Juravle52c48962014-12-16 17:02:57 +00004632void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4633 const FieldInfo& field_info) {
4634 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004635
Calin Juravle52c48962014-12-16 17:02:57 +00004636 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004637 Location base_loc = locations->InAt(0);
4638 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004639 Location out = locations->Out();
4640 bool is_volatile = field_info.IsVolatile();
4641 Primitive::Type field_type = field_info.GetFieldType();
4642 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4643
4644 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004645 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004646 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004647 break;
4648 }
4649
4650 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004651 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004652 break;
4653 }
4654
4655 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004656 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004657 break;
4658 }
4659
4660 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004661 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004662 break;
4663 }
4664
4665 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004666 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004667 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004668
4669 case Primitive::kPrimNot: {
4670 // /* HeapReference<Object> */ out = *(base + offset)
4671 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4672 Location temp_loc = locations->GetTemp(0);
4673 // Note that a potential implicit null check is handled in this
4674 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4675 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4676 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4677 if (is_volatile) {
4678 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4679 }
4680 } else {
4681 __ movl(out.AsRegister<Register>(), Address(base, offset));
4682 codegen_->MaybeRecordImplicitNullCheck(instruction);
4683 if (is_volatile) {
4684 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4685 }
4686 // If read barriers are enabled, emit read barriers other than
4687 // Baker's using a slow path (and also unpoison the loaded
4688 // reference, if heap poisoning is enabled).
4689 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4690 }
4691 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004692 }
4693
4694 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004695 if (is_volatile) {
4696 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4697 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004698 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004699 __ movd(out.AsRegisterPairLow<Register>(), temp);
4700 __ psrlq(temp, Immediate(32));
4701 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4702 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004703 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004704 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004705 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004706 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4707 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004708 break;
4709 }
4710
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004711 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004712 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004713 break;
4714 }
4715
4716 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004717 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004718 break;
4719 }
4720
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004721 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004722 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004723 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004724 }
Calin Juravle52c48962014-12-16 17:02:57 +00004725
Roland Levillain7c1559a2015-12-15 10:55:36 +00004726 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4727 // Potential implicit null checks, in the case of reference or
4728 // long fields, are handled in the previous switch statement.
4729 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004730 codegen_->MaybeRecordImplicitNullCheck(instruction);
4731 }
4732
Calin Juravle52c48962014-12-16 17:02:57 +00004733 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004734 if (field_type == Primitive::kPrimNot) {
4735 // Memory barriers, in the case of references, are also handled
4736 // in the previous switch statement.
4737 } else {
4738 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4739 }
Roland Levillain4d027112015-07-01 15:41:14 +01004740 }
Calin Juravle52c48962014-12-16 17:02:57 +00004741}
4742
4743void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4744 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4745
4746 LocationSummary* locations =
4747 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4748 locations->SetInAt(0, Location::RequiresRegister());
4749 bool is_volatile = field_info.IsVolatile();
4750 Primitive::Type field_type = field_info.GetFieldType();
4751 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4752 || (field_type == Primitive::kPrimByte);
4753
4754 // The register allocator does not support multiple
4755 // inputs that die at entry with one in a specific register.
4756 if (is_byte_type) {
4757 // Ensure the value is in a byte register.
4758 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004759 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004760 if (is_volatile && field_type == Primitive::kPrimDouble) {
4761 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4762 locations->SetInAt(1, Location::RequiresFpuRegister());
4763 } else {
4764 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4765 }
4766 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4767 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004768 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004769
Calin Juravle52c48962014-12-16 17:02:57 +00004770 // 64bits value can be atomically written to an address with movsd and an XMM register.
4771 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4772 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4773 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4774 // isolated cases when we need this it isn't worth adding the extra complexity.
4775 locations->AddTemp(Location::RequiresFpuRegister());
4776 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004777 } else {
4778 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4779
4780 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4781 // Temporary registers for the write barrier.
4782 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4783 // Ensure the card is in a byte register.
4784 locations->AddTemp(Location::RegisterLocation(ECX));
4785 }
Calin Juravle52c48962014-12-16 17:02:57 +00004786 }
4787}
4788
4789void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004790 const FieldInfo& field_info,
4791 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004792 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4793
4794 LocationSummary* locations = instruction->GetLocations();
4795 Register base = locations->InAt(0).AsRegister<Register>();
4796 Location value = locations->InAt(1);
4797 bool is_volatile = field_info.IsVolatile();
4798 Primitive::Type field_type = field_info.GetFieldType();
4799 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004800 bool needs_write_barrier =
4801 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004802
4803 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004804 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004805 }
4806
Mark Mendell81489372015-11-04 11:30:41 -05004807 bool maybe_record_implicit_null_check_done = false;
4808
Calin Juravle52c48962014-12-16 17:02:57 +00004809 switch (field_type) {
4810 case Primitive::kPrimBoolean:
4811 case Primitive::kPrimByte: {
4812 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4813 break;
4814 }
4815
4816 case Primitive::kPrimShort:
4817 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004818 if (value.IsConstant()) {
4819 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4820 __ movw(Address(base, offset), Immediate(v));
4821 } else {
4822 __ movw(Address(base, offset), value.AsRegister<Register>());
4823 }
Calin Juravle52c48962014-12-16 17:02:57 +00004824 break;
4825 }
4826
4827 case Primitive::kPrimInt:
4828 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004829 if (kPoisonHeapReferences && needs_write_barrier) {
4830 // Note that in the case where `value` is a null reference,
4831 // we do not enter this block, as the reference does not
4832 // need poisoning.
4833 DCHECK_EQ(field_type, Primitive::kPrimNot);
4834 Register temp = locations->GetTemp(0).AsRegister<Register>();
4835 __ movl(temp, value.AsRegister<Register>());
4836 __ PoisonHeapReference(temp);
4837 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004838 } else if (value.IsConstant()) {
4839 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4840 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004841 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004842 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004843 __ movl(Address(base, offset), value.AsRegister<Register>());
4844 }
Calin Juravle52c48962014-12-16 17:02:57 +00004845 break;
4846 }
4847
4848 case Primitive::kPrimLong: {
4849 if (is_volatile) {
4850 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4851 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4852 __ movd(temp1, value.AsRegisterPairLow<Register>());
4853 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4854 __ punpckldq(temp1, temp2);
4855 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004856 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004857 } else if (value.IsConstant()) {
4858 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4859 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4860 codegen_->MaybeRecordImplicitNullCheck(instruction);
4861 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004862 } else {
4863 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004864 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004865 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4866 }
Mark Mendell81489372015-11-04 11:30:41 -05004867 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004868 break;
4869 }
4870
4871 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004872 if (value.IsConstant()) {
4873 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4874 __ movl(Address(base, offset), Immediate(v));
4875 } else {
4876 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4877 }
Calin Juravle52c48962014-12-16 17:02:57 +00004878 break;
4879 }
4880
4881 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004882 if (value.IsConstant()) {
4883 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4884 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4885 codegen_->MaybeRecordImplicitNullCheck(instruction);
4886 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4887 maybe_record_implicit_null_check_done = true;
4888 } else {
4889 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4890 }
Calin Juravle52c48962014-12-16 17:02:57 +00004891 break;
4892 }
4893
4894 case Primitive::kPrimVoid:
4895 LOG(FATAL) << "Unreachable type " << field_type;
4896 UNREACHABLE();
4897 }
4898
Mark Mendell81489372015-11-04 11:30:41 -05004899 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004900 codegen_->MaybeRecordImplicitNullCheck(instruction);
4901 }
4902
Roland Levillain4d027112015-07-01 15:41:14 +01004903 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004904 Register temp = locations->GetTemp(0).AsRegister<Register>();
4905 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004906 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004907 }
4908
Calin Juravle52c48962014-12-16 17:02:57 +00004909 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004910 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004911 }
4912}
4913
4914void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4915 HandleFieldGet(instruction, instruction->GetFieldInfo());
4916}
4917
4918void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4919 HandleFieldGet(instruction, instruction->GetFieldInfo());
4920}
4921
4922void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4923 HandleFieldSet(instruction, instruction->GetFieldInfo());
4924}
4925
4926void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004927 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004928}
4929
4930void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4931 HandleFieldSet(instruction, instruction->GetFieldInfo());
4932}
4933
4934void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004935 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004936}
4937
4938void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4939 HandleFieldGet(instruction, instruction->GetFieldInfo());
4940}
4941
4942void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4943 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004944}
4945
Calin Juravlee460d1d2015-09-29 04:52:17 +01004946void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4947 HUnresolvedInstanceFieldGet* instruction) {
4948 FieldAccessCallingConventionX86 calling_convention;
4949 codegen_->CreateUnresolvedFieldLocationSummary(
4950 instruction, instruction->GetFieldType(), calling_convention);
4951}
4952
4953void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4954 HUnresolvedInstanceFieldGet* instruction) {
4955 FieldAccessCallingConventionX86 calling_convention;
4956 codegen_->GenerateUnresolvedFieldAccess(instruction,
4957 instruction->GetFieldType(),
4958 instruction->GetFieldIndex(),
4959 instruction->GetDexPc(),
4960 calling_convention);
4961}
4962
4963void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4964 HUnresolvedInstanceFieldSet* instruction) {
4965 FieldAccessCallingConventionX86 calling_convention;
4966 codegen_->CreateUnresolvedFieldLocationSummary(
4967 instruction, instruction->GetFieldType(), calling_convention);
4968}
4969
4970void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4971 HUnresolvedInstanceFieldSet* instruction) {
4972 FieldAccessCallingConventionX86 calling_convention;
4973 codegen_->GenerateUnresolvedFieldAccess(instruction,
4974 instruction->GetFieldType(),
4975 instruction->GetFieldIndex(),
4976 instruction->GetDexPc(),
4977 calling_convention);
4978}
4979
4980void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4981 HUnresolvedStaticFieldGet* instruction) {
4982 FieldAccessCallingConventionX86 calling_convention;
4983 codegen_->CreateUnresolvedFieldLocationSummary(
4984 instruction, instruction->GetFieldType(), calling_convention);
4985}
4986
4987void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4988 HUnresolvedStaticFieldGet* instruction) {
4989 FieldAccessCallingConventionX86 calling_convention;
4990 codegen_->GenerateUnresolvedFieldAccess(instruction,
4991 instruction->GetFieldType(),
4992 instruction->GetFieldIndex(),
4993 instruction->GetDexPc(),
4994 calling_convention);
4995}
4996
4997void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4998 HUnresolvedStaticFieldSet* instruction) {
4999 FieldAccessCallingConventionX86 calling_convention;
5000 codegen_->CreateUnresolvedFieldLocationSummary(
5001 instruction, instruction->GetFieldType(), calling_convention);
5002}
5003
5004void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5005 HUnresolvedStaticFieldSet* instruction) {
5006 FieldAccessCallingConventionX86 calling_convention;
5007 codegen_->GenerateUnresolvedFieldAccess(instruction,
5008 instruction->GetFieldType(),
5009 instruction->GetFieldIndex(),
5010 instruction->GetDexPc(),
5011 calling_convention);
5012}
5013
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005014void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005015 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5016 ? LocationSummary::kCallOnSlowPath
5017 : LocationSummary::kNoCall;
5018 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5019 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005020 ? Location::RequiresRegister()
5021 : Location::Any();
5022 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005023 if (instruction->HasUses()) {
5024 locations->SetOut(Location::SameAsFirstInput());
5025 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005026}
5027
Calin Juravle2ae48182016-03-16 14:05:09 +00005028void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5029 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005030 return;
5031 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005032 LocationSummary* locations = instruction->GetLocations();
5033 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005034
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005035 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005036 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005037}
5038
Calin Juravle2ae48182016-03-16 14:05:09 +00005039void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005040 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005041 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005042
5043 LocationSummary* locations = instruction->GetLocations();
5044 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005045
5046 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005047 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005048 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005049 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005050 } else {
5051 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005052 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005053 __ jmp(slow_path->GetEntryLabel());
5054 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055 }
5056 __ j(kEqual, slow_path->GetEntryLabel());
5057}
5058
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005059void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005060 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005061}
5062
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005063void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005064 bool object_array_get_with_read_barrier =
5065 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005066 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005067 new (GetGraph()->GetArena()) LocationSummary(instruction,
5068 object_array_get_with_read_barrier ?
5069 LocationSummary::kCallOnSlowPath :
5070 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005071 locations->SetInAt(0, Location::RequiresRegister());
5072 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005073 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5074 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5075 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005076 // The output overlaps in case of long: we don't want the low move
5077 // to overwrite the array's location. Likewise, in the case of an
5078 // object array get with read barriers enabled, we do not want the
5079 // move to overwrite the array's location, as we need it to emit
5080 // the read barrier.
5081 locations->SetOut(
5082 Location::RequiresRegister(),
5083 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5084 Location::kOutputOverlap :
5085 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005086 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005087 // We need a temporary register for the read barrier marking slow
5088 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5089 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5090 locations->AddTemp(Location::RequiresRegister());
5091 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005092}
5093
5094void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5095 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005096 Location obj_loc = locations->InAt(0);
5097 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005098 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005099 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005100
Calin Juravle77520bc2015-01-12 18:45:46 +00005101 Primitive::Type type = instruction->GetType();
5102 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005103 case Primitive::kPrimBoolean: {
5104 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005105 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005106 if (index.IsConstant()) {
5107 __ movzxb(out, Address(obj,
5108 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5109 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005110 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005111 }
5112 break;
5113 }
5114
5115 case Primitive::kPrimByte: {
5116 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005117 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005118 if (index.IsConstant()) {
5119 __ movsxb(out, Address(obj,
5120 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5121 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005122 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005123 }
5124 break;
5125 }
5126
5127 case Primitive::kPrimShort: {
5128 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005129 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005130 if (index.IsConstant()) {
5131 __ movsxw(out, Address(obj,
5132 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5133 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005134 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005135 }
5136 break;
5137 }
5138
5139 case Primitive::kPrimChar: {
5140 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005141 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005142 if (index.IsConstant()) {
5143 __ movzxw(out, Address(obj,
5144 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5145 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005146 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005147 }
5148 break;
5149 }
5150
Roland Levillain7c1559a2015-12-15 10:55:36 +00005151 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005152 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005153 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005154 if (index.IsConstant()) {
5155 __ movl(out, Address(obj,
5156 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5157 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005158 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005159 }
5160 break;
5161 }
5162
Roland Levillain7c1559a2015-12-15 10:55:36 +00005163 case Primitive::kPrimNot: {
5164 static_assert(
5165 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5166 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5167 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5168 // /* HeapReference<Object> */ out =
5169 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5170 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5171 Location temp = locations->GetTemp(0);
5172 // Note that a potential implicit null check is handled in this
5173 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5174 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5175 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5176 } else {
5177 Register out = out_loc.AsRegister<Register>();
5178 if (index.IsConstant()) {
5179 uint32_t offset =
5180 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5181 __ movl(out, Address(obj, offset));
5182 codegen_->MaybeRecordImplicitNullCheck(instruction);
5183 // If read barriers are enabled, emit read barriers other than
5184 // Baker's using a slow path (and also unpoison the loaded
5185 // reference, if heap poisoning is enabled).
5186 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5187 } else {
5188 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5189 codegen_->MaybeRecordImplicitNullCheck(instruction);
5190 // If read barriers are enabled, emit read barriers other than
5191 // Baker's using a slow path (and also unpoison the loaded
5192 // reference, if heap poisoning is enabled).
5193 codegen_->MaybeGenerateReadBarrierSlow(
5194 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5195 }
5196 }
5197 break;
5198 }
5199
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005200 case Primitive::kPrimLong: {
5201 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005202 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005203 if (index.IsConstant()) {
5204 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005205 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005206 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005207 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005208 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005209 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005210 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005211 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005212 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005213 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005214 }
5215 break;
5216 }
5217
Mark Mendell7c8d0092015-01-26 11:21:33 -05005218 case Primitive::kPrimFloat: {
5219 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005220 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005221 if (index.IsConstant()) {
5222 __ movss(out, Address(obj,
5223 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5224 } else {
5225 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5226 }
5227 break;
5228 }
5229
5230 case Primitive::kPrimDouble: {
5231 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005232 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005233 if (index.IsConstant()) {
5234 __ movsd(out, Address(obj,
5235 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5236 } else {
5237 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5238 }
5239 break;
5240 }
5241
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005242 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005243 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005244 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005245 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005246
Roland Levillain7c1559a2015-12-15 10:55:36 +00005247 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5248 // Potential implicit null checks, in the case of reference or
5249 // long arrays, are handled in the previous switch statement.
5250 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005251 codegen_->MaybeRecordImplicitNullCheck(instruction);
5252 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005253}
5254
5255void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005256 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005257
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005258 bool needs_write_barrier =
5259 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005260 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5261 bool object_array_set_with_read_barrier =
5262 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005263
Nicolas Geoffray39468442014-09-02 15:17:15 +01005264 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5265 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005266 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5267 LocationSummary::kCallOnSlowPath :
5268 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005269
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005270 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5271 || (value_type == Primitive::kPrimByte);
5272 // We need the inputs to be different than the output in case of long operation.
5273 // In case of a byte operation, the register allocator does not support multiple
5274 // inputs that die at entry with one in a specific register.
5275 locations->SetInAt(0, Location::RequiresRegister());
5276 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5277 if (is_byte_type) {
5278 // Ensure the value is in a byte register.
5279 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5280 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005281 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005283 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5284 }
5285 if (needs_write_barrier) {
5286 // Temporary registers for the write barrier.
5287 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5288 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005289 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005290 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005291}
5292
5293void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5294 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005295 Location array_loc = locations->InAt(0);
5296 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005297 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005298 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005299 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005300 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5301 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5302 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005303 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005304 bool needs_write_barrier =
5305 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005306
5307 switch (value_type) {
5308 case Primitive::kPrimBoolean:
5309 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005310 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5311 Address address = index.IsConstant()
5312 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5313 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5314 if (value.IsRegister()) {
5315 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005316 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005317 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005318 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005319 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320 break;
5321 }
5322
5323 case Primitive::kPrimShort:
5324 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005325 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5326 Address address = index.IsConstant()
5327 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5328 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5329 if (value.IsRegister()) {
5330 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005331 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005332 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005333 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005334 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335 break;
5336 }
5337
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005338 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005339 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5340 Address address = index.IsConstant()
5341 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5342 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005343
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005344 if (!value.IsRegister()) {
5345 // Just setting null.
5346 DCHECK(instruction->InputAt(2)->IsNullConstant());
5347 DCHECK(value.IsConstant()) << value;
5348 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005349 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005350 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005351 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005352 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005353 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005354
5355 DCHECK(needs_write_barrier);
5356 Register register_value = value.AsRegister<Register>();
5357 NearLabel done, not_null, do_put;
5358 SlowPathCode* slow_path = nullptr;
5359 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005360 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005361 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5362 codegen_->AddSlowPath(slow_path);
5363 if (instruction->GetValueCanBeNull()) {
5364 __ testl(register_value, register_value);
5365 __ j(kNotEqual, &not_null);
5366 __ movl(address, Immediate(0));
5367 codegen_->MaybeRecordImplicitNullCheck(instruction);
5368 __ jmp(&done);
5369 __ Bind(&not_null);
5370 }
5371
Roland Levillain0d5a2812015-11-13 10:07:31 +00005372 if (kEmitCompilerReadBarrier) {
5373 // When read barriers are enabled, the type checking
5374 // instrumentation requires two read barriers:
5375 //
5376 // __ movl(temp2, temp);
5377 // // /* HeapReference<Class> */ temp = temp->component_type_
5378 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005379 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005380 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5381 //
5382 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5383 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005384 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005385 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5386 //
5387 // __ cmpl(temp, temp2);
5388 //
5389 // However, the second read barrier may trash `temp`, as it
5390 // is a temporary register, and as such would not be saved
5391 // along with live registers before calling the runtime (nor
5392 // restored afterwards). So in this case, we bail out and
5393 // delegate the work to the array set slow path.
5394 //
5395 // TODO: Extend the register allocator to support a new
5396 // "(locally) live temp" location so as to avoid always
5397 // going into the slow path when read barriers are enabled.
5398 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005399 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005400 // /* HeapReference<Class> */ temp = array->klass_
5401 __ movl(temp, Address(array, class_offset));
5402 codegen_->MaybeRecordImplicitNullCheck(instruction);
5403 __ MaybeUnpoisonHeapReference(temp);
5404
5405 // /* HeapReference<Class> */ temp = temp->component_type_
5406 __ movl(temp, Address(temp, component_offset));
5407 // If heap poisoning is enabled, no need to unpoison `temp`
5408 // nor the object reference in `register_value->klass`, as
5409 // we are comparing two poisoned references.
5410 __ cmpl(temp, Address(register_value, class_offset));
5411
5412 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5413 __ j(kEqual, &do_put);
5414 // If heap poisoning is enabled, the `temp` reference has
5415 // not been unpoisoned yet; unpoison it now.
5416 __ MaybeUnpoisonHeapReference(temp);
5417
5418 // /* HeapReference<Class> */ temp = temp->super_class_
5419 __ movl(temp, Address(temp, super_offset));
5420 // If heap poisoning is enabled, no need to unpoison
5421 // `temp`, as we are comparing against null below.
5422 __ testl(temp, temp);
5423 __ j(kNotEqual, slow_path->GetEntryLabel());
5424 __ Bind(&do_put);
5425 } else {
5426 __ j(kNotEqual, slow_path->GetEntryLabel());
5427 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005428 }
5429 }
5430
5431 if (kPoisonHeapReferences) {
5432 __ movl(temp, register_value);
5433 __ PoisonHeapReference(temp);
5434 __ movl(address, temp);
5435 } else {
5436 __ movl(address, register_value);
5437 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005438 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005439 codegen_->MaybeRecordImplicitNullCheck(instruction);
5440 }
5441
5442 Register card = locations->GetTemp(1).AsRegister<Register>();
5443 codegen_->MarkGCCard(
5444 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5445 __ Bind(&done);
5446
5447 if (slow_path != nullptr) {
5448 __ Bind(slow_path->GetExitLabel());
5449 }
5450
5451 break;
5452 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005453
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005454 case Primitive::kPrimInt: {
5455 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5456 Address address = index.IsConstant()
5457 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5458 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5459 if (value.IsRegister()) {
5460 __ movl(address, value.AsRegister<Register>());
5461 } else {
5462 DCHECK(value.IsConstant()) << value;
5463 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5464 __ movl(address, Immediate(v));
5465 }
5466 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005467 break;
5468 }
5469
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005470 case Primitive::kPrimLong: {
5471 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005472 if (index.IsConstant()) {
5473 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005474 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005475 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005476 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005477 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005478 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005479 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005480 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005481 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005482 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005483 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005484 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005485 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005486 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005487 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005488 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005489 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005490 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005491 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005492 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005493 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005494 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005495 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005496 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005497 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005498 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005499 Immediate(High32Bits(val)));
5500 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005501 }
5502 break;
5503 }
5504
Mark Mendell7c8d0092015-01-26 11:21:33 -05005505 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005506 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5507 Address address = index.IsConstant()
5508 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5509 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005510 if (value.IsFpuRegister()) {
5511 __ movss(address, value.AsFpuRegister<XmmRegister>());
5512 } else {
5513 DCHECK(value.IsConstant());
5514 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5515 __ movl(address, Immediate(v));
5516 }
5517 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005518 break;
5519 }
5520
5521 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005522 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5523 Address address = index.IsConstant()
5524 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5525 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005526 if (value.IsFpuRegister()) {
5527 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5528 } else {
5529 DCHECK(value.IsConstant());
5530 Address address_hi = index.IsConstant() ?
5531 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5532 offset + kX86WordSize) :
5533 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5534 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5535 __ movl(address, Immediate(Low32Bits(v)));
5536 codegen_->MaybeRecordImplicitNullCheck(instruction);
5537 __ movl(address_hi, Immediate(High32Bits(v)));
5538 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005539 break;
5540 }
5541
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005542 case Primitive::kPrimVoid:
5543 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005544 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005545 }
5546}
5547
5548void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5549 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005550 locations->SetInAt(0, Location::RequiresRegister());
5551 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005552}
5553
5554void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5555 LocationSummary* locations = instruction->GetLocations();
5556 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005557 Register obj = locations->InAt(0).AsRegister<Register>();
5558 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005559 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005560 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005561}
5562
5563void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005564 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5565 ? LocationSummary::kCallOnSlowPath
5566 : LocationSummary::kNoCall;
5567 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005568 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005569 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005570 if (instruction->HasUses()) {
5571 locations->SetOut(Location::SameAsFirstInput());
5572 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005573}
5574
5575void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5576 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005577 Location index_loc = locations->InAt(0);
5578 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005579 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005580 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005581
Mark Mendell99dbd682015-04-22 16:18:52 -04005582 if (length_loc.IsConstant()) {
5583 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5584 if (index_loc.IsConstant()) {
5585 // BCE will remove the bounds check if we are guarenteed to pass.
5586 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5587 if (index < 0 || index >= length) {
5588 codegen_->AddSlowPath(slow_path);
5589 __ jmp(slow_path->GetEntryLabel());
5590 } else {
5591 // Some optimization after BCE may have generated this, and we should not
5592 // generate a bounds check if it is a valid range.
5593 }
5594 return;
5595 }
5596
5597 // We have to reverse the jump condition because the length is the constant.
5598 Register index_reg = index_loc.AsRegister<Register>();
5599 __ cmpl(index_reg, Immediate(length));
5600 codegen_->AddSlowPath(slow_path);
5601 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005602 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005603 Register length = length_loc.AsRegister<Register>();
5604 if (index_loc.IsConstant()) {
5605 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5606 __ cmpl(length, Immediate(value));
5607 } else {
5608 __ cmpl(length, index_loc.AsRegister<Register>());
5609 }
5610 codegen_->AddSlowPath(slow_path);
5611 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005612 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005613}
5614
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005615void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005616 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005617}
5618
5619void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005620 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5621}
5622
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005623void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5624 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5625}
5626
5627void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005628 HBasicBlock* block = instruction->GetBlock();
5629 if (block->GetLoopInformation() != nullptr) {
5630 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5631 // The back edge will generate the suspend check.
5632 return;
5633 }
5634 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5635 // The goto will generate the suspend check.
5636 return;
5637 }
5638 GenerateSuspendCheck(instruction, nullptr);
5639}
5640
5641void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5642 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005643 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005644 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5645 if (slow_path == nullptr) {
5646 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5647 instruction->SetSlowPath(slow_path);
5648 codegen_->AddSlowPath(slow_path);
5649 if (successor != nullptr) {
5650 DCHECK(successor->IsLoopHeader());
5651 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5652 }
5653 } else {
5654 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5655 }
5656
Roland Levillain7c1559a2015-12-15 10:55:36 +00005657 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5658 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005659 if (successor == nullptr) {
5660 __ j(kNotEqual, slow_path->GetEntryLabel());
5661 __ Bind(slow_path->GetReturnLabel());
5662 } else {
5663 __ j(kEqual, codegen_->GetLabelOf(successor));
5664 __ jmp(slow_path->GetEntryLabel());
5665 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005666}
5667
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005668X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5669 return codegen_->GetAssembler();
5670}
5671
Mark Mendell7c8d0092015-01-26 11:21:33 -05005672void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005673 ScratchRegisterScope ensure_scratch(
5674 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5675 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5676 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5677 __ movl(temp_reg, Address(ESP, src + stack_offset));
5678 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005679}
5680
5681void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005682 ScratchRegisterScope ensure_scratch(
5683 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5684 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5685 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5686 __ movl(temp_reg, Address(ESP, src + stack_offset));
5687 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5688 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5689 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005690}
5691
5692void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005693 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005694 Location source = move->GetSource();
5695 Location destination = move->GetDestination();
5696
5697 if (source.IsRegister()) {
5698 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005699 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005700 } else if (destination.IsFpuRegister()) {
5701 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005702 } else {
5703 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005704 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005705 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005706 } else if (source.IsRegisterPair()) {
5707 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5708 // Create stack space for 2 elements.
5709 __ subl(ESP, Immediate(2 * elem_size));
5710 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5711 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5712 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5713 // And remove the temporary stack space we allocated.
5714 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005715 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005716 if (destination.IsRegister()) {
5717 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5718 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005719 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005720 } else if (destination.IsRegisterPair()) {
5721 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5722 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5723 __ psrlq(src_reg, Immediate(32));
5724 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005725 } else if (destination.IsStackSlot()) {
5726 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5727 } else {
5728 DCHECK(destination.IsDoubleStackSlot());
5729 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5730 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005731 } else if (source.IsStackSlot()) {
5732 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005733 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005734 } else if (destination.IsFpuRegister()) {
5735 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005736 } else {
5737 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005738 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5739 }
5740 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005741 if (destination.IsRegisterPair()) {
5742 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5743 __ movl(destination.AsRegisterPairHigh<Register>(),
5744 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5745 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005746 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5747 } else {
5748 DCHECK(destination.IsDoubleStackSlot()) << destination;
5749 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005750 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005751 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005752 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005753 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005754 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005755 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005756 if (value == 0) {
5757 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5758 } else {
5759 __ movl(destination.AsRegister<Register>(), Immediate(value));
5760 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005761 } else {
5762 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005763 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005764 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005765 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005766 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005767 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005768 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005769 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005770 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5771 if (value == 0) {
5772 // Easy handling of 0.0.
5773 __ xorps(dest, dest);
5774 } else {
5775 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005776 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5777 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5778 __ movl(temp, Immediate(value));
5779 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005780 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005781 } else {
5782 DCHECK(destination.IsStackSlot()) << destination;
5783 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5784 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005785 } else if (constant->IsLongConstant()) {
5786 int64_t value = constant->AsLongConstant()->GetValue();
5787 int32_t low_value = Low32Bits(value);
5788 int32_t high_value = High32Bits(value);
5789 Immediate low(low_value);
5790 Immediate high(high_value);
5791 if (destination.IsDoubleStackSlot()) {
5792 __ movl(Address(ESP, destination.GetStackIndex()), low);
5793 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5794 } else {
5795 __ movl(destination.AsRegisterPairLow<Register>(), low);
5796 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5797 }
5798 } else {
5799 DCHECK(constant->IsDoubleConstant());
5800 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005801 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005802 int32_t low_value = Low32Bits(value);
5803 int32_t high_value = High32Bits(value);
5804 Immediate low(low_value);
5805 Immediate high(high_value);
5806 if (destination.IsFpuRegister()) {
5807 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5808 if (value == 0) {
5809 // Easy handling of 0.0.
5810 __ xorpd(dest, dest);
5811 } else {
5812 __ pushl(high);
5813 __ pushl(low);
5814 __ movsd(dest, Address(ESP, 0));
5815 __ addl(ESP, Immediate(8));
5816 }
5817 } else {
5818 DCHECK(destination.IsDoubleStackSlot()) << destination;
5819 __ movl(Address(ESP, destination.GetStackIndex()), low);
5820 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5821 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005822 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005823 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005824 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005825 }
5826}
5827
Mark Mendella5c19ce2015-04-01 12:51:05 -04005828void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005829 Register suggested_scratch = reg == EAX ? EBX : EAX;
5830 ScratchRegisterScope ensure_scratch(
5831 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5832
5833 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5834 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5835 __ movl(Address(ESP, mem + stack_offset), reg);
5836 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005837}
5838
Mark Mendell7c8d0092015-01-26 11:21:33 -05005839void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005840 ScratchRegisterScope ensure_scratch(
5841 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5842
5843 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5844 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5845 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5846 __ movss(Address(ESP, mem + stack_offset), reg);
5847 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005848}
5849
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005850void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005851 ScratchRegisterScope ensure_scratch1(
5852 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005853
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005854 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5855 ScratchRegisterScope ensure_scratch2(
5856 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005857
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005858 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5859 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5860 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5861 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5862 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5863 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005864}
5865
5866void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005867 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005868 Location source = move->GetSource();
5869 Location destination = move->GetDestination();
5870
5871 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005872 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5873 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5874 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5875 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5876 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005877 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005878 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005879 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005880 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005881 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5882 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005883 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5884 // Use XOR Swap algorithm to avoid a temporary.
5885 DCHECK_NE(source.reg(), destination.reg());
5886 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5887 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5888 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5889 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5890 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5891 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5892 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005893 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5894 // Take advantage of the 16 bytes in the XMM register.
5895 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5896 Address stack(ESP, destination.GetStackIndex());
5897 // Load the double into the high doubleword.
5898 __ movhpd(reg, stack);
5899
5900 // Store the low double into the destination.
5901 __ movsd(stack, reg);
5902
5903 // Move the high double to the low double.
5904 __ psrldq(reg, Immediate(8));
5905 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5906 // Take advantage of the 16 bytes in the XMM register.
5907 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5908 Address stack(ESP, source.GetStackIndex());
5909 // Load the double into the high doubleword.
5910 __ movhpd(reg, stack);
5911
5912 // Store the low double into the destination.
5913 __ movsd(stack, reg);
5914
5915 // Move the high double to the low double.
5916 __ psrldq(reg, Immediate(8));
5917 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5918 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5919 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005920 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005921 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005922 }
5923}
5924
5925void ParallelMoveResolverX86::SpillScratch(int reg) {
5926 __ pushl(static_cast<Register>(reg));
5927}
5928
5929void ParallelMoveResolverX86::RestoreScratch(int reg) {
5930 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005931}
5932
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005933void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005934 InvokeRuntimeCallingConvention calling_convention;
5935 CodeGenerator::CreateLoadClassLocationSummary(
5936 cls,
5937 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005938 Location::RegisterLocation(EAX),
5939 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005940}
5941
5942void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005943 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005944 if (cls->NeedsAccessCheck()) {
5945 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5946 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5947 cls,
5948 cls->GetDexPc(),
5949 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005950 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005951 return;
5952 }
5953
Roland Levillain0d5a2812015-11-13 10:07:31 +00005954 Location out_loc = locations->Out();
5955 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005956 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005957
Calin Juravle580b6092015-10-06 17:35:58 +01005958 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005959 DCHECK(!cls->CanCallRuntime());
5960 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005961 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5962 GenerateGcRootFieldLoad(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005963 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005964 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005965 // /* GcRoot<mirror::Class>[] */ out =
5966 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5967 __ movl(out, Address(current_method,
5968 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005969 // /* GcRoot<mirror::Class> */ out = out[type_index]
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005970 GenerateGcRootFieldLoad(
5971 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005972
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005973 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5974 DCHECK(cls->CanCallRuntime());
5975 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5976 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5977 codegen_->AddSlowPath(slow_path);
5978
5979 if (!cls->IsInDexCache()) {
5980 __ testl(out, out);
5981 __ j(kEqual, slow_path->GetEntryLabel());
5982 }
5983
5984 if (cls->MustGenerateClinitCheck()) {
5985 GenerateClassInitializationCheck(slow_path, out);
5986 } else {
5987 __ Bind(slow_path->GetExitLabel());
5988 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005989 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005990 }
5991}
5992
5993void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5994 LocationSummary* locations =
5995 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5996 locations->SetInAt(0, Location::RequiresRegister());
5997 if (check->HasUses()) {
5998 locations->SetOut(Location::SameAsFirstInput());
5999 }
6000}
6001
6002void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006003 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006004 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006005 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006006 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006007 GenerateClassInitializationCheck(slow_path,
6008 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006009}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006010
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006011void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006012 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006013 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6014 Immediate(mirror::Class::kStatusInitialized));
6015 __ j(kLess, slow_path->GetEntryLabel());
6016 __ Bind(slow_path->GetExitLabel());
6017 // No need for memory fence, thanks to the X86 memory model.
6018}
6019
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006020HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6021 HLoadString::LoadKind desired_string_load_kind) {
6022 if (kEmitCompilerReadBarrier) {
6023 switch (desired_string_load_kind) {
6024 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6025 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6026 case HLoadString::LoadKind::kBootImageAddress:
6027 // TODO: Implement for read barrier.
6028 return HLoadString::LoadKind::kDexCacheViaMethod;
6029 default:
6030 break;
6031 }
6032 }
6033 switch (desired_string_load_kind) {
6034 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6035 DCHECK(!GetCompilerOptions().GetCompilePic());
6036 break;
6037 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6038 DCHECK(GetCompilerOptions().GetCompilePic());
6039 FALLTHROUGH_INTENDED;
6040 case HLoadString::LoadKind::kDexCachePcRelative:
6041 DCHECK(!Runtime::Current()->UseJit()); // Note: boot image is also non-JIT.
6042 // We disable pc-relative load when there is an irreducible loop, as the optimization
6043 // is incompatible with it.
6044 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6045 // with irreducible loops.
6046 if (GetGraph()->HasIrreducibleLoops()) {
6047 return HLoadString::LoadKind::kDexCacheViaMethod;
6048 }
6049 break;
6050 case HLoadString::LoadKind::kBootImageAddress:
6051 break;
6052 case HLoadString::LoadKind::kDexCacheAddress:
6053 DCHECK(Runtime::Current()->UseJit());
6054 break;
6055 case HLoadString::LoadKind::kDexCacheViaMethod:
6056 break;
6057 }
6058 return desired_string_load_kind;
6059}
6060
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006061void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006062 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006063 ? LocationSummary::kCallOnSlowPath
6064 : LocationSummary::kNoCall;
6065 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006066 HLoadString::LoadKind load_kind = load->GetLoadKind();
6067 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6068 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6069 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6070 locations->SetInAt(0, Location::RequiresRegister());
6071 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006072 locations->SetOut(Location::RequiresRegister());
6073}
6074
6075void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006076 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006077 Location out_loc = locations->Out();
6078 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006079
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006080 switch (load->GetLoadKind()) {
6081 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6082 DCHECK(!kEmitCompilerReadBarrier);
6083 __ movl(out, Immediate(/* placeholder */ 0));
6084 codegen_->RecordStringPatch(load);
6085 return; // No dex cache slow path.
6086 }
6087 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6088 DCHECK(!kEmitCompilerReadBarrier);
6089 Register method_address = locations->InAt(0).AsRegister<Register>();
6090 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6091 codegen_->RecordStringPatch(load);
6092 return; // No dex cache slow path.
6093 }
6094 case HLoadString::LoadKind::kBootImageAddress: {
6095 DCHECK(!kEmitCompilerReadBarrier);
6096 DCHECK_NE(load->GetAddress(), 0u);
6097 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6098 __ movl(out, Immediate(address));
6099 codegen_->RecordSimplePatch();
6100 return; // No dex cache slow path.
6101 }
6102 case HLoadString::LoadKind::kDexCacheAddress: {
6103 DCHECK_NE(load->GetAddress(), 0u);
6104 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6105 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6106 break;
6107 }
6108 case HLoadString::LoadKind::kDexCachePcRelative: {
6109 Register base_reg = locations->InAt(0).AsRegister<Register>();
6110 uint32_t offset = load->GetDexCacheElementOffset();
6111 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
6112 GenerateGcRootFieldLoad(
6113 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6114 break;
6115 }
6116 case HLoadString::LoadKind::kDexCacheViaMethod: {
6117 Register current_method = locations->InAt(0).AsRegister<Register>();
6118
6119 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6120 GenerateGcRootFieldLoad(
6121 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6122
6123 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6124 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6125 // /* GcRoot<mirror::String> */ out = out[string_index]
6126 GenerateGcRootFieldLoad(
6127 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6128 break;
6129 }
6130 default:
6131 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6132 UNREACHABLE();
6133 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006134
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006135 if (!load->IsInDexCache()) {
6136 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6137 codegen_->AddSlowPath(slow_path);
6138 __ testl(out, out);
6139 __ j(kEqual, slow_path->GetEntryLabel());
6140 __ Bind(slow_path->GetExitLabel());
6141 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006142}
6143
David Brazdilcb1c0552015-08-04 16:22:25 +01006144static Address GetExceptionTlsAddress() {
6145 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6146}
6147
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006148void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6149 LocationSummary* locations =
6150 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6151 locations->SetOut(Location::RequiresRegister());
6152}
6153
6154void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006155 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6156}
6157
6158void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6159 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6160}
6161
6162void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6163 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006164}
6165
6166void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6167 LocationSummary* locations =
6168 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6169 InvokeRuntimeCallingConvention calling_convention;
6170 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6171}
6172
6173void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006174 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6175 instruction,
6176 instruction->GetDexPc(),
6177 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006178 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006179}
6180
Roland Levillain7c1559a2015-12-15 10:55:36 +00006181static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6182 return kEmitCompilerReadBarrier &&
6183 (kUseBakerReadBarrier ||
6184 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6185 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6186 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6187}
6188
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006189void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006190 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006191 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6192 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006193 case TypeCheckKind::kExactCheck:
6194 case TypeCheckKind::kAbstractClassCheck:
6195 case TypeCheckKind::kClassHierarchyCheck:
6196 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006197 call_kind =
6198 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006199 break;
6200 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006201 case TypeCheckKind::kUnresolvedCheck:
6202 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006203 call_kind = LocationSummary::kCallOnSlowPath;
6204 break;
6205 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006206
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006207 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006208 locations->SetInAt(0, Location::RequiresRegister());
6209 locations->SetInAt(1, Location::Any());
6210 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6211 locations->SetOut(Location::RequiresRegister());
6212 // When read barriers are enabled, we need a temporary register for
6213 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006214 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006215 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006216 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006217}
6218
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006219void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006220 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006221 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006222 Location obj_loc = locations->InAt(0);
6223 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006224 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006225 Location out_loc = locations->Out();
6226 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006227 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006228 locations->GetTemp(0) :
6229 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006230 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006231 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6232 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6233 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006234 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006235 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006236
6237 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006238 // Avoid null check if we know obj is not null.
6239 if (instruction->MustDoNullCheck()) {
6240 __ testl(obj, obj);
6241 __ j(kEqual, &zero);
6242 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006243
Roland Levillain0d5a2812015-11-13 10:07:31 +00006244 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006245 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006246
Roland Levillain7c1559a2015-12-15 10:55:36 +00006247 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006248 case TypeCheckKind::kExactCheck: {
6249 if (cls.IsRegister()) {
6250 __ cmpl(out, cls.AsRegister<Register>());
6251 } else {
6252 DCHECK(cls.IsStackSlot()) << cls;
6253 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6254 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006255
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006256 // Classes must be equal for the instanceof to succeed.
6257 __ j(kNotEqual, &zero);
6258 __ movl(out, Immediate(1));
6259 __ jmp(&done);
6260 break;
6261 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006262
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006263 case TypeCheckKind::kAbstractClassCheck: {
6264 // If the class is abstract, we eagerly fetch the super class of the
6265 // object to avoid doing a comparison we know will fail.
6266 NearLabel loop;
6267 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006268 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006269 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006270 __ testl(out, out);
6271 // If `out` is null, we use it for the result, and jump to `done`.
6272 __ j(kEqual, &done);
6273 if (cls.IsRegister()) {
6274 __ cmpl(out, cls.AsRegister<Register>());
6275 } else {
6276 DCHECK(cls.IsStackSlot()) << cls;
6277 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6278 }
6279 __ j(kNotEqual, &loop);
6280 __ movl(out, Immediate(1));
6281 if (zero.IsLinked()) {
6282 __ jmp(&done);
6283 }
6284 break;
6285 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006286
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006287 case TypeCheckKind::kClassHierarchyCheck: {
6288 // Walk over the class hierarchy to find a match.
6289 NearLabel loop, success;
6290 __ Bind(&loop);
6291 if (cls.IsRegister()) {
6292 __ cmpl(out, cls.AsRegister<Register>());
6293 } else {
6294 DCHECK(cls.IsStackSlot()) << cls;
6295 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6296 }
6297 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006298 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006299 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006300 __ testl(out, out);
6301 __ j(kNotEqual, &loop);
6302 // If `out` is null, we use it for the result, and jump to `done`.
6303 __ jmp(&done);
6304 __ Bind(&success);
6305 __ movl(out, Immediate(1));
6306 if (zero.IsLinked()) {
6307 __ jmp(&done);
6308 }
6309 break;
6310 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006311
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006313 // Do an exact check.
6314 NearLabel exact_check;
6315 if (cls.IsRegister()) {
6316 __ cmpl(out, cls.AsRegister<Register>());
6317 } else {
6318 DCHECK(cls.IsStackSlot()) << cls;
6319 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6320 }
6321 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006322 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006323 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006324 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006325 __ testl(out, out);
6326 // If `out` is null, we use it for the result, and jump to `done`.
6327 __ j(kEqual, &done);
6328 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6329 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006330 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006331 __ movl(out, Immediate(1));
6332 __ jmp(&done);
6333 break;
6334 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006335
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006336 case TypeCheckKind::kArrayCheck: {
6337 if (cls.IsRegister()) {
6338 __ cmpl(out, cls.AsRegister<Register>());
6339 } else {
6340 DCHECK(cls.IsStackSlot()) << cls;
6341 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6342 }
6343 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006344 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6345 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006346 codegen_->AddSlowPath(slow_path);
6347 __ j(kNotEqual, slow_path->GetEntryLabel());
6348 __ movl(out, Immediate(1));
6349 if (zero.IsLinked()) {
6350 __ jmp(&done);
6351 }
6352 break;
6353 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006354
Calin Juravle98893e12015-10-02 21:05:03 +01006355 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006356 case TypeCheckKind::kInterfaceCheck: {
6357 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006358 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006359 // cases.
6360 //
6361 // We cannot directly call the InstanceofNonTrivial runtime
6362 // entry point without resorting to a type checking slow path
6363 // here (i.e. by calling InvokeRuntime directly), as it would
6364 // require to assign fixed registers for the inputs of this
6365 // HInstanceOf instruction (following the runtime calling
6366 // convention), which might be cluttered by the potential first
6367 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006368 //
6369 // TODO: Introduce a new runtime entry point taking the object
6370 // to test (instead of its class) as argument, and let it deal
6371 // with the read barrier issues. This will let us refactor this
6372 // case of the `switch` code as it was previously (with a direct
6373 // call to the runtime not using a type checking slow path).
6374 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006375 DCHECK(locations->OnlyCallsOnSlowPath());
6376 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6377 /* is_fatal */ false);
6378 codegen_->AddSlowPath(slow_path);
6379 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006380 if (zero.IsLinked()) {
6381 __ jmp(&done);
6382 }
6383 break;
6384 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006385 }
6386
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006387 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006388 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006389 __ xorl(out, out);
6390 }
6391
6392 if (done.IsLinked()) {
6393 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006394 }
6395
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006396 if (slow_path != nullptr) {
6397 __ Bind(slow_path->GetExitLabel());
6398 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006399}
6400
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006401void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006402 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6403 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006404 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6405 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406 case TypeCheckKind::kExactCheck:
6407 case TypeCheckKind::kAbstractClassCheck:
6408 case TypeCheckKind::kClassHierarchyCheck:
6409 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006410 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6411 LocationSummary::kCallOnSlowPath :
6412 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006413 break;
6414 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006415 case TypeCheckKind::kUnresolvedCheck:
6416 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006417 call_kind = LocationSummary::kCallOnSlowPath;
6418 break;
6419 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006420 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6421 locations->SetInAt(0, Location::RequiresRegister());
6422 locations->SetInAt(1, Location::Any());
6423 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6424 locations->AddTemp(Location::RequiresRegister());
6425 // When read barriers are enabled, we need an additional temporary
6426 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006427 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006428 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006429 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006430}
6431
6432void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006433 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006434 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006435 Location obj_loc = locations->InAt(0);
6436 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006437 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006438 Location temp_loc = locations->GetTemp(0);
6439 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006440 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006441 locations->GetTemp(1) :
6442 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006443 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6444 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6445 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6446 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006447
Roland Levillain0d5a2812015-11-13 10:07:31 +00006448 bool is_type_check_slow_path_fatal =
6449 (type_check_kind == TypeCheckKind::kExactCheck ||
6450 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6451 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6452 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6453 !instruction->CanThrowIntoCatchBlock();
6454 SlowPathCode* type_check_slow_path =
6455 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6456 is_type_check_slow_path_fatal);
6457 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006458
Roland Levillain0d5a2812015-11-13 10:07:31 +00006459 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006460 // Avoid null check if we know obj is not null.
6461 if (instruction->MustDoNullCheck()) {
6462 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006463 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006464 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006465
Roland Levillain0d5a2812015-11-13 10:07:31 +00006466 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006467 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006468
Roland Levillain0d5a2812015-11-13 10:07:31 +00006469 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006470 case TypeCheckKind::kExactCheck:
6471 case TypeCheckKind::kArrayCheck: {
6472 if (cls.IsRegister()) {
6473 __ cmpl(temp, cls.AsRegister<Register>());
6474 } else {
6475 DCHECK(cls.IsStackSlot()) << cls;
6476 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6477 }
6478 // Jump to slow path for throwing the exception or doing a
6479 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006480 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006481 break;
6482 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006483
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006484 case TypeCheckKind::kAbstractClassCheck: {
6485 // If the class is abstract, we eagerly fetch the super class of the
6486 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006487 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006488 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006489 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006490 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006491
6492 // If the class reference currently in `temp` is not null, jump
6493 // to the `compare_classes` label to compare it with the checked
6494 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006495 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006496 __ j(kNotEqual, &compare_classes);
6497 // Otherwise, jump to the slow path to throw the exception.
6498 //
6499 // But before, move back the object's class into `temp` before
6500 // going into the slow path, as it has been overwritten in the
6501 // meantime.
6502 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006503 GenerateReferenceLoadTwoRegisters(
6504 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006505 __ jmp(type_check_slow_path->GetEntryLabel());
6506
6507 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006508 if (cls.IsRegister()) {
6509 __ cmpl(temp, cls.AsRegister<Register>());
6510 } else {
6511 DCHECK(cls.IsStackSlot()) << cls;
6512 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6513 }
6514 __ j(kNotEqual, &loop);
6515 break;
6516 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006517
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006518 case TypeCheckKind::kClassHierarchyCheck: {
6519 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006520 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006521 __ Bind(&loop);
6522 if (cls.IsRegister()) {
6523 __ cmpl(temp, cls.AsRegister<Register>());
6524 } else {
6525 DCHECK(cls.IsStackSlot()) << cls;
6526 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6527 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006528 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006529
Roland Levillain0d5a2812015-11-13 10:07:31 +00006530 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006531 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006532
6533 // If the class reference currently in `temp` is not null, jump
6534 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006535 __ testl(temp, temp);
6536 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006537 // Otherwise, jump to the slow path to throw the exception.
6538 //
6539 // But before, move back the object's class into `temp` before
6540 // going into the slow path, as it has been overwritten in the
6541 // meantime.
6542 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006543 GenerateReferenceLoadTwoRegisters(
6544 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006545 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006546 break;
6547 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006548
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006549 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006550 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006551 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006552 if (cls.IsRegister()) {
6553 __ cmpl(temp, cls.AsRegister<Register>());
6554 } else {
6555 DCHECK(cls.IsStackSlot()) << cls;
6556 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6557 }
6558 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006559
6560 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006561 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006562 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006563
6564 // If the component type is not null (i.e. the object is indeed
6565 // an array), jump to label `check_non_primitive_component_type`
6566 // to further check that this component type is not a primitive
6567 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006568 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006569 __ j(kNotEqual, &check_non_primitive_component_type);
6570 // Otherwise, jump to the slow path to throw the exception.
6571 //
6572 // But before, move back the object's class into `temp` before
6573 // going into the slow path, as it has been overwritten in the
6574 // meantime.
6575 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006576 GenerateReferenceLoadTwoRegisters(
6577 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006578 __ jmp(type_check_slow_path->GetEntryLabel());
6579
6580 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006581 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006582 __ j(kEqual, &done);
6583 // Same comment as above regarding `temp` and the slow path.
6584 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006585 GenerateReferenceLoadTwoRegisters(
6586 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006587 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006588 break;
6589 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006590
Calin Juravle98893e12015-10-02 21:05:03 +01006591 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006592 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006593 // We always go into the type check slow path for the unresolved
6594 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006595 //
6596 // We cannot directly call the CheckCast runtime entry point
6597 // without resorting to a type checking slow path here (i.e. by
6598 // calling InvokeRuntime directly), as it would require to
6599 // assign fixed registers for the inputs of this HInstanceOf
6600 // instruction (following the runtime calling convention), which
6601 // might be cluttered by the potential first read barrier
6602 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006603 //
6604 // TODO: Introduce a new runtime entry point taking the object
6605 // to test (instead of its class) as argument, and let it deal
6606 // with the read barrier issues. This will let us refactor this
6607 // case of the `switch` code as it was previously (with a direct
6608 // call to the runtime not using a type checking slow path).
6609 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006610 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006611 break;
6612 }
6613 __ Bind(&done);
6614
Roland Levillain0d5a2812015-11-13 10:07:31 +00006615 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006616}
6617
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006618void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6619 LocationSummary* locations =
6620 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6621 InvokeRuntimeCallingConvention calling_convention;
6622 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6623}
6624
6625void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006626 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6627 : QUICK_ENTRY_POINT(pUnlockObject),
6628 instruction,
6629 instruction->GetDexPc(),
6630 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006631 if (instruction->IsEnter()) {
6632 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6633 } else {
6634 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6635 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006636}
6637
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006638void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6639void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6640void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6641
6642void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6643 LocationSummary* locations =
6644 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6645 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6646 || instruction->GetResultType() == Primitive::kPrimLong);
6647 locations->SetInAt(0, Location::RequiresRegister());
6648 locations->SetInAt(1, Location::Any());
6649 locations->SetOut(Location::SameAsFirstInput());
6650}
6651
6652void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6653 HandleBitwiseOperation(instruction);
6654}
6655
6656void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6657 HandleBitwiseOperation(instruction);
6658}
6659
6660void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6661 HandleBitwiseOperation(instruction);
6662}
6663
6664void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6665 LocationSummary* locations = instruction->GetLocations();
6666 Location first = locations->InAt(0);
6667 Location second = locations->InAt(1);
6668 DCHECK(first.Equals(locations->Out()));
6669
6670 if (instruction->GetResultType() == Primitive::kPrimInt) {
6671 if (second.IsRegister()) {
6672 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006673 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006674 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006675 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006676 } else {
6677 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006678 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006679 }
6680 } else if (second.IsConstant()) {
6681 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006682 __ andl(first.AsRegister<Register>(),
6683 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006684 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006685 __ orl(first.AsRegister<Register>(),
6686 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006687 } else {
6688 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006689 __ xorl(first.AsRegister<Register>(),
6690 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006691 }
6692 } else {
6693 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006694 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006695 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006696 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006697 } else {
6698 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006699 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006700 }
6701 }
6702 } else {
6703 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6704 if (second.IsRegisterPair()) {
6705 if (instruction->IsAnd()) {
6706 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6707 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6708 } else if (instruction->IsOr()) {
6709 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6710 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6711 } else {
6712 DCHECK(instruction->IsXor());
6713 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6714 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6715 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006716 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006717 if (instruction->IsAnd()) {
6718 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6719 __ andl(first.AsRegisterPairHigh<Register>(),
6720 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6721 } else if (instruction->IsOr()) {
6722 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6723 __ orl(first.AsRegisterPairHigh<Register>(),
6724 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6725 } else {
6726 DCHECK(instruction->IsXor());
6727 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6728 __ xorl(first.AsRegisterPairHigh<Register>(),
6729 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6730 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006731 } else {
6732 DCHECK(second.IsConstant()) << second;
6733 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006734 int32_t low_value = Low32Bits(value);
6735 int32_t high_value = High32Bits(value);
6736 Immediate low(low_value);
6737 Immediate high(high_value);
6738 Register first_low = first.AsRegisterPairLow<Register>();
6739 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006740 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006741 if (low_value == 0) {
6742 __ xorl(first_low, first_low);
6743 } else if (low_value != -1) {
6744 __ andl(first_low, low);
6745 }
6746 if (high_value == 0) {
6747 __ xorl(first_high, first_high);
6748 } else if (high_value != -1) {
6749 __ andl(first_high, high);
6750 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006751 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006752 if (low_value != 0) {
6753 __ orl(first_low, low);
6754 }
6755 if (high_value != 0) {
6756 __ orl(first_high, high);
6757 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006758 } else {
6759 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006760 if (low_value != 0) {
6761 __ xorl(first_low, low);
6762 }
6763 if (high_value != 0) {
6764 __ xorl(first_high, high);
6765 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006766 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006767 }
6768 }
6769}
6770
Roland Levillain7c1559a2015-12-15 10:55:36 +00006771void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6772 Location out,
6773 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006774 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006775 Register out_reg = out.AsRegister<Register>();
6776 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006777 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006778 if (kUseBakerReadBarrier) {
6779 // Load with fast path based Baker's read barrier.
6780 // /* HeapReference<Object> */ out = *(out + offset)
6781 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006782 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006783 } else {
6784 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006785 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006786 // in the following move operation, as we will need it for the
6787 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006788 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006789 // /* HeapReference<Object> */ out = *(out + offset)
6790 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006791 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006792 }
6793 } else {
6794 // Plain load with no read barrier.
6795 // /* HeapReference<Object> */ out = *(out + offset)
6796 __ movl(out_reg, Address(out_reg, offset));
6797 __ MaybeUnpoisonHeapReference(out_reg);
6798 }
6799}
6800
6801void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6802 Location out,
6803 Location obj,
6804 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006805 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006806 Register out_reg = out.AsRegister<Register>();
6807 Register obj_reg = obj.AsRegister<Register>();
6808 if (kEmitCompilerReadBarrier) {
6809 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006810 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006811 // Load with fast path based Baker's read barrier.
6812 // /* HeapReference<Object> */ out = *(obj + offset)
6813 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006814 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006815 } else {
6816 // Load with slow path based read barrier.
6817 // /* HeapReference<Object> */ out = *(obj + offset)
6818 __ movl(out_reg, Address(obj_reg, offset));
6819 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6820 }
6821 } else {
6822 // Plain load with no read barrier.
6823 // /* HeapReference<Object> */ out = *(obj + offset)
6824 __ movl(out_reg, Address(obj_reg, offset));
6825 __ MaybeUnpoisonHeapReference(out_reg);
6826 }
6827}
6828
6829void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6830 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006831 const Address& address,
6832 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006833 Register root_reg = root.AsRegister<Register>();
6834 if (kEmitCompilerReadBarrier) {
6835 if (kUseBakerReadBarrier) {
6836 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6837 // Baker's read barrier are used:
6838 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006839 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006840 // if (Thread::Current()->GetIsGcMarking()) {
6841 // root = ReadBarrier::Mark(root)
6842 // }
6843
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006844 // /* GcRoot<mirror::Object> */ root = *address
6845 __ movl(root_reg, address);
6846 if (fixup_label != nullptr) {
6847 __ Bind(fixup_label);
6848 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006849 static_assert(
6850 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6851 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6852 "have different sizes.");
6853 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6854 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6855 "have different sizes.");
6856
6857 // Slow path used to mark the GC root `root`.
6858 SlowPathCode* slow_path =
6859 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6860 codegen_->AddSlowPath(slow_path);
6861
6862 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6863 Immediate(0));
6864 __ j(kNotEqual, slow_path->GetEntryLabel());
6865 __ Bind(slow_path->GetExitLabel());
6866 } else {
6867 // GC root loaded through a slow path for read barriers other
6868 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006869 // /* GcRoot<mirror::Object>* */ root = address
6870 __ leal(root_reg, address);
6871 if (fixup_label != nullptr) {
6872 __ Bind(fixup_label);
6873 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006874 // /* mirror::Object* */ root = root->Read()
6875 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6876 }
6877 } else {
6878 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006879 // /* GcRoot<mirror::Object> */ root = *address
6880 __ movl(root_reg, address);
6881 if (fixup_label != nullptr) {
6882 __ Bind(fixup_label);
6883 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006884 // Note that GC roots are not affected by heap poisoning, thus we
6885 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006886 }
6887}
6888
6889void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6890 Location ref,
6891 Register obj,
6892 uint32_t offset,
6893 Location temp,
6894 bool needs_null_check) {
6895 DCHECK(kEmitCompilerReadBarrier);
6896 DCHECK(kUseBakerReadBarrier);
6897
6898 // /* HeapReference<Object> */ ref = *(obj + offset)
6899 Address src(obj, offset);
6900 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6901}
6902
6903void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6904 Location ref,
6905 Register obj,
6906 uint32_t data_offset,
6907 Location index,
6908 Location temp,
6909 bool needs_null_check) {
6910 DCHECK(kEmitCompilerReadBarrier);
6911 DCHECK(kUseBakerReadBarrier);
6912
6913 // /* HeapReference<Object> */ ref =
6914 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6915 Address src = index.IsConstant() ?
6916 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6917 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6918 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6919}
6920
6921void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6922 Location ref,
6923 Register obj,
6924 const Address& src,
6925 Location temp,
6926 bool needs_null_check) {
6927 DCHECK(kEmitCompilerReadBarrier);
6928 DCHECK(kUseBakerReadBarrier);
6929
6930 // In slow path based read barriers, the read barrier call is
6931 // inserted after the original load. However, in fast path based
6932 // Baker's read barriers, we need to perform the load of
6933 // mirror::Object::monitor_ *before* the original reference load.
6934 // This load-load ordering is required by the read barrier.
6935 // The fast path/slow path (for Baker's algorithm) should look like:
6936 //
6937 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6938 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6939 // HeapReference<Object> ref = *src; // Original reference load.
6940 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6941 // if (is_gray) {
6942 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6943 // }
6944 //
6945 // Note: the original implementation in ReadBarrier::Barrier is
6946 // slightly more complex as:
6947 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006948 // the high-bits of rb_state, which are expected to be all zeroes
6949 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6950 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006951 // - it performs additional checks that we do not do here for
6952 // performance reasons.
6953
6954 Register ref_reg = ref.AsRegister<Register>();
6955 Register temp_reg = temp.AsRegister<Register>();
6956 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6957
6958 // /* int32_t */ monitor = obj->monitor_
6959 __ movl(temp_reg, Address(obj, monitor_offset));
6960 if (needs_null_check) {
6961 MaybeRecordImplicitNullCheck(instruction);
6962 }
6963 // /* LockWord */ lock_word = LockWord(monitor)
6964 static_assert(sizeof(LockWord) == sizeof(int32_t),
6965 "art::LockWord and int32_t have different sizes.");
6966 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6967 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6968 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6969 static_assert(
6970 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6971 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6972
6973 // Load fence to prevent load-load reordering.
6974 // Note that this is a no-op, thanks to the x86 memory model.
6975 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6976
6977 // The actual reference load.
6978 // /* HeapReference<Object> */ ref = *src
6979 __ movl(ref_reg, src);
6980
6981 // Object* ref = ref_addr->AsMirrorPtr()
6982 __ MaybeUnpoisonHeapReference(ref_reg);
6983
6984 // Slow path used to mark the object `ref` when it is gray.
6985 SlowPathCode* slow_path =
6986 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6987 AddSlowPath(slow_path);
6988
6989 // if (rb_state == ReadBarrier::gray_ptr_)
6990 // ref = ReadBarrier::Mark(ref);
6991 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6992 __ j(kEqual, slow_path->GetEntryLabel());
6993 __ Bind(slow_path->GetExitLabel());
6994}
6995
6996void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6997 Location out,
6998 Location ref,
6999 Location obj,
7000 uint32_t offset,
7001 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007002 DCHECK(kEmitCompilerReadBarrier);
7003
Roland Levillain7c1559a2015-12-15 10:55:36 +00007004 // Insert a slow path based read barrier *after* the reference load.
7005 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007006 // If heap poisoning is enabled, the unpoisoning of the loaded
7007 // reference will be carried out by the runtime within the slow
7008 // path.
7009 //
7010 // Note that `ref` currently does not get unpoisoned (when heap
7011 // poisoning is enabled), which is alright as the `ref` argument is
7012 // not used by the artReadBarrierSlow entry point.
7013 //
7014 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7015 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7016 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7017 AddSlowPath(slow_path);
7018
Roland Levillain0d5a2812015-11-13 10:07:31 +00007019 __ jmp(slow_path->GetEntryLabel());
7020 __ Bind(slow_path->GetExitLabel());
7021}
7022
Roland Levillain7c1559a2015-12-15 10:55:36 +00007023void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7024 Location out,
7025 Location ref,
7026 Location obj,
7027 uint32_t offset,
7028 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007029 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007030 // Baker's read barriers shall be handled by the fast path
7031 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7032 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007033 // If heap poisoning is enabled, unpoisoning will be taken care of
7034 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007035 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007036 } else if (kPoisonHeapReferences) {
7037 __ UnpoisonHeapReference(out.AsRegister<Register>());
7038 }
7039}
7040
Roland Levillain7c1559a2015-12-15 10:55:36 +00007041void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7042 Location out,
7043 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007044 DCHECK(kEmitCompilerReadBarrier);
7045
Roland Levillain7c1559a2015-12-15 10:55:36 +00007046 // Insert a slow path based read barrier *after* the GC root load.
7047 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007048 // Note that GC roots are not affected by heap poisoning, so we do
7049 // not need to do anything special for this here.
7050 SlowPathCode* slow_path =
7051 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7052 AddSlowPath(slow_path);
7053
Roland Levillain0d5a2812015-11-13 10:07:31 +00007054 __ jmp(slow_path->GetEntryLabel());
7055 __ Bind(slow_path->GetExitLabel());
7056}
7057
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007058void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007059 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007060 LOG(FATAL) << "Unreachable";
7061}
7062
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007063void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007064 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007065 LOG(FATAL) << "Unreachable";
7066}
7067
Mark Mendellfe57faa2015-09-18 09:26:15 -04007068// Simple implementation of packed switch - generate cascaded compare/jumps.
7069void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7070 LocationSummary* locations =
7071 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7072 locations->SetInAt(0, Location::RequiresRegister());
7073}
7074
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007075void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7076 int32_t lower_bound,
7077 uint32_t num_entries,
7078 HBasicBlock* switch_block,
7079 HBasicBlock* default_block) {
7080 // Figure out the correct compare values and jump conditions.
7081 // Handle the first compare/branch as a special case because it might
7082 // jump to the default case.
7083 DCHECK_GT(num_entries, 2u);
7084 Condition first_condition;
7085 uint32_t index;
7086 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7087 if (lower_bound != 0) {
7088 first_condition = kLess;
7089 __ cmpl(value_reg, Immediate(lower_bound));
7090 __ j(first_condition, codegen_->GetLabelOf(default_block));
7091 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007092
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007093 index = 1;
7094 } else {
7095 // Handle all the compare/jumps below.
7096 first_condition = kBelow;
7097 index = 0;
7098 }
7099
7100 // Handle the rest of the compare/jumps.
7101 for (; index + 1 < num_entries; index += 2) {
7102 int32_t compare_to_value = lower_bound + index + 1;
7103 __ cmpl(value_reg, Immediate(compare_to_value));
7104 // Jump to successors[index] if value < case_value[index].
7105 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7106 // Jump to successors[index + 1] if value == case_value[index + 1].
7107 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7108 }
7109
7110 if (index != num_entries) {
7111 // There are an odd number of entries. Handle the last one.
7112 DCHECK_EQ(index + 1, num_entries);
7113 __ cmpl(value_reg, Immediate(lower_bound + index));
7114 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007115 }
7116
7117 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007118 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7119 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007120 }
7121}
7122
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007123void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7124 int32_t lower_bound = switch_instr->GetStartValue();
7125 uint32_t num_entries = switch_instr->GetNumEntries();
7126 LocationSummary* locations = switch_instr->GetLocations();
7127 Register value_reg = locations->InAt(0).AsRegister<Register>();
7128
7129 GenPackedSwitchWithCompares(value_reg,
7130 lower_bound,
7131 num_entries,
7132 switch_instr->GetBlock(),
7133 switch_instr->GetDefaultBlock());
7134}
7135
Mark Mendell805b3b52015-09-18 14:10:29 -04007136void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7137 LocationSummary* locations =
7138 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7139 locations->SetInAt(0, Location::RequiresRegister());
7140
7141 // Constant area pointer.
7142 locations->SetInAt(1, Location::RequiresRegister());
7143
7144 // And the temporary we need.
7145 locations->AddTemp(Location::RequiresRegister());
7146}
7147
7148void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7149 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007150 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007151 LocationSummary* locations = switch_instr->GetLocations();
7152 Register value_reg = locations->InAt(0).AsRegister<Register>();
7153 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7154
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007155 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7156 GenPackedSwitchWithCompares(value_reg,
7157 lower_bound,
7158 num_entries,
7159 switch_instr->GetBlock(),
7160 default_block);
7161 return;
7162 }
7163
Mark Mendell805b3b52015-09-18 14:10:29 -04007164 // Optimizing has a jump area.
7165 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7166 Register constant_area = locations->InAt(1).AsRegister<Register>();
7167
7168 // Remove the bias, if needed.
7169 if (lower_bound != 0) {
7170 __ leal(temp_reg, Address(value_reg, -lower_bound));
7171 value_reg = temp_reg;
7172 }
7173
7174 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007175 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007176 __ cmpl(value_reg, Immediate(num_entries - 1));
7177 __ j(kAbove, codegen_->GetLabelOf(default_block));
7178
7179 // We are in the range of the table.
7180 // Load (target-constant_area) from the jump table, indexing by the value.
7181 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7182
7183 // Compute the actual target address by adding in constant_area.
7184 __ addl(temp_reg, constant_area);
7185
7186 // And jump.
7187 __ jmp(temp_reg);
7188}
7189
Mark Mendell0616ae02015-04-17 12:49:27 -04007190void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7191 HX86ComputeBaseMethodAddress* insn) {
7192 LocationSummary* locations =
7193 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7194 locations->SetOut(Location::RequiresRegister());
7195}
7196
7197void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7198 HX86ComputeBaseMethodAddress* insn) {
7199 LocationSummary* locations = insn->GetLocations();
7200 Register reg = locations->Out().AsRegister<Register>();
7201
7202 // Generate call to next instruction.
7203 Label next_instruction;
7204 __ call(&next_instruction);
7205 __ Bind(&next_instruction);
7206
7207 // Remember this offset for later use with constant area.
7208 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7209
7210 // Grab the return address off the stack.
7211 __ popl(reg);
7212}
7213
7214void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7215 HX86LoadFromConstantTable* insn) {
7216 LocationSummary* locations =
7217 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7218
7219 locations->SetInAt(0, Location::RequiresRegister());
7220 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7221
7222 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007223 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007224 return;
7225 }
7226
7227 switch (insn->GetType()) {
7228 case Primitive::kPrimFloat:
7229 case Primitive::kPrimDouble:
7230 locations->SetOut(Location::RequiresFpuRegister());
7231 break;
7232
7233 case Primitive::kPrimInt:
7234 locations->SetOut(Location::RequiresRegister());
7235 break;
7236
7237 default:
7238 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7239 }
7240}
7241
7242void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007243 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007244 return;
7245 }
7246
7247 LocationSummary* locations = insn->GetLocations();
7248 Location out = locations->Out();
7249 Register const_area = locations->InAt(0).AsRegister<Register>();
7250 HConstant *value = insn->GetConstant();
7251
7252 switch (insn->GetType()) {
7253 case Primitive::kPrimFloat:
7254 __ movss(out.AsFpuRegister<XmmRegister>(),
7255 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7256 break;
7257
7258 case Primitive::kPrimDouble:
7259 __ movsd(out.AsFpuRegister<XmmRegister>(),
7260 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7261 break;
7262
7263 case Primitive::kPrimInt:
7264 __ movl(out.AsRegister<Register>(),
7265 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7266 break;
7267
7268 default:
7269 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7270 }
7271}
7272
Mark Mendell0616ae02015-04-17 12:49:27 -04007273/**
7274 * Class to handle late fixup of offsets into constant area.
7275 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007276class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007277 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007278 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7279 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7280
7281 protected:
7282 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7283
7284 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007285
7286 private:
7287 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7288 // Patch the correct offset for the instruction. The place to patch is the
7289 // last 4 bytes of the instruction.
7290 // The value to patch is the distance from the offset in the constant area
7291 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007292 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7293 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007294
7295 // Patch in the right value.
7296 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7297 }
7298
Mark Mendell0616ae02015-04-17 12:49:27 -04007299 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007300 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007301};
7302
Mark Mendell805b3b52015-09-18 14:10:29 -04007303/**
7304 * Class to handle late fixup of offsets to a jump table that will be created in the
7305 * constant area.
7306 */
7307class JumpTableRIPFixup : public RIPFixup {
7308 public:
7309 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7310 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7311
7312 void CreateJumpTable() {
7313 X86Assembler* assembler = codegen_->GetAssembler();
7314
7315 // Ensure that the reference to the jump table has the correct offset.
7316 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7317 SetOffset(offset_in_constant_table);
7318
7319 // The label values in the jump table are computed relative to the
7320 // instruction addressing the constant area.
7321 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7322
7323 // Populate the jump table with the correct values for the jump table.
7324 int32_t num_entries = switch_instr_->GetNumEntries();
7325 HBasicBlock* block = switch_instr_->GetBlock();
7326 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7327 // The value that we want is the target offset - the position of the table.
7328 for (int32_t i = 0; i < num_entries; i++) {
7329 HBasicBlock* b = successors[i];
7330 Label* l = codegen_->GetLabelOf(b);
7331 DCHECK(l->IsBound());
7332 int32_t offset_to_block = l->Position() - relative_offset;
7333 assembler->AppendInt32(offset_to_block);
7334 }
7335 }
7336
7337 private:
7338 const HX86PackedSwitch* switch_instr_;
7339};
7340
7341void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7342 // Generate the constant area if needed.
7343 X86Assembler* assembler = GetAssembler();
7344 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7345 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7346 // byte values.
7347 assembler->Align(4, 0);
7348 constant_area_start_ = assembler->CodeSize();
7349
7350 // Populate any jump tables.
7351 for (auto jump_table : fixups_to_jump_tables_) {
7352 jump_table->CreateJumpTable();
7353 }
7354
7355 // And now add the constant area to the generated code.
7356 assembler->AddConstantArea();
7357 }
7358
7359 // And finish up.
7360 CodeGenerator::Finalize(allocator);
7361}
7362
Mark Mendell0616ae02015-04-17 12:49:27 -04007363Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7364 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7365 return Address(reg, kDummy32BitOffset, fixup);
7366}
7367
7368Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7369 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7370 return Address(reg, kDummy32BitOffset, fixup);
7371}
7372
7373Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7374 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7375 return Address(reg, kDummy32BitOffset, fixup);
7376}
7377
7378Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7379 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7380 return Address(reg, kDummy32BitOffset, fixup);
7381}
7382
Aart Bika19616e2016-02-01 18:57:58 -08007383void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7384 if (value == 0) {
7385 __ xorl(dest, dest);
7386 } else {
7387 __ movl(dest, Immediate(value));
7388 }
7389}
7390
7391void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7392 if (value == 0) {
7393 __ testl(dest, dest);
7394 } else {
7395 __ cmpl(dest, Immediate(value));
7396 }
7397}
7398
Mark Mendell805b3b52015-09-18 14:10:29 -04007399Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7400 Register reg,
7401 Register value) {
7402 // Create a fixup to be used to create and address the jump table.
7403 JumpTableRIPFixup* table_fixup =
7404 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7405
7406 // We have to populate the jump tables.
7407 fixups_to_jump_tables_.push_back(table_fixup);
7408
7409 // We want a scaled address, as we are extracting the correct offset from the table.
7410 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7411}
7412
Andreas Gampe85b62f22015-09-09 13:15:38 -07007413// TODO: target as memory.
7414void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7415 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007416 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007417 return;
7418 }
7419
7420 DCHECK_NE(type, Primitive::kPrimVoid);
7421
7422 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7423 if (target.Equals(return_loc)) {
7424 return;
7425 }
7426
7427 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7428 // with the else branch.
7429 if (type == Primitive::kPrimLong) {
7430 HParallelMove parallel_move(GetGraph()->GetArena());
7431 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7432 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7433 GetMoveResolver()->EmitNativeCode(&parallel_move);
7434 } else {
7435 // Let the parallel move resolver take care of all of this.
7436 HParallelMove parallel_move(GetGraph()->GetArena());
7437 parallel_move.AddMove(return_loc, target, type, nullptr);
7438 GetMoveResolver()->EmitNativeCode(&parallel_move);
7439 }
7440}
7441
Roland Levillain4d027112015-07-01 15:41:14 +01007442#undef __
7443
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007444} // namespace x86
7445} // namespace art