blob: fce9df408c017050431055431116c94e64e8abb7 [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)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400802 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000803 // Use a fake return address register to mimic Quick.
804 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100805}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100806
David Brazdil58282f42016-01-14 12:45:10 +0000807void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100808 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100809 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100810
811 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100812 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100813
Calin Juravle34bacdf2014-10-07 20:23:36 +0100814 UpdateBlockedPairRegisters();
815}
816
817void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
818 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
819 X86ManagedRegister current =
820 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
821 if (blocked_core_registers_[current.AsRegisterPairLow()]
822 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
823 blocked_register_pairs_[i] = true;
824 }
825 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100826}
827
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100828InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800829 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100830 assembler_(codegen->GetAssembler()),
831 codegen_(codegen) {}
832
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100833static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100834 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100835}
836
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000837void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100838 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000839 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000840 bool skip_overflow_check =
841 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000842 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000843
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000844 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100845 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100846 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100847 }
848
Mark Mendell5f874182015-03-04 15:42:45 -0500849 if (HasEmptyFrame()) {
850 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000851 }
Mark Mendell5f874182015-03-04 15:42:45 -0500852
853 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
854 Register reg = kCoreCalleeSaves[i];
855 if (allocated_registers_.ContainsCoreRegister(reg)) {
856 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100857 __ cfi().AdjustCFAOffset(kX86WordSize);
858 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500859 }
860 }
861
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100862 int adjust = GetFrameSize() - FrameEntrySpillSize();
863 __ subl(ESP, Immediate(adjust));
864 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100865 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000866}
867
868void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100869 __ cfi().RememberState();
870 if (!HasEmptyFrame()) {
871 int adjust = GetFrameSize() - FrameEntrySpillSize();
872 __ addl(ESP, Immediate(adjust));
873 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500874
David Srbeckyc34dc932015-04-12 09:27:43 +0100875 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
876 Register reg = kCoreCalleeSaves[i];
877 if (allocated_registers_.ContainsCoreRegister(reg)) {
878 __ popl(reg);
879 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
880 __ cfi().Restore(DWARFReg(reg));
881 }
Mark Mendell5f874182015-03-04 15:42:45 -0500882 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000883 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100884 __ ret();
885 __ cfi().RestoreState();
886 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000887}
888
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100889void CodeGeneratorX86::Bind(HBasicBlock* block) {
890 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000891}
892
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100893Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
894 switch (load->GetType()) {
895 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100896 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100897 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100898
899 case Primitive::kPrimInt:
900 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100901 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100902 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100903
904 case Primitive::kPrimBoolean:
905 case Primitive::kPrimByte:
906 case Primitive::kPrimChar:
907 case Primitive::kPrimShort:
908 case Primitive::kPrimVoid:
909 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700910 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100911 }
912
913 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700914 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100915}
916
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100917Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
918 switch (type) {
919 case Primitive::kPrimBoolean:
920 case Primitive::kPrimByte:
921 case Primitive::kPrimChar:
922 case Primitive::kPrimShort:
923 case Primitive::kPrimInt:
924 case Primitive::kPrimNot:
925 return Location::RegisterLocation(EAX);
926
927 case Primitive::kPrimLong:
928 return Location::RegisterPairLocation(EAX, EDX);
929
930 case Primitive::kPrimVoid:
931 return Location::NoLocation();
932
933 case Primitive::kPrimDouble:
934 case Primitive::kPrimFloat:
935 return Location::FpuRegisterLocation(XMM0);
936 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100937
938 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100939}
940
941Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
942 return Location::RegisterLocation(kMethodRegisterArgument);
943}
944
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100945Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100946 switch (type) {
947 case Primitive::kPrimBoolean:
948 case Primitive::kPrimByte:
949 case Primitive::kPrimChar:
950 case Primitive::kPrimShort:
951 case Primitive::kPrimInt:
952 case Primitive::kPrimNot: {
953 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000954 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100955 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100956 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100957 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000958 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100959 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100960 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100961
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000962 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100963 uint32_t index = gp_index_;
964 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000965 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100966 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100967 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
968 calling_convention.GetRegisterPairAt(index));
969 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100970 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000971 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
972 }
973 }
974
975 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100976 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000977 stack_index_++;
978 if (index < calling_convention.GetNumberOfFpuRegisters()) {
979 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
980 } else {
981 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
982 }
983 }
984
985 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100986 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000987 stack_index_ += 2;
988 if (index < calling_convention.GetNumberOfFpuRegisters()) {
989 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
990 } else {
991 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100992 }
993 }
994
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100995 case Primitive::kPrimVoid:
996 LOG(FATAL) << "Unexpected parameter type " << type;
997 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100998 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000999 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001000}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001001
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002void CodeGeneratorX86::Move32(Location destination, Location source) {
1003 if (source.Equals(destination)) {
1004 return;
1005 }
1006 if (destination.IsRegister()) {
1007 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001008 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001009 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001010 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001011 } else {
1012 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001013 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001014 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001015 } else if (destination.IsFpuRegister()) {
1016 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001017 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001018 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001019 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001020 } else {
1021 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001022 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001023 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001024 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001025 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001026 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001027 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001028 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001029 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001030 } else if (source.IsConstant()) {
1031 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001032 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001033 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001034 } else {
1035 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001036 __ pushl(Address(ESP, source.GetStackIndex()));
1037 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001038 }
1039 }
1040}
1041
1042void CodeGeneratorX86::Move64(Location destination, Location source) {
1043 if (source.Equals(destination)) {
1044 return;
1045 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001046 if (destination.IsRegisterPair()) {
1047 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001048 EmitParallelMoves(
1049 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1050 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001051 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001052 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001053 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1054 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001055 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001056 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1057 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1058 __ psrlq(src_reg, Immediate(32));
1059 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001060 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001061 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001062 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001063 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1064 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001065 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1066 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001067 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001068 if (source.IsFpuRegister()) {
1069 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1070 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001071 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001072 } else if (source.IsRegisterPair()) {
1073 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1074 // Create stack space for 2 elements.
1075 __ subl(ESP, Immediate(2 * elem_size));
1076 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1077 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1078 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1079 // And remove the temporary stack space we allocated.
1080 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001081 } else {
1082 LOG(FATAL) << "Unimplemented";
1083 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001084 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001085 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001086 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001087 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001088 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001089 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001090 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001091 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001092 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001093 } else if (source.IsConstant()) {
1094 HConstant* constant = source.GetConstant();
1095 int64_t value;
1096 if (constant->IsLongConstant()) {
1097 value = constant->AsLongConstant()->GetValue();
1098 } else {
1099 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001100 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001101 }
1102 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1103 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001104 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001105 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001106 EmitParallelMoves(
1107 Location::StackSlot(source.GetStackIndex()),
1108 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001109 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001110 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001111 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1112 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001113 }
1114 }
1115}
1116
Calin Juravle175dc732015-08-25 15:42:32 +01001117void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1118 DCHECK(location.IsRegister());
1119 __ movl(location.AsRegister<Register>(), Immediate(value));
1120}
1121
Calin Juravlee460d1d2015-09-29 04:52:17 +01001122void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001123 HParallelMove move(GetGraph()->GetArena());
1124 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1125 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1126 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001127 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001128 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001129 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001130 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001131}
1132
1133void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1134 if (location.IsRegister()) {
1135 locations->AddTemp(location);
1136 } else if (location.IsRegisterPair()) {
1137 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1138 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1139 } else {
1140 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1141 }
1142}
1143
David Brazdilfc6a86a2015-06-26 10:33:45 +00001144void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001145 DCHECK(!successor->IsExitBlock());
1146
1147 HBasicBlock* block = got->GetBlock();
1148 HInstruction* previous = got->GetPrevious();
1149
1150 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001151 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001152 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1153 return;
1154 }
1155
1156 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1157 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1158 }
1159 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001160 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001161 }
1162}
1163
David Brazdilfc6a86a2015-06-26 10:33:45 +00001164void LocationsBuilderX86::VisitGoto(HGoto* got) {
1165 got->SetLocations(nullptr);
1166}
1167
1168void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1169 HandleGoto(got, got->GetSuccessor());
1170}
1171
1172void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1173 try_boundary->SetLocations(nullptr);
1174}
1175
1176void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1177 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1178 if (!successor->IsExitBlock()) {
1179 HandleGoto(try_boundary, successor);
1180 }
1181}
1182
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001183void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001184 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001185}
1186
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001187void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001188}
1189
Mark Mendell152408f2015-12-31 12:28:50 -05001190template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001191void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001192 LabelType* true_label,
1193 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001194 if (cond->IsFPConditionTrueIfNaN()) {
1195 __ j(kUnordered, true_label);
1196 } else if (cond->IsFPConditionFalseIfNaN()) {
1197 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001198 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001199 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001200}
1201
Mark Mendell152408f2015-12-31 12:28:50 -05001202template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001203void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001204 LabelType* true_label,
1205 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001206 LocationSummary* locations = cond->GetLocations();
1207 Location left = locations->InAt(0);
1208 Location right = locations->InAt(1);
1209 IfCondition if_cond = cond->GetCondition();
1210
Mark Mendellc4701932015-04-10 13:18:51 -04001211 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001212 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001213 IfCondition true_high_cond = if_cond;
1214 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001215 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001216
1217 // Set the conditions for the test, remembering that == needs to be
1218 // decided using the low words.
1219 switch (if_cond) {
1220 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001221 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001222 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001223 break;
1224 case kCondLT:
1225 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001226 break;
1227 case kCondLE:
1228 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001229 break;
1230 case kCondGT:
1231 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001232 break;
1233 case kCondGE:
1234 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001235 break;
Aart Bike9f37602015-10-09 11:15:55 -07001236 case kCondB:
1237 false_high_cond = kCondA;
1238 break;
1239 case kCondBE:
1240 true_high_cond = kCondB;
1241 break;
1242 case kCondA:
1243 false_high_cond = kCondB;
1244 break;
1245 case kCondAE:
1246 true_high_cond = kCondA;
1247 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001248 }
1249
1250 if (right.IsConstant()) {
1251 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001252 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001253 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001254
Aart Bika19616e2016-02-01 18:57:58 -08001255 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001256 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001257 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001258 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001259 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001260 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001261 __ j(X86Condition(true_high_cond), true_label);
1262 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001263 }
1264 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001265 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001266 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001267 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001268 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001269
1270 __ cmpl(left_high, right_high);
1271 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001272 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001273 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001274 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001275 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001276 __ j(X86Condition(true_high_cond), true_label);
1277 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001278 }
1279 // Must be equal high, so compare the lows.
1280 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001281 } else {
1282 DCHECK(right.IsDoubleStackSlot());
1283 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1284 if (if_cond == kCondNE) {
1285 __ j(X86Condition(true_high_cond), true_label);
1286 } else if (if_cond == kCondEQ) {
1287 __ j(X86Condition(false_high_cond), false_label);
1288 } else {
1289 __ j(X86Condition(true_high_cond), true_label);
1290 __ j(X86Condition(false_high_cond), false_label);
1291 }
1292 // Must be equal high, so compare the lows.
1293 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001294 }
1295 // The last comparison might be unsigned.
1296 __ j(final_condition, true_label);
1297}
1298
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001299void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1300 Location rhs,
1301 HInstruction* insn,
1302 bool is_double) {
1303 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1304 if (is_double) {
1305 if (rhs.IsFpuRegister()) {
1306 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1307 } else if (const_area != nullptr) {
1308 DCHECK(const_area->IsEmittedAtUseSite());
1309 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1310 codegen_->LiteralDoubleAddress(
1311 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1312 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1313 } else {
1314 DCHECK(rhs.IsDoubleStackSlot());
1315 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1316 }
1317 } else {
1318 if (rhs.IsFpuRegister()) {
1319 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1320 } else if (const_area != nullptr) {
1321 DCHECK(const_area->IsEmittedAtUseSite());
1322 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1323 codegen_->LiteralFloatAddress(
1324 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1325 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1326 } else {
1327 DCHECK(rhs.IsStackSlot());
1328 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1329 }
1330 }
1331}
1332
Mark Mendell152408f2015-12-31 12:28:50 -05001333template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001334void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001335 LabelType* true_target_in,
1336 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001337 // Generated branching requires both targets to be explicit. If either of the
1338 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001339 LabelType fallthrough_target;
1340 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1341 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001342
Mark Mendellc4701932015-04-10 13:18:51 -04001343 LocationSummary* locations = condition->GetLocations();
1344 Location left = locations->InAt(0);
1345 Location right = locations->InAt(1);
1346
Mark Mendellc4701932015-04-10 13:18:51 -04001347 Primitive::Type type = condition->InputAt(0)->GetType();
1348 switch (type) {
1349 case Primitive::kPrimLong:
1350 GenerateLongComparesAndJumps(condition, true_target, false_target);
1351 break;
1352 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001353 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001354 GenerateFPJumps(condition, true_target, false_target);
1355 break;
1356 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001357 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001358 GenerateFPJumps(condition, true_target, false_target);
1359 break;
1360 default:
1361 LOG(FATAL) << "Unexpected compare type " << type;
1362 }
1363
David Brazdil0debae72015-11-12 18:37:00 +00001364 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001365 __ jmp(false_target);
1366 }
David Brazdil0debae72015-11-12 18:37:00 +00001367
1368 if (fallthrough_target.IsLinked()) {
1369 __ Bind(&fallthrough_target);
1370 }
Mark Mendellc4701932015-04-10 13:18:51 -04001371}
1372
David Brazdil0debae72015-11-12 18:37:00 +00001373static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1374 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1375 // are set only strictly before `branch`. We can't use the eflags on long/FP
1376 // conditions if they are materialized due to the complex branching.
1377 return cond->IsCondition() &&
1378 cond->GetNext() == branch &&
1379 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1380 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1381}
1382
Mark Mendell152408f2015-12-31 12:28:50 -05001383template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001384void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001385 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001386 LabelType* true_target,
1387 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001388 HInstruction* cond = instruction->InputAt(condition_input_index);
1389
1390 if (true_target == nullptr && false_target == nullptr) {
1391 // Nothing to do. The code always falls through.
1392 return;
1393 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001394 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001395 if (cond->AsIntConstant()->IsOne()) {
1396 if (true_target != nullptr) {
1397 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001398 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001399 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001400 DCHECK(cond->AsIntConstant()->IsZero());
1401 if (false_target != nullptr) {
1402 __ jmp(false_target);
1403 }
1404 }
1405 return;
1406 }
1407
1408 // The following code generates these patterns:
1409 // (1) true_target == nullptr && false_target != nullptr
1410 // - opposite condition true => branch to false_target
1411 // (2) true_target != nullptr && false_target == nullptr
1412 // - condition true => branch to true_target
1413 // (3) true_target != nullptr && false_target != nullptr
1414 // - condition true => branch to true_target
1415 // - branch to false_target
1416 if (IsBooleanValueOrMaterializedCondition(cond)) {
1417 if (AreEflagsSetFrom(cond, instruction)) {
1418 if (true_target == nullptr) {
1419 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1420 } else {
1421 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1422 }
1423 } else {
1424 // Materialized condition, compare against 0.
1425 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1426 if (lhs.IsRegister()) {
1427 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1428 } else {
1429 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1430 }
1431 if (true_target == nullptr) {
1432 __ j(kEqual, false_target);
1433 } else {
1434 __ j(kNotEqual, true_target);
1435 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001436 }
1437 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001438 // Condition has not been materialized, use its inputs as the comparison and
1439 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001440 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001441
1442 // If this is a long or FP comparison that has been folded into
1443 // the HCondition, generate the comparison directly.
1444 Primitive::Type type = condition->InputAt(0)->GetType();
1445 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1446 GenerateCompareTestAndBranch(condition, true_target, false_target);
1447 return;
1448 }
1449
1450 Location lhs = condition->GetLocations()->InAt(0);
1451 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001452 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001453 if (rhs.IsRegister()) {
1454 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1455 } else if (rhs.IsConstant()) {
1456 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001457 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001458 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001459 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1460 }
1461 if (true_target == nullptr) {
1462 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1463 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001464 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001465 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001466 }
David Brazdil0debae72015-11-12 18:37:00 +00001467
1468 // If neither branch falls through (case 3), the conditional branch to `true_target`
1469 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1470 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001471 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001472 }
1473}
1474
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001475void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001476 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1477 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001478 locations->SetInAt(0, Location::Any());
1479 }
1480}
1481
1482void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001483 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1484 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1485 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1486 nullptr : codegen_->GetLabelOf(true_successor);
1487 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1488 nullptr : codegen_->GetLabelOf(false_successor);
1489 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001490}
1491
1492void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1493 LocationSummary* locations = new (GetGraph()->GetArena())
1494 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001495 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001496 locations->SetInAt(0, Location::Any());
1497 }
1498}
1499
1500void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001501 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001502 GenerateTestAndBranch<Label>(deoptimize,
1503 /* condition_input_index */ 0,
1504 slow_path->GetEntryLabel(),
1505 /* false_target */ nullptr);
1506}
1507
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001508static bool SelectCanUseCMOV(HSelect* select) {
1509 // There are no conditional move instructions for XMMs.
1510 if (Primitive::IsFloatingPointType(select->GetType())) {
1511 return false;
1512 }
1513
1514 // A FP condition doesn't generate the single CC that we need.
1515 // In 32 bit mode, a long condition doesn't generate a single CC either.
1516 HInstruction* condition = select->GetCondition();
1517 if (condition->IsCondition()) {
1518 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1519 if (compare_type == Primitive::kPrimLong ||
1520 Primitive::IsFloatingPointType(compare_type)) {
1521 return false;
1522 }
1523 }
1524
1525 // We can generate a CMOV for this Select.
1526 return true;
1527}
1528
David Brazdil74eb1b22015-12-14 11:44:01 +00001529void LocationsBuilderX86::VisitSelect(HSelect* select) {
1530 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001531 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001532 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001533 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001534 } else {
1535 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001536 if (SelectCanUseCMOV(select)) {
1537 if (select->InputAt(1)->IsConstant()) {
1538 // Cmov can't handle a constant value.
1539 locations->SetInAt(1, Location::RequiresRegister());
1540 } else {
1541 locations->SetInAt(1, Location::Any());
1542 }
1543 } else {
1544 locations->SetInAt(1, Location::Any());
1545 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001546 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001547 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1548 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001549 }
1550 locations->SetOut(Location::SameAsFirstInput());
1551}
1552
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001553void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1554 Register lhs_reg = lhs.AsRegister<Register>();
1555 if (rhs.IsConstant()) {
1556 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1557 codegen_->Compare32BitValue(lhs_reg, value);
1558 } else if (rhs.IsStackSlot()) {
1559 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1560 } else {
1561 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1562 }
1563}
1564
David Brazdil74eb1b22015-12-14 11:44:01 +00001565void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1566 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001567 DCHECK(locations->InAt(0).Equals(locations->Out()));
1568 if (SelectCanUseCMOV(select)) {
1569 // If both the condition and the source types are integer, we can generate
1570 // a CMOV to implement Select.
1571
1572 HInstruction* select_condition = select->GetCondition();
1573 Condition cond = kNotEqual;
1574
1575 // Figure out how to test the 'condition'.
1576 if (select_condition->IsCondition()) {
1577 HCondition* condition = select_condition->AsCondition();
1578 if (!condition->IsEmittedAtUseSite()) {
1579 // This was a previously materialized condition.
1580 // Can we use the existing condition code?
1581 if (AreEflagsSetFrom(condition, select)) {
1582 // Materialization was the previous instruction. Condition codes are right.
1583 cond = X86Condition(condition->GetCondition());
1584 } else {
1585 // No, we have to recreate the condition code.
1586 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1587 __ testl(cond_reg, cond_reg);
1588 }
1589 } else {
1590 // We can't handle FP or long here.
1591 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1592 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1593 LocationSummary* cond_locations = condition->GetLocations();
1594 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1595 cond = X86Condition(condition->GetCondition());
1596 }
1597 } else {
1598 // Must be a boolean condition, which needs to be compared to 0.
1599 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1600 __ testl(cond_reg, cond_reg);
1601 }
1602
1603 // If the condition is true, overwrite the output, which already contains false.
1604 Location false_loc = locations->InAt(0);
1605 Location true_loc = locations->InAt(1);
1606 if (select->GetType() == Primitive::kPrimLong) {
1607 // 64 bit conditional move.
1608 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1609 Register false_low = false_loc.AsRegisterPairLow<Register>();
1610 if (true_loc.IsRegisterPair()) {
1611 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1612 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1613 } else {
1614 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1615 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1616 }
1617 } else {
1618 // 32 bit conditional move.
1619 Register false_reg = false_loc.AsRegister<Register>();
1620 if (true_loc.IsRegister()) {
1621 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1622 } else {
1623 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1624 }
1625 }
1626 } else {
1627 NearLabel false_target;
1628 GenerateTestAndBranch<NearLabel>(
1629 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1630 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1631 __ Bind(&false_target);
1632 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001633}
1634
David Srbecky0cf44932015-12-09 14:09:59 +00001635void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1636 new (GetGraph()->GetArena()) LocationSummary(info);
1637}
1638
1639void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyc7098ff2016-02-09 14:30:11 +00001640 codegen_->MaybeRecordNativeDebugInfo(info, info->GetDexPc());
1641}
1642
1643void CodeGeneratorX86::GenerateNop() {
1644 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001645}
1646
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001647void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001648 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001649}
1650
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001651void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1652 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001653}
1654
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001655void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001656 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001657}
1658
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001659void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001660 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001661}
1662
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001663void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001664 LocationSummary* locations =
1665 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001666 switch (store->InputAt(1)->GetType()) {
1667 case Primitive::kPrimBoolean:
1668 case Primitive::kPrimByte:
1669 case Primitive::kPrimChar:
1670 case Primitive::kPrimShort:
1671 case Primitive::kPrimInt:
1672 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001673 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001674 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1675 break;
1676
1677 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001678 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001679 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1680 break;
1681
1682 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001683 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001684 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001685}
1686
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001687void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001688}
1689
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001690void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001691 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001692 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001693 // Handle the long/FP comparisons made in instruction simplification.
1694 switch (cond->InputAt(0)->GetType()) {
1695 case Primitive::kPrimLong: {
1696 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001697 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001698 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001699 locations->SetOut(Location::RequiresRegister());
1700 }
1701 break;
1702 }
1703 case Primitive::kPrimFloat:
1704 case Primitive::kPrimDouble: {
1705 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001706 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1707 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1708 } else if (cond->InputAt(1)->IsConstant()) {
1709 locations->SetInAt(1, Location::RequiresFpuRegister());
1710 } else {
1711 locations->SetInAt(1, Location::Any());
1712 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001713 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001714 locations->SetOut(Location::RequiresRegister());
1715 }
1716 break;
1717 }
1718 default:
1719 locations->SetInAt(0, Location::RequiresRegister());
1720 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001721 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001722 // We need a byte register.
1723 locations->SetOut(Location::RegisterLocation(ECX));
1724 }
1725 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001726 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001727}
1728
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001729void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001730 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001731 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001732 }
Mark Mendellc4701932015-04-10 13:18:51 -04001733
1734 LocationSummary* locations = cond->GetLocations();
1735 Location lhs = locations->InAt(0);
1736 Location rhs = locations->InAt(1);
1737 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001738 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001739
1740 switch (cond->InputAt(0)->GetType()) {
1741 default: {
1742 // Integer case.
1743
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001744 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001745 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001746 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001747 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001748 return;
1749 }
1750 case Primitive::kPrimLong:
1751 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1752 break;
1753 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001754 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001755 GenerateFPJumps(cond, &true_label, &false_label);
1756 break;
1757 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001758 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001759 GenerateFPJumps(cond, &true_label, &false_label);
1760 break;
1761 }
1762
1763 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001764 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001765
Roland Levillain4fa13f62015-07-06 18:11:54 +01001766 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001767 __ Bind(&false_label);
1768 __ xorl(reg, reg);
1769 __ jmp(&done_label);
1770
Roland Levillain4fa13f62015-07-06 18:11:54 +01001771 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001772 __ Bind(&true_label);
1773 __ movl(reg, Immediate(1));
1774 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001775}
1776
1777void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001778 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001779}
1780
1781void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001783}
1784
1785void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001787}
1788
1789void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001791}
1792
1793void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001794 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001795}
1796
1797void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001798 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001799}
1800
1801void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001802 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001803}
1804
1805void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001806 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001807}
1808
1809void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001810 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001811}
1812
1813void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001814 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001815}
1816
1817void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001818 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001819}
1820
1821void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001822 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001823}
1824
Aart Bike9f37602015-10-09 11:15:55 -07001825void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001826 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001827}
1828
1829void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001830 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001831}
1832
1833void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001834 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001835}
1836
1837void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001838 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001839}
1840
1841void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001842 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001843}
1844
1845void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001846 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001847}
1848
1849void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001850 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001851}
1852
1853void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001854 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001855}
1856
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001857void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001858 LocationSummary* locations =
1859 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001860 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001861}
1862
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001863void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001864 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001865}
1866
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001867void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1868 LocationSummary* locations =
1869 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1870 locations->SetOut(Location::ConstantLocation(constant));
1871}
1872
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001873void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001874 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001875}
1876
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001877void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001878 LocationSummary* locations =
1879 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001880 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881}
1882
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001883void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001884 // Will be generated at use site.
1885}
1886
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001887void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1888 LocationSummary* locations =
1889 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1890 locations->SetOut(Location::ConstantLocation(constant));
1891}
1892
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001893void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001894 // Will be generated at use site.
1895}
1896
1897void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1898 LocationSummary* locations =
1899 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1900 locations->SetOut(Location::ConstantLocation(constant));
1901}
1902
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001903void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001904 // Will be generated at use site.
1905}
1906
Calin Juravle27df7582015-04-17 19:12:31 +01001907void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1908 memory_barrier->SetLocations(nullptr);
1909}
1910
1911void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001912 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001913}
1914
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001915void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001916 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001917}
1918
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001919void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001920 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001921}
1922
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001923void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001924 LocationSummary* locations =
1925 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926 switch (ret->InputAt(0)->GetType()) {
1927 case Primitive::kPrimBoolean:
1928 case Primitive::kPrimByte:
1929 case Primitive::kPrimChar:
1930 case Primitive::kPrimShort:
1931 case Primitive::kPrimInt:
1932 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001933 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001934 break;
1935
1936 case Primitive::kPrimLong:
1937 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001938 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001939 break;
1940
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001941 case Primitive::kPrimFloat:
1942 case Primitive::kPrimDouble:
1943 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001944 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001945 break;
1946
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001947 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001948 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001949 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001950}
1951
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001952void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001953 if (kIsDebugBuild) {
1954 switch (ret->InputAt(0)->GetType()) {
1955 case Primitive::kPrimBoolean:
1956 case Primitive::kPrimByte:
1957 case Primitive::kPrimChar:
1958 case Primitive::kPrimShort:
1959 case Primitive::kPrimInt:
1960 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001961 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001962 break;
1963
1964 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001965 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1966 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001967 break;
1968
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001969 case Primitive::kPrimFloat:
1970 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001971 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001972 break;
1973
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001974 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001975 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001976 }
1977 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001978 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001979}
1980
Calin Juravle175dc732015-08-25 15:42:32 +01001981void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1982 // The trampoline uses the same calling convention as dex calling conventions,
1983 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1984 // the method_idx.
1985 HandleInvoke(invoke);
1986}
1987
1988void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1989 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1990}
1991
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001992void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001993 // Explicit clinit checks triggered by static invokes must have been pruned by
1994 // art::PrepareForRegisterAllocation.
1995 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001996
Mark Mendellfb8d2792015-03-31 22:16:59 -04001997 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001998 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001999 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002000 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002001 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002002 return;
2003 }
2004
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002005 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002006
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002007 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2008 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002009 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002010 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002011}
2012
Mark Mendell09ed1a32015-03-25 08:30:06 -04002013static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2014 if (invoke->GetLocations()->Intrinsified()) {
2015 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2016 intrinsic.Dispatch(invoke);
2017 return true;
2018 }
2019 return false;
2020}
2021
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002022void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002023 // Explicit clinit checks triggered by static invokes must have been pruned by
2024 // art::PrepareForRegisterAllocation.
2025 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002026
Mark Mendell09ed1a32015-03-25 08:30:06 -04002027 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2028 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002029 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002030
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002031 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002032 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002033 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002034 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002035}
2036
2037void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002038 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2039 if (intrinsic.TryDispatch(invoke)) {
2040 return;
2041 }
2042
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002043 HandleInvoke(invoke);
2044}
2045
2046void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002047 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002048 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002049}
2050
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002051void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002052 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2053 return;
2054 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002055
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002056 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002057 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002058 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002059}
2060
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002061void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002062 // This call to HandleInvoke allocates a temporary (core) register
2063 // which is also used to transfer the hidden argument from FP to
2064 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002065 HandleInvoke(invoke);
2066 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002067 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002068}
2069
2070void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2071 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002072 LocationSummary* locations = invoke->GetLocations();
2073 Register temp = locations->GetTemp(0).AsRegister<Register>();
2074 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002075 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2076 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002077 Location receiver = locations->InAt(0);
2078 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2079
Roland Levillain0d5a2812015-11-13 10:07:31 +00002080 // Set the hidden argument. This is safe to do this here, as XMM7
2081 // won't be modified thereafter, before the `call` instruction.
2082 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002083 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002084 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002085
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002086 if (receiver.IsStackSlot()) {
2087 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002088 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002089 __ movl(temp, Address(temp, class_offset));
2090 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002091 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002092 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002093 }
Roland Levillain4d027112015-07-01 15:41:14 +01002094 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002095 // Instead of simply (possibly) unpoisoning `temp` here, we should
2096 // emit a read barrier for the previous class reference load.
2097 // However this is not required in practice, as this is an
2098 // intermediate/temporary reference and because the current
2099 // concurrent copying collector keeps the from-space memory
2100 // intact/accessible until the end of the marking phase (the
2101 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002102 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002103 // temp = temp->GetImtEntryAt(method_offset);
2104 __ movl(temp, Address(temp, method_offset));
2105 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002106 __ call(Address(temp,
2107 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002108
2109 DCHECK(!codegen_->IsLeafMethod());
2110 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2111}
2112
Roland Levillain88cb1752014-10-20 16:36:47 +01002113void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2114 LocationSummary* locations =
2115 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2116 switch (neg->GetResultType()) {
2117 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002118 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002119 locations->SetInAt(0, Location::RequiresRegister());
2120 locations->SetOut(Location::SameAsFirstInput());
2121 break;
2122
Roland Levillain88cb1752014-10-20 16:36:47 +01002123 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002124 locations->SetInAt(0, Location::RequiresFpuRegister());
2125 locations->SetOut(Location::SameAsFirstInput());
2126 locations->AddTemp(Location::RequiresRegister());
2127 locations->AddTemp(Location::RequiresFpuRegister());
2128 break;
2129
Roland Levillain88cb1752014-10-20 16:36:47 +01002130 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002131 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002132 locations->SetOut(Location::SameAsFirstInput());
2133 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002134 break;
2135
2136 default:
2137 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2138 }
2139}
2140
2141void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2142 LocationSummary* locations = neg->GetLocations();
2143 Location out = locations->Out();
2144 Location in = locations->InAt(0);
2145 switch (neg->GetResultType()) {
2146 case Primitive::kPrimInt:
2147 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002148 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002149 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002150 break;
2151
2152 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002153 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002154 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002155 __ negl(out.AsRegisterPairLow<Register>());
2156 // Negation is similar to subtraction from zero. The least
2157 // significant byte triggers a borrow when it is different from
2158 // zero; to take it into account, add 1 to the most significant
2159 // byte if the carry flag (CF) is set to 1 after the first NEGL
2160 // operation.
2161 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2162 __ negl(out.AsRegisterPairHigh<Register>());
2163 break;
2164
Roland Levillain5368c212014-11-27 15:03:41 +00002165 case Primitive::kPrimFloat: {
2166 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002167 Register constant = locations->GetTemp(0).AsRegister<Register>();
2168 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002169 // Implement float negation with an exclusive or with value
2170 // 0x80000000 (mask for bit 31, representing the sign of a
2171 // single-precision floating-point number).
2172 __ movl(constant, Immediate(INT32_C(0x80000000)));
2173 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002174 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002175 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002176 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002177
Roland Levillain5368c212014-11-27 15:03:41 +00002178 case Primitive::kPrimDouble: {
2179 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002180 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002181 // Implement double negation with an exclusive or with value
2182 // 0x8000000000000000 (mask for bit 63, representing the sign of
2183 // a double-precision floating-point number).
2184 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002185 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002186 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002187 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002188
2189 default:
2190 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2191 }
2192}
2193
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002194void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2195 LocationSummary* locations =
2196 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2197 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2198 locations->SetInAt(0, Location::RequiresFpuRegister());
2199 locations->SetInAt(1, Location::RequiresRegister());
2200 locations->SetOut(Location::SameAsFirstInput());
2201 locations->AddTemp(Location::RequiresFpuRegister());
2202}
2203
2204void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2205 LocationSummary* locations = neg->GetLocations();
2206 Location out = locations->Out();
2207 DCHECK(locations->InAt(0).Equals(out));
2208
2209 Register constant_area = locations->InAt(1).AsRegister<Register>();
2210 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2211 if (neg->GetType() == Primitive::kPrimFloat) {
2212 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2213 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2214 } else {
2215 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2216 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2217 }
2218}
2219
Roland Levillaindff1f282014-11-05 14:15:05 +00002220void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002221 Primitive::Type result_type = conversion->GetResultType();
2222 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002223 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002224
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002225 // The float-to-long and double-to-long type conversions rely on a
2226 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002227 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002228 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2229 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002230 ? LocationSummary::kCall
2231 : LocationSummary::kNoCall;
2232 LocationSummary* locations =
2233 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2234
David Brazdilb2bd1c52015-03-25 11:17:37 +00002235 // The Java language does not allow treating boolean as an integral type but
2236 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002237
Roland Levillaindff1f282014-11-05 14:15:05 +00002238 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002239 case Primitive::kPrimByte:
2240 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002241 case Primitive::kPrimLong: {
2242 // Type conversion from long to byte is a result of code transformations.
2243 HInstruction* input = conversion->InputAt(0);
2244 Location input_location = input->IsConstant()
2245 ? Location::ConstantLocation(input->AsConstant())
2246 : Location::RegisterPairLocation(EAX, EDX);
2247 locations->SetInAt(0, input_location);
2248 // Make the output overlap to please the register allocator. This greatly simplifies
2249 // the validation of the linear scan implementation
2250 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2251 break;
2252 }
David Brazdil46e2a392015-03-16 17:31:52 +00002253 case Primitive::kPrimBoolean:
2254 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002255 case Primitive::kPrimShort:
2256 case Primitive::kPrimInt:
2257 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002258 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002259 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2260 // Make the output overlap to please the register allocator. This greatly simplifies
2261 // the validation of the linear scan implementation
2262 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002263 break;
2264
2265 default:
2266 LOG(FATAL) << "Unexpected type conversion from " << input_type
2267 << " to " << result_type;
2268 }
2269 break;
2270
Roland Levillain01a8d712014-11-14 16:27:39 +00002271 case Primitive::kPrimShort:
2272 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002273 case Primitive::kPrimLong:
2274 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002275 case Primitive::kPrimBoolean:
2276 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002277 case Primitive::kPrimByte:
2278 case Primitive::kPrimInt:
2279 case Primitive::kPrimChar:
2280 // Processing a Dex `int-to-short' instruction.
2281 locations->SetInAt(0, Location::Any());
2282 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2283 break;
2284
2285 default:
2286 LOG(FATAL) << "Unexpected type conversion from " << input_type
2287 << " to " << result_type;
2288 }
2289 break;
2290
Roland Levillain946e1432014-11-11 17:35:19 +00002291 case Primitive::kPrimInt:
2292 switch (input_type) {
2293 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002294 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002295 locations->SetInAt(0, Location::Any());
2296 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2297 break;
2298
2299 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002300 // Processing a Dex `float-to-int' instruction.
2301 locations->SetInAt(0, Location::RequiresFpuRegister());
2302 locations->SetOut(Location::RequiresRegister());
2303 locations->AddTemp(Location::RequiresFpuRegister());
2304 break;
2305
Roland Levillain946e1432014-11-11 17:35:19 +00002306 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002307 // Processing a Dex `double-to-int' instruction.
2308 locations->SetInAt(0, Location::RequiresFpuRegister());
2309 locations->SetOut(Location::RequiresRegister());
2310 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002311 break;
2312
2313 default:
2314 LOG(FATAL) << "Unexpected type conversion from " << input_type
2315 << " to " << result_type;
2316 }
2317 break;
2318
Roland Levillaindff1f282014-11-05 14:15:05 +00002319 case Primitive::kPrimLong:
2320 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002321 case Primitive::kPrimBoolean:
2322 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002323 case Primitive::kPrimByte:
2324 case Primitive::kPrimShort:
2325 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002326 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002327 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002328 locations->SetInAt(0, Location::RegisterLocation(EAX));
2329 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2330 break;
2331
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002332 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002333 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002334 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002335 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002336 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2337 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2338
Vladimir Marko949c91f2015-01-27 10:48:44 +00002339 // The runtime helper puts the result in EAX, EDX.
2340 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002341 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002342 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002343
2344 default:
2345 LOG(FATAL) << "Unexpected type conversion from " << input_type
2346 << " to " << result_type;
2347 }
2348 break;
2349
Roland Levillain981e4542014-11-14 11:47:14 +00002350 case Primitive::kPrimChar:
2351 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002352 case Primitive::kPrimLong:
2353 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002354 case Primitive::kPrimBoolean:
2355 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002356 case Primitive::kPrimByte:
2357 case Primitive::kPrimShort:
2358 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002359 // Processing a Dex `int-to-char' instruction.
2360 locations->SetInAt(0, Location::Any());
2361 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2362 break;
2363
2364 default:
2365 LOG(FATAL) << "Unexpected type conversion from " << input_type
2366 << " to " << result_type;
2367 }
2368 break;
2369
Roland Levillaindff1f282014-11-05 14:15:05 +00002370 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002371 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002372 case Primitive::kPrimBoolean:
2373 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002374 case Primitive::kPrimByte:
2375 case Primitive::kPrimShort:
2376 case Primitive::kPrimInt:
2377 case Primitive::kPrimChar:
2378 // Processing a Dex `int-to-float' instruction.
2379 locations->SetInAt(0, Location::RequiresRegister());
2380 locations->SetOut(Location::RequiresFpuRegister());
2381 break;
2382
2383 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002384 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002385 locations->SetInAt(0, Location::Any());
2386 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002387 break;
2388
Roland Levillaincff13742014-11-17 14:32:17 +00002389 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002390 // Processing a Dex `double-to-float' instruction.
2391 locations->SetInAt(0, Location::RequiresFpuRegister());
2392 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002393 break;
2394
2395 default:
2396 LOG(FATAL) << "Unexpected type conversion from " << input_type
2397 << " to " << result_type;
2398 };
2399 break;
2400
Roland Levillaindff1f282014-11-05 14:15:05 +00002401 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002402 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002403 case Primitive::kPrimBoolean:
2404 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002405 case Primitive::kPrimByte:
2406 case Primitive::kPrimShort:
2407 case Primitive::kPrimInt:
2408 case Primitive::kPrimChar:
2409 // Processing a Dex `int-to-double' instruction.
2410 locations->SetInAt(0, Location::RequiresRegister());
2411 locations->SetOut(Location::RequiresFpuRegister());
2412 break;
2413
2414 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002415 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002416 locations->SetInAt(0, Location::Any());
2417 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002418 break;
2419
Roland Levillaincff13742014-11-17 14:32:17 +00002420 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002421 // Processing a Dex `float-to-double' instruction.
2422 locations->SetInAt(0, Location::RequiresFpuRegister());
2423 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002424 break;
2425
2426 default:
2427 LOG(FATAL) << "Unexpected type conversion from " << input_type
2428 << " to " << result_type;
2429 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002430 break;
2431
2432 default:
2433 LOG(FATAL) << "Unexpected type conversion from " << input_type
2434 << " to " << result_type;
2435 }
2436}
2437
2438void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2439 LocationSummary* locations = conversion->GetLocations();
2440 Location out = locations->Out();
2441 Location in = locations->InAt(0);
2442 Primitive::Type result_type = conversion->GetResultType();
2443 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002444 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002445 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002446 case Primitive::kPrimByte:
2447 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002448 case Primitive::kPrimLong:
2449 // Type conversion from long to byte is a result of code transformations.
2450 if (in.IsRegisterPair()) {
2451 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2452 } else {
2453 DCHECK(in.GetConstant()->IsLongConstant());
2454 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2455 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2456 }
2457 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002458 case Primitive::kPrimBoolean:
2459 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002460 case Primitive::kPrimShort:
2461 case Primitive::kPrimInt:
2462 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002463 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002464 if (in.IsRegister()) {
2465 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002466 } else {
2467 DCHECK(in.GetConstant()->IsIntConstant());
2468 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2469 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2470 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002471 break;
2472
2473 default:
2474 LOG(FATAL) << "Unexpected type conversion from " << input_type
2475 << " to " << result_type;
2476 }
2477 break;
2478
Roland Levillain01a8d712014-11-14 16:27:39 +00002479 case Primitive::kPrimShort:
2480 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002481 case Primitive::kPrimLong:
2482 // Type conversion from long to short is a result of code transformations.
2483 if (in.IsRegisterPair()) {
2484 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2485 } else if (in.IsDoubleStackSlot()) {
2486 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2487 } else {
2488 DCHECK(in.GetConstant()->IsLongConstant());
2489 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2490 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2491 }
2492 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002493 case Primitive::kPrimBoolean:
2494 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002495 case Primitive::kPrimByte:
2496 case Primitive::kPrimInt:
2497 case Primitive::kPrimChar:
2498 // Processing a Dex `int-to-short' instruction.
2499 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002500 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002501 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002502 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002503 } else {
2504 DCHECK(in.GetConstant()->IsIntConstant());
2505 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002506 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002507 }
2508 break;
2509
2510 default:
2511 LOG(FATAL) << "Unexpected type conversion from " << input_type
2512 << " to " << result_type;
2513 }
2514 break;
2515
Roland Levillain946e1432014-11-11 17:35:19 +00002516 case Primitive::kPrimInt:
2517 switch (input_type) {
2518 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002519 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002520 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002521 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002522 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002523 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002524 } else {
2525 DCHECK(in.IsConstant());
2526 DCHECK(in.GetConstant()->IsLongConstant());
2527 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002528 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002529 }
2530 break;
2531
Roland Levillain3f8f9362014-12-02 17:45:01 +00002532 case Primitive::kPrimFloat: {
2533 // Processing a Dex `float-to-int' instruction.
2534 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2535 Register output = out.AsRegister<Register>();
2536 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002537 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002538
2539 __ movl(output, Immediate(kPrimIntMax));
2540 // temp = int-to-float(output)
2541 __ cvtsi2ss(temp, output);
2542 // if input >= temp goto done
2543 __ comiss(input, temp);
2544 __ j(kAboveEqual, &done);
2545 // if input == NaN goto nan
2546 __ j(kUnordered, &nan);
2547 // output = float-to-int-truncate(input)
2548 __ cvttss2si(output, input);
2549 __ jmp(&done);
2550 __ Bind(&nan);
2551 // output = 0
2552 __ xorl(output, output);
2553 __ Bind(&done);
2554 break;
2555 }
2556
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002557 case Primitive::kPrimDouble: {
2558 // Processing a Dex `double-to-int' instruction.
2559 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2560 Register output = out.AsRegister<Register>();
2561 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002562 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002563
2564 __ movl(output, Immediate(kPrimIntMax));
2565 // temp = int-to-double(output)
2566 __ cvtsi2sd(temp, output);
2567 // if input >= temp goto done
2568 __ comisd(input, temp);
2569 __ j(kAboveEqual, &done);
2570 // if input == NaN goto nan
2571 __ j(kUnordered, &nan);
2572 // output = double-to-int-truncate(input)
2573 __ cvttsd2si(output, input);
2574 __ jmp(&done);
2575 __ Bind(&nan);
2576 // output = 0
2577 __ xorl(output, output);
2578 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002579 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002580 }
Roland Levillain946e1432014-11-11 17:35:19 +00002581
2582 default:
2583 LOG(FATAL) << "Unexpected type conversion from " << input_type
2584 << " to " << result_type;
2585 }
2586 break;
2587
Roland Levillaindff1f282014-11-05 14:15:05 +00002588 case Primitive::kPrimLong:
2589 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002590 case Primitive::kPrimBoolean:
2591 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002592 case Primitive::kPrimByte:
2593 case Primitive::kPrimShort:
2594 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002595 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002596 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002597 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2598 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002599 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002600 __ cdq();
2601 break;
2602
2603 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002604 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002605 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2606 conversion,
2607 conversion->GetDexPc(),
2608 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002609 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002610 break;
2611
Roland Levillaindff1f282014-11-05 14:15:05 +00002612 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002613 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002614 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2615 conversion,
2616 conversion->GetDexPc(),
2617 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002618 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002619 break;
2620
2621 default:
2622 LOG(FATAL) << "Unexpected type conversion from " << input_type
2623 << " to " << result_type;
2624 }
2625 break;
2626
Roland Levillain981e4542014-11-14 11:47:14 +00002627 case Primitive::kPrimChar:
2628 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002629 case Primitive::kPrimLong:
2630 // Type conversion from long to short is a result of code transformations.
2631 if (in.IsRegisterPair()) {
2632 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2633 } else if (in.IsDoubleStackSlot()) {
2634 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2635 } else {
2636 DCHECK(in.GetConstant()->IsLongConstant());
2637 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2638 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2639 }
2640 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002641 case Primitive::kPrimBoolean:
2642 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002643 case Primitive::kPrimByte:
2644 case Primitive::kPrimShort:
2645 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002646 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2647 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002648 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002649 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002650 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002651 } else {
2652 DCHECK(in.GetConstant()->IsIntConstant());
2653 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002654 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002655 }
2656 break;
2657
2658 default:
2659 LOG(FATAL) << "Unexpected type conversion from " << input_type
2660 << " to " << result_type;
2661 }
2662 break;
2663
Roland Levillaindff1f282014-11-05 14:15:05 +00002664 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002665 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002666 case Primitive::kPrimBoolean:
2667 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002668 case Primitive::kPrimByte:
2669 case Primitive::kPrimShort:
2670 case Primitive::kPrimInt:
2671 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002672 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002673 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002674 break;
2675
Roland Levillain6d0e4832014-11-27 18:31:21 +00002676 case Primitive::kPrimLong: {
2677 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002678 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002679
Roland Levillain232ade02015-04-20 15:14:36 +01002680 // Create stack space for the call to
2681 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2682 // TODO: enhance register allocator to ask for stack temporaries.
2683 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2684 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2685 __ subl(ESP, Immediate(adjustment));
2686 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002687
Roland Levillain232ade02015-04-20 15:14:36 +01002688 // Load the value to the FP stack, using temporaries if needed.
2689 PushOntoFPStack(in, 0, adjustment, false, true);
2690
2691 if (out.IsStackSlot()) {
2692 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2693 } else {
2694 __ fstps(Address(ESP, 0));
2695 Location stack_temp = Location::StackSlot(0);
2696 codegen_->Move32(out, stack_temp);
2697 }
2698
2699 // Remove the temporary stack space we allocated.
2700 if (adjustment != 0) {
2701 __ addl(ESP, Immediate(adjustment));
2702 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002703 break;
2704 }
2705
Roland Levillaincff13742014-11-17 14:32:17 +00002706 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002707 // Processing a Dex `double-to-float' instruction.
2708 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002709 break;
2710
2711 default:
2712 LOG(FATAL) << "Unexpected type conversion from " << input_type
2713 << " to " << result_type;
2714 };
2715 break;
2716
Roland Levillaindff1f282014-11-05 14:15:05 +00002717 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002718 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002719 case Primitive::kPrimBoolean:
2720 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002721 case Primitive::kPrimByte:
2722 case Primitive::kPrimShort:
2723 case Primitive::kPrimInt:
2724 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002725 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002726 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002727 break;
2728
Roland Levillain647b9ed2014-11-27 12:06:00 +00002729 case Primitive::kPrimLong: {
2730 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002731 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002732
Roland Levillain232ade02015-04-20 15:14:36 +01002733 // Create stack space for the call to
2734 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2735 // TODO: enhance register allocator to ask for stack temporaries.
2736 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2737 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2738 __ subl(ESP, Immediate(adjustment));
2739 }
2740
2741 // Load the value to the FP stack, using temporaries if needed.
2742 PushOntoFPStack(in, 0, adjustment, false, true);
2743
2744 if (out.IsDoubleStackSlot()) {
2745 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2746 } else {
2747 __ fstpl(Address(ESP, 0));
2748 Location stack_temp = Location::DoubleStackSlot(0);
2749 codegen_->Move64(out, stack_temp);
2750 }
2751
2752 // Remove the temporary stack space we allocated.
2753 if (adjustment != 0) {
2754 __ addl(ESP, Immediate(adjustment));
2755 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002756 break;
2757 }
2758
Roland Levillaincff13742014-11-17 14:32:17 +00002759 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002760 // Processing a Dex `float-to-double' instruction.
2761 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002762 break;
2763
2764 default:
2765 LOG(FATAL) << "Unexpected type conversion from " << input_type
2766 << " to " << result_type;
2767 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002768 break;
2769
2770 default:
2771 LOG(FATAL) << "Unexpected type conversion from " << input_type
2772 << " to " << result_type;
2773 }
2774}
2775
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002776void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002777 LocationSummary* locations =
2778 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002779 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002780 case Primitive::kPrimInt: {
2781 locations->SetInAt(0, Location::RequiresRegister());
2782 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2783 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2784 break;
2785 }
2786
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002787 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002788 locations->SetInAt(0, Location::RequiresRegister());
2789 locations->SetInAt(1, Location::Any());
2790 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002791 break;
2792 }
2793
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002794 case Primitive::kPrimFloat:
2795 case Primitive::kPrimDouble: {
2796 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002797 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2798 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002799 } else if (add->InputAt(1)->IsConstant()) {
2800 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002801 } else {
2802 locations->SetInAt(1, Location::Any());
2803 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002804 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002805 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002806 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002807
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002808 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002809 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2810 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002811 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002812}
2813
2814void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2815 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002816 Location first = locations->InAt(0);
2817 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002818 Location out = locations->Out();
2819
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002820 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002821 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002822 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002823 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2824 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002825 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2826 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002827 } else {
2828 __ leal(out.AsRegister<Register>(), Address(
2829 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2830 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002831 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002832 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2833 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2834 __ addl(out.AsRegister<Register>(), Immediate(value));
2835 } else {
2836 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2837 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002838 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002839 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002840 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002841 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002842 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002843 }
2844
2845 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002846 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002847 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2848 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002849 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002850 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2851 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002852 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002853 } else {
2854 DCHECK(second.IsConstant()) << second;
2855 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2856 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2857 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002858 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002859 break;
2860 }
2861
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002862 case Primitive::kPrimFloat: {
2863 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002864 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002865 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2866 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002867 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002868 __ addss(first.AsFpuRegister<XmmRegister>(),
2869 codegen_->LiteralFloatAddress(
2870 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2871 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2872 } else {
2873 DCHECK(second.IsStackSlot());
2874 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002875 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002876 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002877 }
2878
2879 case Primitive::kPrimDouble: {
2880 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002881 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002882 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2883 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002884 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002885 __ addsd(first.AsFpuRegister<XmmRegister>(),
2886 codegen_->LiteralDoubleAddress(
2887 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2888 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2889 } else {
2890 DCHECK(second.IsDoubleStackSlot());
2891 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002892 }
2893 break;
2894 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002895
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002896 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002897 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002898 }
2899}
2900
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002901void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002902 LocationSummary* locations =
2903 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002904 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002905 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002906 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002907 locations->SetInAt(0, Location::RequiresRegister());
2908 locations->SetInAt(1, Location::Any());
2909 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002910 break;
2911 }
Calin Juravle11351682014-10-23 15:38:15 +01002912 case Primitive::kPrimFloat:
2913 case Primitive::kPrimDouble: {
2914 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002915 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2916 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002917 } else if (sub->InputAt(1)->IsConstant()) {
2918 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002919 } else {
2920 locations->SetInAt(1, Location::Any());
2921 }
Calin Juravle11351682014-10-23 15:38:15 +01002922 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002923 break;
Calin Juravle11351682014-10-23 15:38:15 +01002924 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002925
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002926 default:
Calin Juravle11351682014-10-23 15:38:15 +01002927 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002928 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002929}
2930
2931void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2932 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002933 Location first = locations->InAt(0);
2934 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002935 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002936 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002937 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002938 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002939 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002940 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002941 __ subl(first.AsRegister<Register>(),
2942 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002943 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002944 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002945 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002946 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002947 }
2948
2949 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002950 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002951 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2952 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002953 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002954 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002955 __ sbbl(first.AsRegisterPairHigh<Register>(),
2956 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002957 } else {
2958 DCHECK(second.IsConstant()) << second;
2959 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2960 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2961 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002962 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002963 break;
2964 }
2965
Calin Juravle11351682014-10-23 15:38:15 +01002966 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002967 if (second.IsFpuRegister()) {
2968 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2969 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2970 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002971 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002972 __ subss(first.AsFpuRegister<XmmRegister>(),
2973 codegen_->LiteralFloatAddress(
2974 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2975 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2976 } else {
2977 DCHECK(second.IsStackSlot());
2978 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2979 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002980 break;
Calin Juravle11351682014-10-23 15:38:15 +01002981 }
2982
2983 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002984 if (second.IsFpuRegister()) {
2985 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2986 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2987 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002988 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002989 __ subsd(first.AsFpuRegister<XmmRegister>(),
2990 codegen_->LiteralDoubleAddress(
2991 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2992 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2993 } else {
2994 DCHECK(second.IsDoubleStackSlot());
2995 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2996 }
Calin Juravle11351682014-10-23 15:38:15 +01002997 break;
2998 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002999
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003000 default:
Calin Juravle11351682014-10-23 15:38:15 +01003001 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003002 }
3003}
3004
Calin Juravle34bacdf2014-10-07 20:23:36 +01003005void LocationsBuilderX86::VisitMul(HMul* mul) {
3006 LocationSummary* locations =
3007 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3008 switch (mul->GetResultType()) {
3009 case Primitive::kPrimInt:
3010 locations->SetInAt(0, Location::RequiresRegister());
3011 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003012 if (mul->InputAt(1)->IsIntConstant()) {
3013 // Can use 3 operand multiply.
3014 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3015 } else {
3016 locations->SetOut(Location::SameAsFirstInput());
3017 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003018 break;
3019 case Primitive::kPrimLong: {
3020 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003021 locations->SetInAt(1, Location::Any());
3022 locations->SetOut(Location::SameAsFirstInput());
3023 // Needed for imul on 32bits with 64bits output.
3024 locations->AddTemp(Location::RegisterLocation(EAX));
3025 locations->AddTemp(Location::RegisterLocation(EDX));
3026 break;
3027 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003028 case Primitive::kPrimFloat:
3029 case Primitive::kPrimDouble: {
3030 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003031 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3032 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003033 } else if (mul->InputAt(1)->IsConstant()) {
3034 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003035 } else {
3036 locations->SetInAt(1, Location::Any());
3037 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003038 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003039 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003040 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003041
3042 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003043 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003044 }
3045}
3046
3047void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3048 LocationSummary* locations = mul->GetLocations();
3049 Location first = locations->InAt(0);
3050 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003051 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003052
3053 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003054 case Primitive::kPrimInt:
3055 // The constant may have ended up in a register, so test explicitly to avoid
3056 // problems where the output may not be the same as the first operand.
3057 if (mul->InputAt(1)->IsIntConstant()) {
3058 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3059 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3060 } else if (second.IsRegister()) {
3061 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003062 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003063 } else {
3064 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003065 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003066 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003067 }
3068 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003069
3070 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003071 Register in1_hi = first.AsRegisterPairHigh<Register>();
3072 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003073 Register eax = locations->GetTemp(0).AsRegister<Register>();
3074 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003075
3076 DCHECK_EQ(EAX, eax);
3077 DCHECK_EQ(EDX, edx);
3078
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003079 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003080 // output: in1
3081 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3082 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3083 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003084 if (second.IsConstant()) {
3085 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003086
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003087 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3088 int32_t low_value = Low32Bits(value);
3089 int32_t high_value = High32Bits(value);
3090 Immediate low(low_value);
3091 Immediate high(high_value);
3092
3093 __ movl(eax, high);
3094 // eax <- in1.lo * in2.hi
3095 __ imull(eax, in1_lo);
3096 // in1.hi <- in1.hi * in2.lo
3097 __ imull(in1_hi, low);
3098 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3099 __ addl(in1_hi, eax);
3100 // move in2_lo to eax to prepare for double precision
3101 __ movl(eax, low);
3102 // edx:eax <- in1.lo * in2.lo
3103 __ mull(in1_lo);
3104 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3105 __ addl(in1_hi, edx);
3106 // in1.lo <- (in1.lo * in2.lo)[31:0];
3107 __ movl(in1_lo, eax);
3108 } else if (second.IsRegisterPair()) {
3109 Register in2_hi = second.AsRegisterPairHigh<Register>();
3110 Register in2_lo = second.AsRegisterPairLow<Register>();
3111
3112 __ movl(eax, in2_hi);
3113 // eax <- in1.lo * in2.hi
3114 __ imull(eax, in1_lo);
3115 // in1.hi <- in1.hi * in2.lo
3116 __ imull(in1_hi, in2_lo);
3117 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3118 __ addl(in1_hi, eax);
3119 // move in1_lo to eax to prepare for double precision
3120 __ movl(eax, in1_lo);
3121 // edx:eax <- in1.lo * in2.lo
3122 __ mull(in2_lo);
3123 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3124 __ addl(in1_hi, edx);
3125 // in1.lo <- (in1.lo * in2.lo)[31:0];
3126 __ movl(in1_lo, eax);
3127 } else {
3128 DCHECK(second.IsDoubleStackSlot()) << second;
3129 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3130 Address in2_lo(ESP, second.GetStackIndex());
3131
3132 __ movl(eax, in2_hi);
3133 // eax <- in1.lo * in2.hi
3134 __ imull(eax, in1_lo);
3135 // in1.hi <- in1.hi * in2.lo
3136 __ imull(in1_hi, in2_lo);
3137 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3138 __ addl(in1_hi, eax);
3139 // move in1_lo to eax to prepare for double precision
3140 __ movl(eax, in1_lo);
3141 // edx:eax <- in1.lo * in2.lo
3142 __ mull(in2_lo);
3143 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3144 __ addl(in1_hi, edx);
3145 // in1.lo <- (in1.lo * in2.lo)[31:0];
3146 __ movl(in1_lo, eax);
3147 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003148
3149 break;
3150 }
3151
Calin Juravleb5bfa962014-10-21 18:02:24 +01003152 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003153 DCHECK(first.Equals(locations->Out()));
3154 if (second.IsFpuRegister()) {
3155 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3156 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3157 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003158 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003159 __ mulss(first.AsFpuRegister<XmmRegister>(),
3160 codegen_->LiteralFloatAddress(
3161 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3162 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3163 } else {
3164 DCHECK(second.IsStackSlot());
3165 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3166 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003167 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003168 }
3169
3170 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003171 DCHECK(first.Equals(locations->Out()));
3172 if (second.IsFpuRegister()) {
3173 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3174 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3175 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003176 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003177 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3178 codegen_->LiteralDoubleAddress(
3179 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3180 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3181 } else {
3182 DCHECK(second.IsDoubleStackSlot());
3183 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3184 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003185 break;
3186 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003187
3188 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003189 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003190 }
3191}
3192
Roland Levillain232ade02015-04-20 15:14:36 +01003193void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3194 uint32_t temp_offset,
3195 uint32_t stack_adjustment,
3196 bool is_fp,
3197 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003198 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003199 DCHECK(!is_wide);
3200 if (is_fp) {
3201 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3202 } else {
3203 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3204 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003205 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003206 DCHECK(is_wide);
3207 if (is_fp) {
3208 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3209 } else {
3210 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3211 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003212 } else {
3213 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003214 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003215 Location stack_temp = Location::StackSlot(temp_offset);
3216 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003217 if (is_fp) {
3218 __ flds(Address(ESP, temp_offset));
3219 } else {
3220 __ filds(Address(ESP, temp_offset));
3221 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003222 } else {
3223 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3224 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003225 if (is_fp) {
3226 __ fldl(Address(ESP, temp_offset));
3227 } else {
3228 __ fildl(Address(ESP, temp_offset));
3229 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003230 }
3231 }
3232}
3233
3234void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3235 Primitive::Type type = rem->GetResultType();
3236 bool is_float = type == Primitive::kPrimFloat;
3237 size_t elem_size = Primitive::ComponentSize(type);
3238 LocationSummary* locations = rem->GetLocations();
3239 Location first = locations->InAt(0);
3240 Location second = locations->InAt(1);
3241 Location out = locations->Out();
3242
3243 // Create stack space for 2 elements.
3244 // TODO: enhance register allocator to ask for stack temporaries.
3245 __ subl(ESP, Immediate(2 * elem_size));
3246
3247 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003248 const bool is_wide = !is_float;
3249 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3250 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003251
3252 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003253 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003254 __ Bind(&retry);
3255 __ fprem();
3256
3257 // Move FP status to AX.
3258 __ fstsw();
3259
3260 // And see if the argument reduction is complete. This is signaled by the
3261 // C2 FPU flag bit set to 0.
3262 __ andl(EAX, Immediate(kC2ConditionMask));
3263 __ j(kNotEqual, &retry);
3264
3265 // We have settled on the final value. Retrieve it into an XMM register.
3266 // Store FP top of stack to real stack.
3267 if (is_float) {
3268 __ fsts(Address(ESP, 0));
3269 } else {
3270 __ fstl(Address(ESP, 0));
3271 }
3272
3273 // Pop the 2 items from the FP stack.
3274 __ fucompp();
3275
3276 // Load the value from the stack into an XMM register.
3277 DCHECK(out.IsFpuRegister()) << out;
3278 if (is_float) {
3279 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3280 } else {
3281 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3282 }
3283
3284 // And remove the temporary stack space we allocated.
3285 __ addl(ESP, Immediate(2 * elem_size));
3286}
3287
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003288
3289void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3290 DCHECK(instruction->IsDiv() || instruction->IsRem());
3291
3292 LocationSummary* locations = instruction->GetLocations();
3293 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003294 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003295
3296 Register out_register = locations->Out().AsRegister<Register>();
3297 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003298 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003299
3300 DCHECK(imm == 1 || imm == -1);
3301
3302 if (instruction->IsRem()) {
3303 __ xorl(out_register, out_register);
3304 } else {
3305 __ movl(out_register, input_register);
3306 if (imm == -1) {
3307 __ negl(out_register);
3308 }
3309 }
3310}
3311
3312
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003313void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003314 LocationSummary* locations = instruction->GetLocations();
3315
3316 Register out_register = locations->Out().AsRegister<Register>();
3317 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003318 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003319 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3320 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003321
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003322 Register num = locations->GetTemp(0).AsRegister<Register>();
3323
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003324 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003325 __ testl(input_register, input_register);
3326 __ cmovl(kGreaterEqual, num, input_register);
3327 int shift = CTZ(imm);
3328 __ sarl(num, Immediate(shift));
3329
3330 if (imm < 0) {
3331 __ negl(num);
3332 }
3333
3334 __ movl(out_register, num);
3335}
3336
3337void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3338 DCHECK(instruction->IsDiv() || instruction->IsRem());
3339
3340 LocationSummary* locations = instruction->GetLocations();
3341 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3342
3343 Register eax = locations->InAt(0).AsRegister<Register>();
3344 Register out = locations->Out().AsRegister<Register>();
3345 Register num;
3346 Register edx;
3347
3348 if (instruction->IsDiv()) {
3349 edx = locations->GetTemp(0).AsRegister<Register>();
3350 num = locations->GetTemp(1).AsRegister<Register>();
3351 } else {
3352 edx = locations->Out().AsRegister<Register>();
3353 num = locations->GetTemp(0).AsRegister<Register>();
3354 }
3355
3356 DCHECK_EQ(EAX, eax);
3357 DCHECK_EQ(EDX, edx);
3358 if (instruction->IsDiv()) {
3359 DCHECK_EQ(EAX, out);
3360 } else {
3361 DCHECK_EQ(EDX, out);
3362 }
3363
3364 int64_t magic;
3365 int shift;
3366 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3367
Mark Mendell0c9497d2015-08-21 09:30:05 -04003368 NearLabel ndiv;
3369 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003370 // If numerator is 0, the result is 0, no computation needed.
3371 __ testl(eax, eax);
3372 __ j(kNotEqual, &ndiv);
3373
3374 __ xorl(out, out);
3375 __ jmp(&end);
3376
3377 __ Bind(&ndiv);
3378
3379 // Save the numerator.
3380 __ movl(num, eax);
3381
3382 // EAX = magic
3383 __ movl(eax, Immediate(magic));
3384
3385 // EDX:EAX = magic * numerator
3386 __ imull(num);
3387
3388 if (imm > 0 && magic < 0) {
3389 // EDX += num
3390 __ addl(edx, num);
3391 } else if (imm < 0 && magic > 0) {
3392 __ subl(edx, num);
3393 }
3394
3395 // Shift if needed.
3396 if (shift != 0) {
3397 __ sarl(edx, Immediate(shift));
3398 }
3399
3400 // EDX += 1 if EDX < 0
3401 __ movl(eax, edx);
3402 __ shrl(edx, Immediate(31));
3403 __ addl(edx, eax);
3404
3405 if (instruction->IsRem()) {
3406 __ movl(eax, num);
3407 __ imull(edx, Immediate(imm));
3408 __ subl(eax, edx);
3409 __ movl(edx, eax);
3410 } else {
3411 __ movl(eax, edx);
3412 }
3413 __ Bind(&end);
3414}
3415
Calin Juravlebacfec32014-11-14 15:54:36 +00003416void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3417 DCHECK(instruction->IsDiv() || instruction->IsRem());
3418
3419 LocationSummary* locations = instruction->GetLocations();
3420 Location out = locations->Out();
3421 Location first = locations->InAt(0);
3422 Location second = locations->InAt(1);
3423 bool is_div = instruction->IsDiv();
3424
3425 switch (instruction->GetResultType()) {
3426 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003427 DCHECK_EQ(EAX, first.AsRegister<Register>());
3428 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003429
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003430 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003431 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003432
3433 if (imm == 0) {
3434 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3435 } else if (imm == 1 || imm == -1) {
3436 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003437 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003438 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003439 } else {
3440 DCHECK(imm <= -2 || imm >= 2);
3441 GenerateDivRemWithAnyConstant(instruction);
3442 }
3443 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003444 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3445 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003446 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003447
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003448 Register second_reg = second.AsRegister<Register>();
3449 // 0x80000000/-1 triggers an arithmetic exception!
3450 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3451 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003452
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003453 __ cmpl(second_reg, Immediate(-1));
3454 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003455
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003456 // edx:eax <- sign-extended of eax
3457 __ cdq();
3458 // eax = quotient, edx = remainder
3459 __ idivl(second_reg);
3460 __ Bind(slow_path->GetExitLabel());
3461 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003462 break;
3463 }
3464
3465 case Primitive::kPrimLong: {
3466 InvokeRuntimeCallingConvention calling_convention;
3467 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3468 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3469 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3470 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3471 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3472 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3473
3474 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003475 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3476 instruction,
3477 instruction->GetDexPc(),
3478 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003479 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003480 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003481 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3482 instruction,
3483 instruction->GetDexPc(),
3484 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003485 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003486 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003487 break;
3488 }
3489
3490 default:
3491 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3492 }
3493}
3494
Calin Juravle7c4954d2014-10-28 16:57:40 +00003495void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003496 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003497 ? LocationSummary::kCall
3498 : LocationSummary::kNoCall;
3499 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3500
Calin Juravle7c4954d2014-10-28 16:57:40 +00003501 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003502 case Primitive::kPrimInt: {
3503 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003504 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003505 locations->SetOut(Location::SameAsFirstInput());
3506 // Intel uses edx:eax as the dividend.
3507 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003508 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3509 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3510 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003511 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003512 locations->AddTemp(Location::RequiresRegister());
3513 }
Calin Juravled0d48522014-11-04 16:40:20 +00003514 break;
3515 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003516 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003517 InvokeRuntimeCallingConvention calling_convention;
3518 locations->SetInAt(0, Location::RegisterPairLocation(
3519 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3520 locations->SetInAt(1, Location::RegisterPairLocation(
3521 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3522 // Runtime helper puts the result in EAX, EDX.
3523 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003524 break;
3525 }
3526 case Primitive::kPrimFloat:
3527 case Primitive::kPrimDouble: {
3528 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003529 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3530 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003531 } else if (div->InputAt(1)->IsConstant()) {
3532 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003533 } else {
3534 locations->SetInAt(1, Location::Any());
3535 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003536 locations->SetOut(Location::SameAsFirstInput());
3537 break;
3538 }
3539
3540 default:
3541 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3542 }
3543}
3544
3545void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3546 LocationSummary* locations = div->GetLocations();
3547 Location first = locations->InAt(0);
3548 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003549
3550 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003551 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003552 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003553 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003554 break;
3555 }
3556
3557 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003558 if (second.IsFpuRegister()) {
3559 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3560 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3561 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003562 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003563 __ divss(first.AsFpuRegister<XmmRegister>(),
3564 codegen_->LiteralFloatAddress(
3565 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3566 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3567 } else {
3568 DCHECK(second.IsStackSlot());
3569 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3570 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003571 break;
3572 }
3573
3574 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003575 if (second.IsFpuRegister()) {
3576 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3577 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3578 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003579 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003580 __ divsd(first.AsFpuRegister<XmmRegister>(),
3581 codegen_->LiteralDoubleAddress(
3582 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3583 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3584 } else {
3585 DCHECK(second.IsDoubleStackSlot());
3586 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3587 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003588 break;
3589 }
3590
3591 default:
3592 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3593 }
3594}
3595
Calin Juravlebacfec32014-11-14 15:54:36 +00003596void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003597 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003598
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003599 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3600 ? LocationSummary::kCall
3601 : LocationSummary::kNoCall;
3602 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003603
Calin Juravled2ec87d2014-12-08 14:24:46 +00003604 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003605 case Primitive::kPrimInt: {
3606 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003607 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003608 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003609 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3610 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3611 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003612 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003613 locations->AddTemp(Location::RequiresRegister());
3614 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003615 break;
3616 }
3617 case Primitive::kPrimLong: {
3618 InvokeRuntimeCallingConvention calling_convention;
3619 locations->SetInAt(0, Location::RegisterPairLocation(
3620 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3621 locations->SetInAt(1, Location::RegisterPairLocation(
3622 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3623 // Runtime helper puts the result in EAX, EDX.
3624 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3625 break;
3626 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003627 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003628 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003629 locations->SetInAt(0, Location::Any());
3630 locations->SetInAt(1, Location::Any());
3631 locations->SetOut(Location::RequiresFpuRegister());
3632 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003633 break;
3634 }
3635
3636 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003637 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003638 }
3639}
3640
3641void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3642 Primitive::Type type = rem->GetResultType();
3643 switch (type) {
3644 case Primitive::kPrimInt:
3645 case Primitive::kPrimLong: {
3646 GenerateDivRemIntegral(rem);
3647 break;
3648 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003649 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003650 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003651 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003652 break;
3653 }
3654 default:
3655 LOG(FATAL) << "Unexpected rem type " << type;
3656 }
3657}
3658
Calin Juravled0d48522014-11-04 16:40:20 +00003659void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003660 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3661 ? LocationSummary::kCallOnSlowPath
3662 : LocationSummary::kNoCall;
3663 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003664 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003665 case Primitive::kPrimByte:
3666 case Primitive::kPrimChar:
3667 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003668 case Primitive::kPrimInt: {
3669 locations->SetInAt(0, Location::Any());
3670 break;
3671 }
3672 case Primitive::kPrimLong: {
3673 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3674 if (!instruction->IsConstant()) {
3675 locations->AddTemp(Location::RequiresRegister());
3676 }
3677 break;
3678 }
3679 default:
3680 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3681 }
Calin Juravled0d48522014-11-04 16:40:20 +00003682 if (instruction->HasUses()) {
3683 locations->SetOut(Location::SameAsFirstInput());
3684 }
3685}
3686
3687void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003688 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003689 codegen_->AddSlowPath(slow_path);
3690
3691 LocationSummary* locations = instruction->GetLocations();
3692 Location value = locations->InAt(0);
3693
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003694 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003695 case Primitive::kPrimByte:
3696 case Primitive::kPrimChar:
3697 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003698 case Primitive::kPrimInt: {
3699 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003700 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003701 __ j(kEqual, slow_path->GetEntryLabel());
3702 } else if (value.IsStackSlot()) {
3703 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3704 __ j(kEqual, slow_path->GetEntryLabel());
3705 } else {
3706 DCHECK(value.IsConstant()) << value;
3707 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3708 __ jmp(slow_path->GetEntryLabel());
3709 }
3710 }
3711 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003712 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003713 case Primitive::kPrimLong: {
3714 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003715 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003716 __ movl(temp, value.AsRegisterPairLow<Register>());
3717 __ orl(temp, value.AsRegisterPairHigh<Register>());
3718 __ j(kEqual, slow_path->GetEntryLabel());
3719 } else {
3720 DCHECK(value.IsConstant()) << value;
3721 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3722 __ jmp(slow_path->GetEntryLabel());
3723 }
3724 }
3725 break;
3726 }
3727 default:
3728 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003729 }
Calin Juravled0d48522014-11-04 16:40:20 +00003730}
3731
Calin Juravle9aec02f2014-11-18 23:06:35 +00003732void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3733 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3734
3735 LocationSummary* locations =
3736 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3737
3738 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003739 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003740 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003741 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003742 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003743 // The shift count needs to be in CL or a constant.
3744 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003745 locations->SetOut(Location::SameAsFirstInput());
3746 break;
3747 }
3748 default:
3749 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3750 }
3751}
3752
3753void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3754 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3755
3756 LocationSummary* locations = op->GetLocations();
3757 Location first = locations->InAt(0);
3758 Location second = locations->InAt(1);
3759 DCHECK(first.Equals(locations->Out()));
3760
3761 switch (op->GetResultType()) {
3762 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003763 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003764 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003765 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003766 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003767 DCHECK_EQ(ECX, second_reg);
3768 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003769 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003770 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003771 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003772 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003773 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003774 }
3775 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003776 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3777 if (shift == 0) {
3778 return;
3779 }
3780 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003781 if (op->IsShl()) {
3782 __ shll(first_reg, imm);
3783 } else if (op->IsShr()) {
3784 __ sarl(first_reg, imm);
3785 } else {
3786 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003787 }
3788 }
3789 break;
3790 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003791 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003792 if (second.IsRegister()) {
3793 Register second_reg = second.AsRegister<Register>();
3794 DCHECK_EQ(ECX, second_reg);
3795 if (op->IsShl()) {
3796 GenerateShlLong(first, second_reg);
3797 } else if (op->IsShr()) {
3798 GenerateShrLong(first, second_reg);
3799 } else {
3800 GenerateUShrLong(first, second_reg);
3801 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003802 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003803 // Shift by a constant.
3804 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3805 // Nothing to do if the shift is 0, as the input is already the output.
3806 if (shift != 0) {
3807 if (op->IsShl()) {
3808 GenerateShlLong(first, shift);
3809 } else if (op->IsShr()) {
3810 GenerateShrLong(first, shift);
3811 } else {
3812 GenerateUShrLong(first, shift);
3813 }
3814 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003815 }
3816 break;
3817 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003818 default:
3819 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3820 }
3821}
3822
Mark P Mendell73945692015-04-29 14:56:17 +00003823void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3824 Register low = loc.AsRegisterPairLow<Register>();
3825 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003826 if (shift == 1) {
3827 // This is just an addition.
3828 __ addl(low, low);
3829 __ adcl(high, high);
3830 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003831 // Shift by 32 is easy. High gets low, and low gets 0.
3832 codegen_->EmitParallelMoves(
3833 loc.ToLow(),
3834 loc.ToHigh(),
3835 Primitive::kPrimInt,
3836 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3837 loc.ToLow(),
3838 Primitive::kPrimInt);
3839 } else if (shift > 32) {
3840 // Low part becomes 0. High part is low part << (shift-32).
3841 __ movl(high, low);
3842 __ shll(high, Immediate(shift - 32));
3843 __ xorl(low, low);
3844 } else {
3845 // Between 1 and 31.
3846 __ shld(high, low, Immediate(shift));
3847 __ shll(low, Immediate(shift));
3848 }
3849}
3850
Calin Juravle9aec02f2014-11-18 23:06:35 +00003851void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003852 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003853 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3854 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3855 __ testl(shifter, Immediate(32));
3856 __ j(kEqual, &done);
3857 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3858 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3859 __ Bind(&done);
3860}
3861
Mark P Mendell73945692015-04-29 14:56:17 +00003862void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3863 Register low = loc.AsRegisterPairLow<Register>();
3864 Register high = loc.AsRegisterPairHigh<Register>();
3865 if (shift == 32) {
3866 // Need to copy the sign.
3867 DCHECK_NE(low, high);
3868 __ movl(low, high);
3869 __ sarl(high, Immediate(31));
3870 } else if (shift > 32) {
3871 DCHECK_NE(low, high);
3872 // High part becomes sign. Low part is shifted by shift - 32.
3873 __ movl(low, high);
3874 __ sarl(high, Immediate(31));
3875 __ sarl(low, Immediate(shift - 32));
3876 } else {
3877 // Between 1 and 31.
3878 __ shrd(low, high, Immediate(shift));
3879 __ sarl(high, Immediate(shift));
3880 }
3881}
3882
Calin Juravle9aec02f2014-11-18 23:06:35 +00003883void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003884 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003885 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3886 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3887 __ testl(shifter, Immediate(32));
3888 __ j(kEqual, &done);
3889 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3890 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3891 __ Bind(&done);
3892}
3893
Mark P Mendell73945692015-04-29 14:56:17 +00003894void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3895 Register low = loc.AsRegisterPairLow<Register>();
3896 Register high = loc.AsRegisterPairHigh<Register>();
3897 if (shift == 32) {
3898 // Shift by 32 is easy. Low gets high, and high gets 0.
3899 codegen_->EmitParallelMoves(
3900 loc.ToHigh(),
3901 loc.ToLow(),
3902 Primitive::kPrimInt,
3903 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3904 loc.ToHigh(),
3905 Primitive::kPrimInt);
3906 } else if (shift > 32) {
3907 // Low part is high >> (shift - 32). High part becomes 0.
3908 __ movl(low, high);
3909 __ shrl(low, Immediate(shift - 32));
3910 __ xorl(high, high);
3911 } else {
3912 // Between 1 and 31.
3913 __ shrd(low, high, Immediate(shift));
3914 __ shrl(high, Immediate(shift));
3915 }
3916}
3917
Calin Juravle9aec02f2014-11-18 23:06:35 +00003918void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003919 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003920 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3921 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3922 __ testl(shifter, Immediate(32));
3923 __ j(kEqual, &done);
3924 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3925 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3926 __ Bind(&done);
3927}
3928
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003929void LocationsBuilderX86::VisitRor(HRor* ror) {
3930 LocationSummary* locations =
3931 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3932
3933 switch (ror->GetResultType()) {
3934 case Primitive::kPrimLong:
3935 // Add the temporary needed.
3936 locations->AddTemp(Location::RequiresRegister());
3937 FALLTHROUGH_INTENDED;
3938 case Primitive::kPrimInt:
3939 locations->SetInAt(0, Location::RequiresRegister());
3940 // The shift count needs to be in CL (unless it is a constant).
3941 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3942 locations->SetOut(Location::SameAsFirstInput());
3943 break;
3944 default:
3945 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3946 UNREACHABLE();
3947 }
3948}
3949
3950void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3951 LocationSummary* locations = ror->GetLocations();
3952 Location first = locations->InAt(0);
3953 Location second = locations->InAt(1);
3954
3955 if (ror->GetResultType() == Primitive::kPrimInt) {
3956 Register first_reg = first.AsRegister<Register>();
3957 if (second.IsRegister()) {
3958 Register second_reg = second.AsRegister<Register>();
3959 __ rorl(first_reg, second_reg);
3960 } else {
3961 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3962 __ rorl(first_reg, imm);
3963 }
3964 return;
3965 }
3966
3967 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3968 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3969 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3970 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3971 if (second.IsRegister()) {
3972 Register second_reg = second.AsRegister<Register>();
3973 DCHECK_EQ(second_reg, ECX);
3974 __ movl(temp_reg, first_reg_hi);
3975 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3976 __ shrd(first_reg_lo, temp_reg, second_reg);
3977 __ movl(temp_reg, first_reg_hi);
3978 __ testl(second_reg, Immediate(32));
3979 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3980 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3981 } else {
3982 int32_t shift_amt =
3983 CodeGenerator::GetInt64ValueOf(second.GetConstant()) & kMaxLongShiftValue;
3984 if (shift_amt == 0) {
3985 // Already fine.
3986 return;
3987 }
3988 if (shift_amt == 32) {
3989 // Just swap.
3990 __ movl(temp_reg, first_reg_lo);
3991 __ movl(first_reg_lo, first_reg_hi);
3992 __ movl(first_reg_hi, temp_reg);
3993 return;
3994 }
3995
3996 Immediate imm(shift_amt);
3997 // Save the constents of the low value.
3998 __ movl(temp_reg, first_reg_lo);
3999
4000 // Shift right into low, feeding bits from high.
4001 __ shrd(first_reg_lo, first_reg_hi, imm);
4002
4003 // Shift right into high, feeding bits from the original low.
4004 __ shrd(first_reg_hi, temp_reg, imm);
4005
4006 // Swap if needed.
4007 if (shift_amt > 32) {
4008 __ movl(temp_reg, first_reg_lo);
4009 __ movl(first_reg_lo, first_reg_hi);
4010 __ movl(first_reg_hi, temp_reg);
4011 }
4012 }
4013}
4014
Calin Juravle9aec02f2014-11-18 23:06:35 +00004015void LocationsBuilderX86::VisitShl(HShl* shl) {
4016 HandleShift(shl);
4017}
4018
4019void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4020 HandleShift(shl);
4021}
4022
4023void LocationsBuilderX86::VisitShr(HShr* shr) {
4024 HandleShift(shr);
4025}
4026
4027void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4028 HandleShift(shr);
4029}
4030
4031void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4032 HandleShift(ushr);
4033}
4034
4035void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4036 HandleShift(ushr);
4037}
4038
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004039void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004040 LocationSummary* locations =
4041 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004042 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004043 if (instruction->IsStringAlloc()) {
4044 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4045 } else {
4046 InvokeRuntimeCallingConvention calling_convention;
4047 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4048 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4049 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004050}
4051
4052void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004053 // Note: if heap poisoning is enabled, the entry point takes cares
4054 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004055 if (instruction->IsStringAlloc()) {
4056 // String is allocated through StringFactory. Call NewEmptyString entry point.
4057 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4058 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
4059 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4060 __ call(Address(temp, code_offset.Int32Value()));
4061 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4062 } else {
4063 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4064 instruction,
4065 instruction->GetDexPc(),
4066 nullptr);
4067 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4068 DCHECK(!codegen_->IsLeafMethod());
4069 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004070}
4071
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004072void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4073 LocationSummary* locations =
4074 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4075 locations->SetOut(Location::RegisterLocation(EAX));
4076 InvokeRuntimeCallingConvention calling_convention;
4077 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004078 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004079 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004080}
4081
4082void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4083 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004084 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004085 // Note: if heap poisoning is enabled, the entry point takes cares
4086 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004087 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4088 instruction,
4089 instruction->GetDexPc(),
4090 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004091 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004092 DCHECK(!codegen_->IsLeafMethod());
4093}
4094
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004095void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004096 LocationSummary* locations =
4097 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004098 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4099 if (location.IsStackSlot()) {
4100 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4101 } else if (location.IsDoubleStackSlot()) {
4102 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004103 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004104 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004105}
4106
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004107void InstructionCodeGeneratorX86::VisitParameterValue(
4108 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4109}
4110
4111void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4112 LocationSummary* locations =
4113 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4114 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4115}
4116
4117void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004118}
4119
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004120void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4121 LocationSummary* locations =
4122 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4123 locations->SetInAt(0, Location::RequiresRegister());
4124 locations->SetOut(Location::RequiresRegister());
4125}
4126
4127void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4128 LocationSummary* locations = instruction->GetLocations();
4129 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004130 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004131 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4132 instruction->GetIndex(), kX86PointerSize).SizeValue();
4133 } else {
4134 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4135 instruction->GetIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
4136 }
4137 __ movl(locations->Out().AsRegister<Register>(),
4138 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4139}
4140
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004141void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004142 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004143 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004144 locations->SetInAt(0, Location::RequiresRegister());
4145 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004146}
4147
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004148void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4149 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004150 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004151 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004152 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004153 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004154 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004155 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004156 break;
4157
4158 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004159 __ notl(out.AsRegisterPairLow<Register>());
4160 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004161 break;
4162
4163 default:
4164 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4165 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004166}
4167
David Brazdil66d126e2015-04-03 16:02:44 +01004168void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4169 LocationSummary* locations =
4170 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4171 locations->SetInAt(0, Location::RequiresRegister());
4172 locations->SetOut(Location::SameAsFirstInput());
4173}
4174
4175void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004176 LocationSummary* locations = bool_not->GetLocations();
4177 Location in = locations->InAt(0);
4178 Location out = locations->Out();
4179 DCHECK(in.Equals(out));
4180 __ xorl(out.AsRegister<Register>(), Immediate(1));
4181}
4182
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004183void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004184 LocationSummary* locations =
4185 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004186 switch (compare->InputAt(0)->GetType()) {
Aart Bika19616e2016-02-01 18:57:58 -08004187 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004188 case Primitive::kPrimLong: {
4189 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004190 locations->SetInAt(1, Location::Any());
4191 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4192 break;
4193 }
4194 case Primitive::kPrimFloat:
4195 case Primitive::kPrimDouble: {
4196 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004197 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4198 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4199 } else if (compare->InputAt(1)->IsConstant()) {
4200 locations->SetInAt(1, Location::RequiresFpuRegister());
4201 } else {
4202 locations->SetInAt(1, Location::Any());
4203 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004204 locations->SetOut(Location::RequiresRegister());
4205 break;
4206 }
4207 default:
4208 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4209 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004210}
4211
4212void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004213 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004214 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004215 Location left = locations->InAt(0);
4216 Location right = locations->InAt(1);
4217
Mark Mendell0c9497d2015-08-21 09:30:05 -04004218 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004219 Condition less_cond = kLess;
4220
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004221 switch (compare->InputAt(0)->GetType()) {
Aart Bika19616e2016-02-01 18:57:58 -08004222 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004223 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004224 break;
4225 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004226 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004227 Register left_low = left.AsRegisterPairLow<Register>();
4228 Register left_high = left.AsRegisterPairHigh<Register>();
4229 int32_t val_low = 0;
4230 int32_t val_high = 0;
4231 bool right_is_const = false;
4232
4233 if (right.IsConstant()) {
4234 DCHECK(right.GetConstant()->IsLongConstant());
4235 right_is_const = true;
4236 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4237 val_low = Low32Bits(val);
4238 val_high = High32Bits(val);
4239 }
4240
Calin Juravleddb7df22014-11-25 20:56:51 +00004241 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004242 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004243 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004244 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004245 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004246 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004247 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004248 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004249 __ j(kLess, &less); // Signed compare.
4250 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004251 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004252 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004253 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004254 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004255 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004256 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004257 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004258 }
Aart Bika19616e2016-02-01 18:57:58 -08004259 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004260 break;
4261 }
4262 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004263 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004264 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004265 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004266 break;
4267 }
4268 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004269 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004270 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004271 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004272 break;
4273 }
4274 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004275 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004276 }
Aart Bika19616e2016-02-01 18:57:58 -08004277
Calin Juravleddb7df22014-11-25 20:56:51 +00004278 __ movl(out, Immediate(0));
4279 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004280 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004281
4282 __ Bind(&greater);
4283 __ movl(out, Immediate(1));
4284 __ jmp(&done);
4285
4286 __ Bind(&less);
4287 __ movl(out, Immediate(-1));
4288
4289 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004290}
4291
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004292void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004293 LocationSummary* locations =
4294 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004295 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4296 locations->SetInAt(i, Location::Any());
4297 }
4298 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004299}
4300
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004301void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004302 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004303}
4304
Roland Levillain7c1559a2015-12-15 10:55:36 +00004305void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004306 /*
4307 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4308 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4309 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4310 */
4311 switch (kind) {
4312 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004313 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004314 break;
4315 }
4316 case MemBarrierKind::kAnyStore:
4317 case MemBarrierKind::kLoadAny:
4318 case MemBarrierKind::kStoreStore: {
4319 // nop
4320 break;
4321 }
4322 default:
4323 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004324 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004325}
4326
Vladimir Markodc151b22015-10-15 18:02:30 +01004327HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4328 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4329 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004330 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4331
4332 // We disable pc-relative load when there is an irreducible loop, as the optimization
4333 // is incompatible with it.
4334 if (GetGraph()->HasIrreducibleLoops() &&
4335 (dispatch_info.method_load_kind ==
4336 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4337 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4338 }
4339 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004340 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4341 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4342 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4343 // (Though the direct CALL ptr16:32 is available for consideration).
4344 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004345 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004346 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004347 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004348 0u
4349 };
4350 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004351 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004352 }
4353}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004354
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004355Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4356 Register temp) {
4357 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004358 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004359 if (!invoke->GetLocations()->Intrinsified()) {
4360 return location.AsRegister<Register>();
4361 }
4362 // For intrinsics we allow any location, so it may be on the stack.
4363 if (!location.IsRegister()) {
4364 __ movl(temp, Address(ESP, location.GetStackIndex()));
4365 return temp;
4366 }
4367 // For register locations, check if the register was saved. If so, get it from the stack.
4368 // Note: There is a chance that the register was saved but not overwritten, so we could
4369 // save one load. However, since this is just an intrinsic slow path we prefer this
4370 // simple and more robust approach rather that trying to determine if that's the case.
4371 SlowPathCode* slow_path = GetCurrentSlowPath();
4372 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4373 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4374 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4375 __ movl(temp, Address(ESP, stack_offset));
4376 return temp;
4377 }
4378 return location.AsRegister<Register>();
4379}
4380
Vladimir Marko58155012015-08-19 12:49:41 +00004381void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4382 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4383 switch (invoke->GetMethodLoadKind()) {
4384 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4385 // temp = thread->string_init_entrypoint
4386 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4387 break;
4388 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004389 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004390 break;
4391 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4392 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4393 break;
4394 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4395 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4396 method_patches_.emplace_back(invoke->GetTargetMethod());
4397 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4398 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004399 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4400 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4401 temp.AsRegister<Register>());
4402 uint32_t offset = invoke->GetDexCacheArrayOffset();
4403 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4404 // Add the patch entry and bind its label at the end of the instruction.
4405 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4406 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4407 break;
4408 }
Vladimir Marko58155012015-08-19 12:49:41 +00004409 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004410 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004411 Register method_reg;
4412 Register reg = temp.AsRegister<Register>();
4413 if (current_method.IsRegister()) {
4414 method_reg = current_method.AsRegister<Register>();
4415 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004416 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004417 DCHECK(!current_method.IsValid());
4418 method_reg = reg;
4419 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4420 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004421 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004422 __ movl(reg, Address(method_reg,
4423 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004424 // temp = temp[index_in_cache]
4425 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4426 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4427 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004428 }
Vladimir Marko58155012015-08-19 12:49:41 +00004429 }
4430
4431 switch (invoke->GetCodePtrLocation()) {
4432 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4433 __ call(GetFrameEntryLabel());
4434 break;
4435 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4436 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4437 Label* label = &relative_call_patches_.back().label;
4438 __ call(label); // Bind to the patch label, override at link time.
4439 __ Bind(label); // Bind the label at the end of the "call" insn.
4440 break;
4441 }
4442 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4443 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004444 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4445 LOG(FATAL) << "Unsupported";
4446 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004447 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4448 // (callee_method + offset_of_quick_compiled_code)()
4449 __ call(Address(callee_method.AsRegister<Register>(),
4450 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4451 kX86WordSize).Int32Value()));
4452 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004453 }
4454
4455 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004456}
4457
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004458void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4459 Register temp = temp_in.AsRegister<Register>();
4460 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4461 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004462
4463 // Use the calling convention instead of the location of the receiver, as
4464 // intrinsics may have put the receiver in a different register. In the intrinsics
4465 // slow path, the arguments have been moved to the right place, so here we are
4466 // guaranteed that the receiver is the first register of the calling convention.
4467 InvokeDexCallingConvention calling_convention;
4468 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004469 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004470 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004471 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004472 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004473 // Instead of simply (possibly) unpoisoning `temp` here, we should
4474 // emit a read barrier for the previous class reference load.
4475 // However this is not required in practice, as this is an
4476 // intermediate/temporary reference and because the current
4477 // concurrent copying collector keeps the from-space memory
4478 // intact/accessible until the end of the marking phase (the
4479 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004480 __ MaybeUnpoisonHeapReference(temp);
4481 // temp = temp->GetMethodAt(method_offset);
4482 __ movl(temp, Address(temp, method_offset));
4483 // call temp->GetEntryPoint();
4484 __ call(Address(
4485 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4486}
4487
Vladimir Marko58155012015-08-19 12:49:41 +00004488void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4489 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004490 size_t size =
4491 method_patches_.size() +
4492 relative_call_patches_.size() +
4493 pc_relative_dex_cache_patches_.size();
4494 linker_patches->reserve(size);
4495 // The label points to the end of the "movl" insn but the literal offset for method
4496 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4497 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004498 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004499 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004500 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4501 info.target_method.dex_file,
4502 info.target_method.dex_method_index));
4503 }
4504 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004505 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004506 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4507 info.target_method.dex_file,
4508 info.target_method.dex_method_index));
4509 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004510 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4511 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4512 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4513 &info.target_dex_file,
4514 GetMethodAddressOffset(),
4515 info.element_offset));
4516 }
Vladimir Marko58155012015-08-19 12:49:41 +00004517}
4518
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004519void CodeGeneratorX86::MarkGCCard(Register temp,
4520 Register card,
4521 Register object,
4522 Register value,
4523 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004524 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004525 if (value_can_be_null) {
4526 __ testl(value, value);
4527 __ j(kEqual, &is_null);
4528 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004529 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4530 __ movl(temp, object);
4531 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004532 __ movb(Address(temp, card, TIMES_1, 0),
4533 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004534 if (value_can_be_null) {
4535 __ Bind(&is_null);
4536 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004537}
4538
Calin Juravle52c48962014-12-16 17:02:57 +00004539void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4540 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004541
4542 bool object_field_get_with_read_barrier =
4543 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004544 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004545 new (GetGraph()->GetArena()) LocationSummary(instruction,
4546 kEmitCompilerReadBarrier ?
4547 LocationSummary::kCallOnSlowPath :
4548 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004549 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004550
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004551 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4552 locations->SetOut(Location::RequiresFpuRegister());
4553 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004554 // The output overlaps in case of long: we don't want the low move
4555 // to overwrite the object's location. Likewise, in the case of
4556 // an object field get with read barriers enabled, we do not want
4557 // the move to overwrite the object's location, as we need it to emit
4558 // the read barrier.
4559 locations->SetOut(
4560 Location::RequiresRegister(),
4561 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4562 Location::kOutputOverlap :
4563 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004564 }
Calin Juravle52c48962014-12-16 17:02:57 +00004565
4566 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4567 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004568 // So we use an XMM register as a temp to achieve atomicity (first
4569 // load the temp into the XMM and then copy the XMM into the
4570 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004571 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004572 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4573 // We need a temporary register for the read barrier marking slow
4574 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4575 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004576 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004577}
4578
Calin Juravle52c48962014-12-16 17:02:57 +00004579void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4580 const FieldInfo& field_info) {
4581 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004582
Calin Juravle52c48962014-12-16 17:02:57 +00004583 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004584 Location base_loc = locations->InAt(0);
4585 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004586 Location out = locations->Out();
4587 bool is_volatile = field_info.IsVolatile();
4588 Primitive::Type field_type = field_info.GetFieldType();
4589 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4590
4591 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004592 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004593 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004594 break;
4595 }
4596
4597 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004598 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004599 break;
4600 }
4601
4602 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004603 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004604 break;
4605 }
4606
4607 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004608 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004609 break;
4610 }
4611
4612 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004613 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004614 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004615
4616 case Primitive::kPrimNot: {
4617 // /* HeapReference<Object> */ out = *(base + offset)
4618 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4619 Location temp_loc = locations->GetTemp(0);
4620 // Note that a potential implicit null check is handled in this
4621 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4622 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4623 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4624 if (is_volatile) {
4625 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4626 }
4627 } else {
4628 __ movl(out.AsRegister<Register>(), Address(base, offset));
4629 codegen_->MaybeRecordImplicitNullCheck(instruction);
4630 if (is_volatile) {
4631 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4632 }
4633 // If read barriers are enabled, emit read barriers other than
4634 // Baker's using a slow path (and also unpoison the loaded
4635 // reference, if heap poisoning is enabled).
4636 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4637 }
4638 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004639 }
4640
4641 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004642 if (is_volatile) {
4643 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4644 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004645 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004646 __ movd(out.AsRegisterPairLow<Register>(), temp);
4647 __ psrlq(temp, Immediate(32));
4648 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4649 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004650 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004651 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004652 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004653 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4654 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655 break;
4656 }
4657
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004658 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004659 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004660 break;
4661 }
4662
4663 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004664 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004665 break;
4666 }
4667
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004668 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004669 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004670 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004671 }
Calin Juravle52c48962014-12-16 17:02:57 +00004672
Roland Levillain7c1559a2015-12-15 10:55:36 +00004673 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4674 // Potential implicit null checks, in the case of reference or
4675 // long fields, are handled in the previous switch statement.
4676 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004677 codegen_->MaybeRecordImplicitNullCheck(instruction);
4678 }
4679
Calin Juravle52c48962014-12-16 17:02:57 +00004680 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004681 if (field_type == Primitive::kPrimNot) {
4682 // Memory barriers, in the case of references, are also handled
4683 // in the previous switch statement.
4684 } else {
4685 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4686 }
Roland Levillain4d027112015-07-01 15:41:14 +01004687 }
Calin Juravle52c48962014-12-16 17:02:57 +00004688}
4689
4690void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4691 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4692
4693 LocationSummary* locations =
4694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4695 locations->SetInAt(0, Location::RequiresRegister());
4696 bool is_volatile = field_info.IsVolatile();
4697 Primitive::Type field_type = field_info.GetFieldType();
4698 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4699 || (field_type == Primitive::kPrimByte);
4700
4701 // The register allocator does not support multiple
4702 // inputs that die at entry with one in a specific register.
4703 if (is_byte_type) {
4704 // Ensure the value is in a byte register.
4705 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004706 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004707 if (is_volatile && field_type == Primitive::kPrimDouble) {
4708 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4709 locations->SetInAt(1, Location::RequiresFpuRegister());
4710 } else {
4711 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4712 }
4713 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4714 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004715 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004716
Calin Juravle52c48962014-12-16 17:02:57 +00004717 // 64bits value can be atomically written to an address with movsd and an XMM register.
4718 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4719 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4720 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4721 // isolated cases when we need this it isn't worth adding the extra complexity.
4722 locations->AddTemp(Location::RequiresFpuRegister());
4723 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004724 } else {
4725 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4726
4727 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4728 // Temporary registers for the write barrier.
4729 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4730 // Ensure the card is in a byte register.
4731 locations->AddTemp(Location::RegisterLocation(ECX));
4732 }
Calin Juravle52c48962014-12-16 17:02:57 +00004733 }
4734}
4735
4736void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004737 const FieldInfo& field_info,
4738 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004739 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4740
4741 LocationSummary* locations = instruction->GetLocations();
4742 Register base = locations->InAt(0).AsRegister<Register>();
4743 Location value = locations->InAt(1);
4744 bool is_volatile = field_info.IsVolatile();
4745 Primitive::Type field_type = field_info.GetFieldType();
4746 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004747 bool needs_write_barrier =
4748 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004749
4750 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004751 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004752 }
4753
Mark Mendell81489372015-11-04 11:30:41 -05004754 bool maybe_record_implicit_null_check_done = false;
4755
Calin Juravle52c48962014-12-16 17:02:57 +00004756 switch (field_type) {
4757 case Primitive::kPrimBoolean:
4758 case Primitive::kPrimByte: {
4759 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4760 break;
4761 }
4762
4763 case Primitive::kPrimShort:
4764 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004765 if (value.IsConstant()) {
4766 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4767 __ movw(Address(base, offset), Immediate(v));
4768 } else {
4769 __ movw(Address(base, offset), value.AsRegister<Register>());
4770 }
Calin Juravle52c48962014-12-16 17:02:57 +00004771 break;
4772 }
4773
4774 case Primitive::kPrimInt:
4775 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004776 if (kPoisonHeapReferences && needs_write_barrier) {
4777 // Note that in the case where `value` is a null reference,
4778 // we do not enter this block, as the reference does not
4779 // need poisoning.
4780 DCHECK_EQ(field_type, Primitive::kPrimNot);
4781 Register temp = locations->GetTemp(0).AsRegister<Register>();
4782 __ movl(temp, value.AsRegister<Register>());
4783 __ PoisonHeapReference(temp);
4784 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004785 } else if (value.IsConstant()) {
4786 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4787 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004788 } else {
4789 __ movl(Address(base, offset), value.AsRegister<Register>());
4790 }
Calin Juravle52c48962014-12-16 17:02:57 +00004791 break;
4792 }
4793
4794 case Primitive::kPrimLong: {
4795 if (is_volatile) {
4796 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4797 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4798 __ movd(temp1, value.AsRegisterPairLow<Register>());
4799 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4800 __ punpckldq(temp1, temp2);
4801 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004802 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004803 } else if (value.IsConstant()) {
4804 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4805 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4806 codegen_->MaybeRecordImplicitNullCheck(instruction);
4807 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004808 } else {
4809 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004810 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004811 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4812 }
Mark Mendell81489372015-11-04 11:30:41 -05004813 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004814 break;
4815 }
4816
4817 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004818 if (value.IsConstant()) {
4819 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4820 __ movl(Address(base, offset), Immediate(v));
4821 } else {
4822 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4823 }
Calin Juravle52c48962014-12-16 17:02:57 +00004824 break;
4825 }
4826
4827 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004828 if (value.IsConstant()) {
4829 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4830 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4831 codegen_->MaybeRecordImplicitNullCheck(instruction);
4832 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4833 maybe_record_implicit_null_check_done = true;
4834 } else {
4835 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4836 }
Calin Juravle52c48962014-12-16 17:02:57 +00004837 break;
4838 }
4839
4840 case Primitive::kPrimVoid:
4841 LOG(FATAL) << "Unreachable type " << field_type;
4842 UNREACHABLE();
4843 }
4844
Mark Mendell81489372015-11-04 11:30:41 -05004845 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004846 codegen_->MaybeRecordImplicitNullCheck(instruction);
4847 }
4848
Roland Levillain4d027112015-07-01 15:41:14 +01004849 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004850 Register temp = locations->GetTemp(0).AsRegister<Register>();
4851 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004852 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004853 }
4854
Calin Juravle52c48962014-12-16 17:02:57 +00004855 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004856 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004857 }
4858}
4859
4860void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4861 HandleFieldGet(instruction, instruction->GetFieldInfo());
4862}
4863
4864void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4865 HandleFieldGet(instruction, instruction->GetFieldInfo());
4866}
4867
4868void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4869 HandleFieldSet(instruction, instruction->GetFieldInfo());
4870}
4871
4872void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004873 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004874}
4875
4876void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4877 HandleFieldSet(instruction, instruction->GetFieldInfo());
4878}
4879
4880void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004881 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004882}
4883
4884void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4885 HandleFieldGet(instruction, instruction->GetFieldInfo());
4886}
4887
4888void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4889 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004890}
4891
Calin Juravlee460d1d2015-09-29 04:52:17 +01004892void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4893 HUnresolvedInstanceFieldGet* instruction) {
4894 FieldAccessCallingConventionX86 calling_convention;
4895 codegen_->CreateUnresolvedFieldLocationSummary(
4896 instruction, instruction->GetFieldType(), calling_convention);
4897}
4898
4899void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4900 HUnresolvedInstanceFieldGet* instruction) {
4901 FieldAccessCallingConventionX86 calling_convention;
4902 codegen_->GenerateUnresolvedFieldAccess(instruction,
4903 instruction->GetFieldType(),
4904 instruction->GetFieldIndex(),
4905 instruction->GetDexPc(),
4906 calling_convention);
4907}
4908
4909void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4910 HUnresolvedInstanceFieldSet* instruction) {
4911 FieldAccessCallingConventionX86 calling_convention;
4912 codegen_->CreateUnresolvedFieldLocationSummary(
4913 instruction, instruction->GetFieldType(), calling_convention);
4914}
4915
4916void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4917 HUnresolvedInstanceFieldSet* instruction) {
4918 FieldAccessCallingConventionX86 calling_convention;
4919 codegen_->GenerateUnresolvedFieldAccess(instruction,
4920 instruction->GetFieldType(),
4921 instruction->GetFieldIndex(),
4922 instruction->GetDexPc(),
4923 calling_convention);
4924}
4925
4926void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4927 HUnresolvedStaticFieldGet* instruction) {
4928 FieldAccessCallingConventionX86 calling_convention;
4929 codegen_->CreateUnresolvedFieldLocationSummary(
4930 instruction, instruction->GetFieldType(), calling_convention);
4931}
4932
4933void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4934 HUnresolvedStaticFieldGet* instruction) {
4935 FieldAccessCallingConventionX86 calling_convention;
4936 codegen_->GenerateUnresolvedFieldAccess(instruction,
4937 instruction->GetFieldType(),
4938 instruction->GetFieldIndex(),
4939 instruction->GetDexPc(),
4940 calling_convention);
4941}
4942
4943void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4944 HUnresolvedStaticFieldSet* instruction) {
4945 FieldAccessCallingConventionX86 calling_convention;
4946 codegen_->CreateUnresolvedFieldLocationSummary(
4947 instruction, instruction->GetFieldType(), calling_convention);
4948}
4949
4950void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4951 HUnresolvedStaticFieldSet* instruction) {
4952 FieldAccessCallingConventionX86 calling_convention;
4953 codegen_->GenerateUnresolvedFieldAccess(instruction,
4954 instruction->GetFieldType(),
4955 instruction->GetFieldIndex(),
4956 instruction->GetDexPc(),
4957 calling_convention);
4958}
4959
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004960void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004961 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4962 ? LocationSummary::kCallOnSlowPath
4963 : LocationSummary::kNoCall;
4964 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4965 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004966 ? Location::RequiresRegister()
4967 : Location::Any();
4968 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004969 if (instruction->HasUses()) {
4970 locations->SetOut(Location::SameAsFirstInput());
4971 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004972}
4973
Calin Juravle2ae48182016-03-16 14:05:09 +00004974void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4975 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004976 return;
4977 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004978 LocationSummary* locations = instruction->GetLocations();
4979 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004980
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004981 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004982 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004983}
4984
Calin Juravle2ae48182016-03-16 14:05:09 +00004985void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004986 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004987 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004988
4989 LocationSummary* locations = instruction->GetLocations();
4990 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004991
4992 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004993 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004994 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004995 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004996 } else {
4997 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004998 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004999 __ jmp(slow_path->GetEntryLabel());
5000 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005001 }
5002 __ j(kEqual, slow_path->GetEntryLabel());
5003}
5004
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005005void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005006 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005007}
5008
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005009void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005010 bool object_array_get_with_read_barrier =
5011 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005012 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005013 new (GetGraph()->GetArena()) LocationSummary(instruction,
5014 object_array_get_with_read_barrier ?
5015 LocationSummary::kCallOnSlowPath :
5016 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005017 locations->SetInAt(0, Location::RequiresRegister());
5018 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005019 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5020 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5021 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005022 // The output overlaps in case of long: we don't want the low move
5023 // to overwrite the array's location. Likewise, in the case of an
5024 // object array get with read barriers enabled, we do not want the
5025 // move to overwrite the array's location, as we need it to emit
5026 // the read barrier.
5027 locations->SetOut(
5028 Location::RequiresRegister(),
5029 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5030 Location::kOutputOverlap :
5031 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005032 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005033 // We need a temporary register for the read barrier marking slow
5034 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5035 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5036 locations->AddTemp(Location::RequiresRegister());
5037 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005038}
5039
5040void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5041 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005042 Location obj_loc = locations->InAt(0);
5043 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005044 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005045 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005046
Calin Juravle77520bc2015-01-12 18:45:46 +00005047 Primitive::Type type = instruction->GetType();
5048 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005049 case Primitive::kPrimBoolean: {
5050 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005051 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005052 if (index.IsConstant()) {
5053 __ movzxb(out, Address(obj,
5054 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5055 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005056 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005057 }
5058 break;
5059 }
5060
5061 case Primitive::kPrimByte: {
5062 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005063 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064 if (index.IsConstant()) {
5065 __ movsxb(out, Address(obj,
5066 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5067 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005068 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069 }
5070 break;
5071 }
5072
5073 case Primitive::kPrimShort: {
5074 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005075 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005076 if (index.IsConstant()) {
5077 __ movsxw(out, Address(obj,
5078 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5079 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005080 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081 }
5082 break;
5083 }
5084
5085 case Primitive::kPrimChar: {
5086 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005087 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005088 if (index.IsConstant()) {
5089 __ movzxw(out, Address(obj,
5090 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5091 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005092 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005093 }
5094 break;
5095 }
5096
Roland Levillain7c1559a2015-12-15 10:55:36 +00005097 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005098 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005099 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005100 if (index.IsConstant()) {
5101 __ movl(out, Address(obj,
5102 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5103 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005104 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005105 }
5106 break;
5107 }
5108
Roland Levillain7c1559a2015-12-15 10:55:36 +00005109 case Primitive::kPrimNot: {
5110 static_assert(
5111 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5112 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5113 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5114 // /* HeapReference<Object> */ out =
5115 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5116 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5117 Location temp = locations->GetTemp(0);
5118 // Note that a potential implicit null check is handled in this
5119 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5120 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5121 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5122 } else {
5123 Register out = out_loc.AsRegister<Register>();
5124 if (index.IsConstant()) {
5125 uint32_t offset =
5126 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5127 __ movl(out, Address(obj, offset));
5128 codegen_->MaybeRecordImplicitNullCheck(instruction);
5129 // If read barriers are enabled, emit read barriers other than
5130 // Baker's using a slow path (and also unpoison the loaded
5131 // reference, if heap poisoning is enabled).
5132 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5133 } else {
5134 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5135 codegen_->MaybeRecordImplicitNullCheck(instruction);
5136 // If read barriers are enabled, emit read barriers other than
5137 // Baker's using a slow path (and also unpoison the loaded
5138 // reference, if heap poisoning is enabled).
5139 codegen_->MaybeGenerateReadBarrierSlow(
5140 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5141 }
5142 }
5143 break;
5144 }
5145
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005146 case Primitive::kPrimLong: {
5147 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005148 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005149 if (index.IsConstant()) {
5150 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005151 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005152 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005153 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005154 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005155 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005156 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005157 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005158 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005159 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160 }
5161 break;
5162 }
5163
Mark Mendell7c8d0092015-01-26 11:21:33 -05005164 case Primitive::kPrimFloat: {
5165 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005166 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005167 if (index.IsConstant()) {
5168 __ movss(out, Address(obj,
5169 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5170 } else {
5171 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5172 }
5173 break;
5174 }
5175
5176 case Primitive::kPrimDouble: {
5177 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005178 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005179 if (index.IsConstant()) {
5180 __ movsd(out, Address(obj,
5181 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5182 } else {
5183 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5184 }
5185 break;
5186 }
5187
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005188 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005189 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005190 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005191 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005192
Roland Levillain7c1559a2015-12-15 10:55:36 +00005193 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5194 // Potential implicit null checks, in the case of reference or
5195 // long arrays, are handled in the previous switch statement.
5196 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005197 codegen_->MaybeRecordImplicitNullCheck(instruction);
5198 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005199}
5200
5201void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005202 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005203
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005204 bool needs_write_barrier =
5205 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005206 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5207 bool object_array_set_with_read_barrier =
5208 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005209
Nicolas Geoffray39468442014-09-02 15:17:15 +01005210 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5211 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005212 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5213 LocationSummary::kCallOnSlowPath :
5214 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005215
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005216 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5217 || (value_type == Primitive::kPrimByte);
5218 // We need the inputs to be different than the output in case of long operation.
5219 // In case of a byte operation, the register allocator does not support multiple
5220 // inputs that die at entry with one in a specific register.
5221 locations->SetInAt(0, Location::RequiresRegister());
5222 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5223 if (is_byte_type) {
5224 // Ensure the value is in a byte register.
5225 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5226 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005227 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005228 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005229 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5230 }
5231 if (needs_write_barrier) {
5232 // Temporary registers for the write barrier.
5233 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5234 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005235 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005236 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237}
5238
5239void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5240 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005241 Location array_loc = locations->InAt(0);
5242 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005243 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005244 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005245 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005246 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5247 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5248 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005249 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005250 bool needs_write_barrier =
5251 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005252
5253 switch (value_type) {
5254 case Primitive::kPrimBoolean:
5255 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005256 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5257 Address address = index.IsConstant()
5258 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5259 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5260 if (value.IsRegister()) {
5261 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005262 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005263 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005264 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005265 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005266 break;
5267 }
5268
5269 case Primitive::kPrimShort:
5270 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005271 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5272 Address address = index.IsConstant()
5273 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5274 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5275 if (value.IsRegister()) {
5276 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005277 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005278 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005279 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005280 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005281 break;
5282 }
5283
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005284 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005285 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5286 Address address = index.IsConstant()
5287 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5288 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005289
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005290 if (!value.IsRegister()) {
5291 // Just setting null.
5292 DCHECK(instruction->InputAt(2)->IsNullConstant());
5293 DCHECK(value.IsConstant()) << value;
5294 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005295 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005296 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005297 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005298 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005300
5301 DCHECK(needs_write_barrier);
5302 Register register_value = value.AsRegister<Register>();
5303 NearLabel done, not_null, do_put;
5304 SlowPathCode* slow_path = nullptr;
5305 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005306 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005307 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5308 codegen_->AddSlowPath(slow_path);
5309 if (instruction->GetValueCanBeNull()) {
5310 __ testl(register_value, register_value);
5311 __ j(kNotEqual, &not_null);
5312 __ movl(address, Immediate(0));
5313 codegen_->MaybeRecordImplicitNullCheck(instruction);
5314 __ jmp(&done);
5315 __ Bind(&not_null);
5316 }
5317
Roland Levillain0d5a2812015-11-13 10:07:31 +00005318 if (kEmitCompilerReadBarrier) {
5319 // When read barriers are enabled, the type checking
5320 // instrumentation requires two read barriers:
5321 //
5322 // __ movl(temp2, temp);
5323 // // /* HeapReference<Class> */ temp = temp->component_type_
5324 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005325 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005326 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5327 //
5328 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5329 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005330 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005331 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5332 //
5333 // __ cmpl(temp, temp2);
5334 //
5335 // However, the second read barrier may trash `temp`, as it
5336 // is a temporary register, and as such would not be saved
5337 // along with live registers before calling the runtime (nor
5338 // restored afterwards). So in this case, we bail out and
5339 // delegate the work to the array set slow path.
5340 //
5341 // TODO: Extend the register allocator to support a new
5342 // "(locally) live temp" location so as to avoid always
5343 // going into the slow path when read barriers are enabled.
5344 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005345 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005346 // /* HeapReference<Class> */ temp = array->klass_
5347 __ movl(temp, Address(array, class_offset));
5348 codegen_->MaybeRecordImplicitNullCheck(instruction);
5349 __ MaybeUnpoisonHeapReference(temp);
5350
5351 // /* HeapReference<Class> */ temp = temp->component_type_
5352 __ movl(temp, Address(temp, component_offset));
5353 // If heap poisoning is enabled, no need to unpoison `temp`
5354 // nor the object reference in `register_value->klass`, as
5355 // we are comparing two poisoned references.
5356 __ cmpl(temp, Address(register_value, class_offset));
5357
5358 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5359 __ j(kEqual, &do_put);
5360 // If heap poisoning is enabled, the `temp` reference has
5361 // not been unpoisoned yet; unpoison it now.
5362 __ MaybeUnpoisonHeapReference(temp);
5363
5364 // /* HeapReference<Class> */ temp = temp->super_class_
5365 __ movl(temp, Address(temp, super_offset));
5366 // If heap poisoning is enabled, no need to unpoison
5367 // `temp`, as we are comparing against null below.
5368 __ testl(temp, temp);
5369 __ j(kNotEqual, slow_path->GetEntryLabel());
5370 __ Bind(&do_put);
5371 } else {
5372 __ j(kNotEqual, slow_path->GetEntryLabel());
5373 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005374 }
5375 }
5376
5377 if (kPoisonHeapReferences) {
5378 __ movl(temp, register_value);
5379 __ PoisonHeapReference(temp);
5380 __ movl(address, temp);
5381 } else {
5382 __ movl(address, register_value);
5383 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005384 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005385 codegen_->MaybeRecordImplicitNullCheck(instruction);
5386 }
5387
5388 Register card = locations->GetTemp(1).AsRegister<Register>();
5389 codegen_->MarkGCCard(
5390 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5391 __ Bind(&done);
5392
5393 if (slow_path != nullptr) {
5394 __ Bind(slow_path->GetExitLabel());
5395 }
5396
5397 break;
5398 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005399
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005400 case Primitive::kPrimInt: {
5401 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5402 Address address = index.IsConstant()
5403 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5404 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5405 if (value.IsRegister()) {
5406 __ movl(address, value.AsRegister<Register>());
5407 } else {
5408 DCHECK(value.IsConstant()) << value;
5409 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5410 __ movl(address, Immediate(v));
5411 }
5412 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005413 break;
5414 }
5415
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005416 case Primitive::kPrimLong: {
5417 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005418 if (index.IsConstant()) {
5419 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005420 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005421 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005422 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005423 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005424 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005425 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005426 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005427 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005428 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005429 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005430 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005431 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005432 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005433 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005434 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005435 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005436 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005437 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005438 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005439 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005440 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005441 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005442 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005443 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005444 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005445 Immediate(High32Bits(val)));
5446 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005447 }
5448 break;
5449 }
5450
Mark Mendell7c8d0092015-01-26 11:21:33 -05005451 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005452 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5453 Address address = index.IsConstant()
5454 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5455 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005456 if (value.IsFpuRegister()) {
5457 __ movss(address, value.AsFpuRegister<XmmRegister>());
5458 } else {
5459 DCHECK(value.IsConstant());
5460 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5461 __ movl(address, Immediate(v));
5462 }
5463 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005464 break;
5465 }
5466
5467 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005468 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5469 Address address = index.IsConstant()
5470 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5471 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005472 if (value.IsFpuRegister()) {
5473 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5474 } else {
5475 DCHECK(value.IsConstant());
5476 Address address_hi = index.IsConstant() ?
5477 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5478 offset + kX86WordSize) :
5479 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5480 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5481 __ movl(address, Immediate(Low32Bits(v)));
5482 codegen_->MaybeRecordImplicitNullCheck(instruction);
5483 __ movl(address_hi, Immediate(High32Bits(v)));
5484 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005485 break;
5486 }
5487
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005488 case Primitive::kPrimVoid:
5489 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005490 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005491 }
5492}
5493
5494void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5495 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005496 locations->SetInAt(0, Location::RequiresRegister());
5497 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005498}
5499
5500void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5501 LocationSummary* locations = instruction->GetLocations();
5502 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005503 Register obj = locations->InAt(0).AsRegister<Register>();
5504 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005505 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005506 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005507}
5508
5509void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005510 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5511 ? LocationSummary::kCallOnSlowPath
5512 : LocationSummary::kNoCall;
5513 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005514 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005515 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005516 if (instruction->HasUses()) {
5517 locations->SetOut(Location::SameAsFirstInput());
5518 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005519}
5520
5521void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5522 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005523 Location index_loc = locations->InAt(0);
5524 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005525 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005526 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005527
Mark Mendell99dbd682015-04-22 16:18:52 -04005528 if (length_loc.IsConstant()) {
5529 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5530 if (index_loc.IsConstant()) {
5531 // BCE will remove the bounds check if we are guarenteed to pass.
5532 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5533 if (index < 0 || index >= length) {
5534 codegen_->AddSlowPath(slow_path);
5535 __ jmp(slow_path->GetEntryLabel());
5536 } else {
5537 // Some optimization after BCE may have generated this, and we should not
5538 // generate a bounds check if it is a valid range.
5539 }
5540 return;
5541 }
5542
5543 // We have to reverse the jump condition because the length is the constant.
5544 Register index_reg = index_loc.AsRegister<Register>();
5545 __ cmpl(index_reg, Immediate(length));
5546 codegen_->AddSlowPath(slow_path);
5547 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005548 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005549 Register length = length_loc.AsRegister<Register>();
5550 if (index_loc.IsConstant()) {
5551 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5552 __ cmpl(length, Immediate(value));
5553 } else {
5554 __ cmpl(length, index_loc.AsRegister<Register>());
5555 }
5556 codegen_->AddSlowPath(slow_path);
5557 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005558 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005559}
5560
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005561void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005562 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005563}
5564
5565void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005566 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5567}
5568
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005569void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5570 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5571}
5572
5573void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005574 HBasicBlock* block = instruction->GetBlock();
5575 if (block->GetLoopInformation() != nullptr) {
5576 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5577 // The back edge will generate the suspend check.
5578 return;
5579 }
5580 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5581 // The goto will generate the suspend check.
5582 return;
5583 }
5584 GenerateSuspendCheck(instruction, nullptr);
5585}
5586
5587void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5588 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005589 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005590 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5591 if (slow_path == nullptr) {
5592 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5593 instruction->SetSlowPath(slow_path);
5594 codegen_->AddSlowPath(slow_path);
5595 if (successor != nullptr) {
5596 DCHECK(successor->IsLoopHeader());
5597 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5598 }
5599 } else {
5600 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5601 }
5602
Roland Levillain7c1559a2015-12-15 10:55:36 +00005603 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5604 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005605 if (successor == nullptr) {
5606 __ j(kNotEqual, slow_path->GetEntryLabel());
5607 __ Bind(slow_path->GetReturnLabel());
5608 } else {
5609 __ j(kEqual, codegen_->GetLabelOf(successor));
5610 __ jmp(slow_path->GetEntryLabel());
5611 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005612}
5613
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005614X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5615 return codegen_->GetAssembler();
5616}
5617
Mark Mendell7c8d0092015-01-26 11:21:33 -05005618void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005619 ScratchRegisterScope ensure_scratch(
5620 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5621 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5622 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5623 __ movl(temp_reg, Address(ESP, src + stack_offset));
5624 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005625}
5626
5627void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005628 ScratchRegisterScope ensure_scratch(
5629 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5630 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5631 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5632 __ movl(temp_reg, Address(ESP, src + stack_offset));
5633 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5634 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5635 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005636}
5637
5638void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005639 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005640 Location source = move->GetSource();
5641 Location destination = move->GetDestination();
5642
5643 if (source.IsRegister()) {
5644 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005645 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005646 } else if (destination.IsFpuRegister()) {
5647 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005648 } else {
5649 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005650 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005651 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005652 } else if (source.IsRegisterPair()) {
5653 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5654 // Create stack space for 2 elements.
5655 __ subl(ESP, Immediate(2 * elem_size));
5656 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5657 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5658 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5659 // And remove the temporary stack space we allocated.
5660 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005661 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005662 if (destination.IsRegister()) {
5663 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5664 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005665 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005666 } else if (destination.IsRegisterPair()) {
5667 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5668 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5669 __ psrlq(src_reg, Immediate(32));
5670 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005671 } else if (destination.IsStackSlot()) {
5672 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5673 } else {
5674 DCHECK(destination.IsDoubleStackSlot());
5675 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5676 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005677 } else if (source.IsStackSlot()) {
5678 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005679 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005680 } else if (destination.IsFpuRegister()) {
5681 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005682 } else {
5683 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005684 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5685 }
5686 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005687 if (destination.IsRegisterPair()) {
5688 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5689 __ movl(destination.AsRegisterPairHigh<Register>(),
5690 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5691 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005692 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5693 } else {
5694 DCHECK(destination.IsDoubleStackSlot()) << destination;
5695 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005696 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005697 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005698 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005699 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005700 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005701 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005702 if (value == 0) {
5703 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5704 } else {
5705 __ movl(destination.AsRegister<Register>(), Immediate(value));
5706 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005707 } else {
5708 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005709 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005710 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005711 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005712 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005713 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005714 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005715 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005716 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5717 if (value == 0) {
5718 // Easy handling of 0.0.
5719 __ xorps(dest, dest);
5720 } else {
5721 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005722 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5723 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5724 __ movl(temp, Immediate(value));
5725 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005726 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005727 } else {
5728 DCHECK(destination.IsStackSlot()) << destination;
5729 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5730 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005731 } else if (constant->IsLongConstant()) {
5732 int64_t value = constant->AsLongConstant()->GetValue();
5733 int32_t low_value = Low32Bits(value);
5734 int32_t high_value = High32Bits(value);
5735 Immediate low(low_value);
5736 Immediate high(high_value);
5737 if (destination.IsDoubleStackSlot()) {
5738 __ movl(Address(ESP, destination.GetStackIndex()), low);
5739 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5740 } else {
5741 __ movl(destination.AsRegisterPairLow<Register>(), low);
5742 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5743 }
5744 } else {
5745 DCHECK(constant->IsDoubleConstant());
5746 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005747 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005748 int32_t low_value = Low32Bits(value);
5749 int32_t high_value = High32Bits(value);
5750 Immediate low(low_value);
5751 Immediate high(high_value);
5752 if (destination.IsFpuRegister()) {
5753 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5754 if (value == 0) {
5755 // Easy handling of 0.0.
5756 __ xorpd(dest, dest);
5757 } else {
5758 __ pushl(high);
5759 __ pushl(low);
5760 __ movsd(dest, Address(ESP, 0));
5761 __ addl(ESP, Immediate(8));
5762 }
5763 } else {
5764 DCHECK(destination.IsDoubleStackSlot()) << destination;
5765 __ movl(Address(ESP, destination.GetStackIndex()), low);
5766 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5767 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005768 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005769 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005770 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005771 }
5772}
5773
Mark Mendella5c19ce2015-04-01 12:51:05 -04005774void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005775 Register suggested_scratch = reg == EAX ? EBX : EAX;
5776 ScratchRegisterScope ensure_scratch(
5777 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5778
5779 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5780 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5781 __ movl(Address(ESP, mem + stack_offset), reg);
5782 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005783}
5784
Mark Mendell7c8d0092015-01-26 11:21:33 -05005785void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005786 ScratchRegisterScope ensure_scratch(
5787 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5788
5789 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5790 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5791 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5792 __ movss(Address(ESP, mem + stack_offset), reg);
5793 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005794}
5795
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005796void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005797 ScratchRegisterScope ensure_scratch1(
5798 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005799
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005800 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5801 ScratchRegisterScope ensure_scratch2(
5802 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005803
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005804 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5805 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5806 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5807 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5808 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5809 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005810}
5811
5812void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005813 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005814 Location source = move->GetSource();
5815 Location destination = move->GetDestination();
5816
5817 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005818 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5819 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5820 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5821 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5822 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005823 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005824 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005825 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005826 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005827 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5828 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005829 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5830 // Use XOR Swap algorithm to avoid a temporary.
5831 DCHECK_NE(source.reg(), destination.reg());
5832 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5833 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5834 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5835 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5836 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5837 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5838 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005839 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5840 // Take advantage of the 16 bytes in the XMM register.
5841 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5842 Address stack(ESP, destination.GetStackIndex());
5843 // Load the double into the high doubleword.
5844 __ movhpd(reg, stack);
5845
5846 // Store the low double into the destination.
5847 __ movsd(stack, reg);
5848
5849 // Move the high double to the low double.
5850 __ psrldq(reg, Immediate(8));
5851 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5852 // Take advantage of the 16 bytes in the XMM register.
5853 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5854 Address stack(ESP, source.GetStackIndex());
5855 // Load the double into the high doubleword.
5856 __ movhpd(reg, stack);
5857
5858 // Store the low double into the destination.
5859 __ movsd(stack, reg);
5860
5861 // Move the high double to the low double.
5862 __ psrldq(reg, Immediate(8));
5863 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5864 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5865 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005866 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005867 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005868 }
5869}
5870
5871void ParallelMoveResolverX86::SpillScratch(int reg) {
5872 __ pushl(static_cast<Register>(reg));
5873}
5874
5875void ParallelMoveResolverX86::RestoreScratch(int reg) {
5876 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005877}
5878
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005879void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005880 InvokeRuntimeCallingConvention calling_convention;
5881 CodeGenerator::CreateLoadClassLocationSummary(
5882 cls,
5883 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005884 Location::RegisterLocation(EAX),
5885 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005886}
5887
5888void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005889 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005890 if (cls->NeedsAccessCheck()) {
5891 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5892 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5893 cls,
5894 cls->GetDexPc(),
5895 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005896 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005897 return;
5898 }
5899
Roland Levillain0d5a2812015-11-13 10:07:31 +00005900 Location out_loc = locations->Out();
5901 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005902 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005903
Calin Juravle580b6092015-10-06 17:35:58 +01005904 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005905 DCHECK(!cls->CanCallRuntime());
5906 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005907 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5908 GenerateGcRootFieldLoad(
5909 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005910 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005911 // /* GcRoot<mirror::Class>[] */ out =
5912 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5913 __ movl(out, Address(current_method,
5914 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005915 // /* GcRoot<mirror::Class> */ out = out[type_index]
5916 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005917
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005918 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5919 DCHECK(cls->CanCallRuntime());
5920 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5921 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5922 codegen_->AddSlowPath(slow_path);
5923
5924 if (!cls->IsInDexCache()) {
5925 __ testl(out, out);
5926 __ j(kEqual, slow_path->GetEntryLabel());
5927 }
5928
5929 if (cls->MustGenerateClinitCheck()) {
5930 GenerateClassInitializationCheck(slow_path, out);
5931 } else {
5932 __ Bind(slow_path->GetExitLabel());
5933 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005934 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005935 }
5936}
5937
5938void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5939 LocationSummary* locations =
5940 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5941 locations->SetInAt(0, Location::RequiresRegister());
5942 if (check->HasUses()) {
5943 locations->SetOut(Location::SameAsFirstInput());
5944 }
5945}
5946
5947void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005948 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005949 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005950 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005951 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005952 GenerateClassInitializationCheck(slow_path,
5953 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005954}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005955
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005956void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005957 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005958 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5959 Immediate(mirror::Class::kStatusInitialized));
5960 __ j(kLess, slow_path->GetEntryLabel());
5961 __ Bind(slow_path->GetExitLabel());
5962 // No need for memory fence, thanks to the X86 memory model.
5963}
5964
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005965void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005966 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5967 ? LocationSummary::kCallOnSlowPath
5968 : LocationSummary::kNoCall;
5969 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005970 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005971 locations->SetOut(Location::RequiresRegister());
5972}
5973
5974void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005975 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005976 Location out_loc = locations->Out();
5977 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005978 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005979
Roland Levillain7c1559a2015-12-15 10:55:36 +00005980 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5981 GenerateGcRootFieldLoad(
5982 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005983 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005984 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005985 // /* GcRoot<mirror::String> */ out = out[string_index]
5986 GenerateGcRootFieldLoad(
5987 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005988
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005989 if (!load->IsInDexCache()) {
5990 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5991 codegen_->AddSlowPath(slow_path);
5992 __ testl(out, out);
5993 __ j(kEqual, slow_path->GetEntryLabel());
5994 __ Bind(slow_path->GetExitLabel());
5995 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005996}
5997
David Brazdilcb1c0552015-08-04 16:22:25 +01005998static Address GetExceptionTlsAddress() {
5999 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6000}
6001
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006002void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6003 LocationSummary* locations =
6004 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6005 locations->SetOut(Location::RequiresRegister());
6006}
6007
6008void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006009 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6010}
6011
6012void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6013 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6014}
6015
6016void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6017 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006018}
6019
6020void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6021 LocationSummary* locations =
6022 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6023 InvokeRuntimeCallingConvention calling_convention;
6024 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6025}
6026
6027void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006028 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6029 instruction,
6030 instruction->GetDexPc(),
6031 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006032 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006033}
6034
Roland Levillain7c1559a2015-12-15 10:55:36 +00006035static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6036 return kEmitCompilerReadBarrier &&
6037 (kUseBakerReadBarrier ||
6038 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6039 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6040 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6041}
6042
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006043void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006044 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006045 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6046 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006047 case TypeCheckKind::kExactCheck:
6048 case TypeCheckKind::kAbstractClassCheck:
6049 case TypeCheckKind::kClassHierarchyCheck:
6050 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006051 call_kind =
6052 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006053 break;
6054 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006055 case TypeCheckKind::kUnresolvedCheck:
6056 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006057 call_kind = LocationSummary::kCallOnSlowPath;
6058 break;
6059 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006060
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006061 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006062 locations->SetInAt(0, Location::RequiresRegister());
6063 locations->SetInAt(1, Location::Any());
6064 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6065 locations->SetOut(Location::RequiresRegister());
6066 // When read barriers are enabled, we need a temporary register for
6067 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006068 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006069 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006070 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006071}
6072
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006073void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006074 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006075 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006076 Location obj_loc = locations->InAt(0);
6077 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006078 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006079 Location out_loc = locations->Out();
6080 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006081 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006082 locations->GetTemp(0) :
6083 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006084 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006085 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6086 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6087 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006088 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006089 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006090
6091 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006092 // Avoid null check if we know obj is not null.
6093 if (instruction->MustDoNullCheck()) {
6094 __ testl(obj, obj);
6095 __ j(kEqual, &zero);
6096 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006097
Roland Levillain0d5a2812015-11-13 10:07:31 +00006098 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006099 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006100
Roland Levillain7c1559a2015-12-15 10:55:36 +00006101 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006102 case TypeCheckKind::kExactCheck: {
6103 if (cls.IsRegister()) {
6104 __ cmpl(out, cls.AsRegister<Register>());
6105 } else {
6106 DCHECK(cls.IsStackSlot()) << cls;
6107 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6108 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006109
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006110 // Classes must be equal for the instanceof to succeed.
6111 __ j(kNotEqual, &zero);
6112 __ movl(out, Immediate(1));
6113 __ jmp(&done);
6114 break;
6115 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006116
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006117 case TypeCheckKind::kAbstractClassCheck: {
6118 // If the class is abstract, we eagerly fetch the super class of the
6119 // object to avoid doing a comparison we know will fail.
6120 NearLabel loop;
6121 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006122 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006123 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006124 __ testl(out, out);
6125 // If `out` is null, we use it for the result, and jump to `done`.
6126 __ j(kEqual, &done);
6127 if (cls.IsRegister()) {
6128 __ cmpl(out, cls.AsRegister<Register>());
6129 } else {
6130 DCHECK(cls.IsStackSlot()) << cls;
6131 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6132 }
6133 __ j(kNotEqual, &loop);
6134 __ movl(out, Immediate(1));
6135 if (zero.IsLinked()) {
6136 __ jmp(&done);
6137 }
6138 break;
6139 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006140
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006141 case TypeCheckKind::kClassHierarchyCheck: {
6142 // Walk over the class hierarchy to find a match.
6143 NearLabel loop, success;
6144 __ Bind(&loop);
6145 if (cls.IsRegister()) {
6146 __ cmpl(out, cls.AsRegister<Register>());
6147 } else {
6148 DCHECK(cls.IsStackSlot()) << cls;
6149 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6150 }
6151 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006152 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006153 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006154 __ testl(out, out);
6155 __ j(kNotEqual, &loop);
6156 // If `out` is null, we use it for the result, and jump to `done`.
6157 __ jmp(&done);
6158 __ Bind(&success);
6159 __ movl(out, Immediate(1));
6160 if (zero.IsLinked()) {
6161 __ jmp(&done);
6162 }
6163 break;
6164 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006165
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006166 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006167 // Do an exact check.
6168 NearLabel exact_check;
6169 if (cls.IsRegister()) {
6170 __ cmpl(out, cls.AsRegister<Register>());
6171 } else {
6172 DCHECK(cls.IsStackSlot()) << cls;
6173 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6174 }
6175 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006176 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006177 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006178 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006179 __ testl(out, out);
6180 // If `out` is null, we use it for the result, and jump to `done`.
6181 __ j(kEqual, &done);
6182 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6183 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006184 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006185 __ movl(out, Immediate(1));
6186 __ jmp(&done);
6187 break;
6188 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006189
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006190 case TypeCheckKind::kArrayCheck: {
6191 if (cls.IsRegister()) {
6192 __ cmpl(out, cls.AsRegister<Register>());
6193 } else {
6194 DCHECK(cls.IsStackSlot()) << cls;
6195 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6196 }
6197 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006198 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6199 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006200 codegen_->AddSlowPath(slow_path);
6201 __ j(kNotEqual, slow_path->GetEntryLabel());
6202 __ movl(out, Immediate(1));
6203 if (zero.IsLinked()) {
6204 __ jmp(&done);
6205 }
6206 break;
6207 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006208
Calin Juravle98893e12015-10-02 21:05:03 +01006209 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006210 case TypeCheckKind::kInterfaceCheck: {
6211 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006212 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006213 // cases.
6214 //
6215 // We cannot directly call the InstanceofNonTrivial runtime
6216 // entry point without resorting to a type checking slow path
6217 // here (i.e. by calling InvokeRuntime directly), as it would
6218 // require to assign fixed registers for the inputs of this
6219 // HInstanceOf instruction (following the runtime calling
6220 // convention), which might be cluttered by the potential first
6221 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006222 //
6223 // TODO: Introduce a new runtime entry point taking the object
6224 // to test (instead of its class) as argument, and let it deal
6225 // with the read barrier issues. This will let us refactor this
6226 // case of the `switch` code as it was previously (with a direct
6227 // call to the runtime not using a type checking slow path).
6228 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006229 DCHECK(locations->OnlyCallsOnSlowPath());
6230 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6231 /* is_fatal */ false);
6232 codegen_->AddSlowPath(slow_path);
6233 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006234 if (zero.IsLinked()) {
6235 __ jmp(&done);
6236 }
6237 break;
6238 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006239 }
6240
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006241 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006242 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006243 __ xorl(out, out);
6244 }
6245
6246 if (done.IsLinked()) {
6247 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006248 }
6249
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006250 if (slow_path != nullptr) {
6251 __ Bind(slow_path->GetExitLabel());
6252 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006253}
6254
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006255void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006256 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6257 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006258 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6259 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006260 case TypeCheckKind::kExactCheck:
6261 case TypeCheckKind::kAbstractClassCheck:
6262 case TypeCheckKind::kClassHierarchyCheck:
6263 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006264 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6265 LocationSummary::kCallOnSlowPath :
6266 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006267 break;
6268 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006269 case TypeCheckKind::kUnresolvedCheck:
6270 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006271 call_kind = LocationSummary::kCallOnSlowPath;
6272 break;
6273 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006274 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6275 locations->SetInAt(0, Location::RequiresRegister());
6276 locations->SetInAt(1, Location::Any());
6277 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6278 locations->AddTemp(Location::RequiresRegister());
6279 // When read barriers are enabled, we need an additional temporary
6280 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006281 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006282 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006283 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006284}
6285
6286void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006287 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006288 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006289 Location obj_loc = locations->InAt(0);
6290 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006291 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006292 Location temp_loc = locations->GetTemp(0);
6293 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006294 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006295 locations->GetTemp(1) :
6296 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006297 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6298 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6299 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6300 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006301
Roland Levillain0d5a2812015-11-13 10:07:31 +00006302 bool is_type_check_slow_path_fatal =
6303 (type_check_kind == TypeCheckKind::kExactCheck ||
6304 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6305 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6306 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6307 !instruction->CanThrowIntoCatchBlock();
6308 SlowPathCode* type_check_slow_path =
6309 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6310 is_type_check_slow_path_fatal);
6311 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312
Roland Levillain0d5a2812015-11-13 10:07:31 +00006313 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006314 // Avoid null check if we know obj is not null.
6315 if (instruction->MustDoNullCheck()) {
6316 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006317 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006318 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006319
Roland Levillain0d5a2812015-11-13 10:07:31 +00006320 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006321 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006322
Roland Levillain0d5a2812015-11-13 10:07:31 +00006323 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006324 case TypeCheckKind::kExactCheck:
6325 case TypeCheckKind::kArrayCheck: {
6326 if (cls.IsRegister()) {
6327 __ cmpl(temp, cls.AsRegister<Register>());
6328 } else {
6329 DCHECK(cls.IsStackSlot()) << cls;
6330 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6331 }
6332 // Jump to slow path for throwing the exception or doing a
6333 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006334 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006335 break;
6336 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006337
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006338 case TypeCheckKind::kAbstractClassCheck: {
6339 // If the class is abstract, we eagerly fetch the super class of the
6340 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006341 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006342 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006343 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006344 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006345
6346 // If the class reference currently in `temp` is not null, jump
6347 // to the `compare_classes` label to compare it with the checked
6348 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006349 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006350 __ j(kNotEqual, &compare_classes);
6351 // Otherwise, jump to the slow path to throw the exception.
6352 //
6353 // But before, move back the object's class into `temp` before
6354 // going into the slow path, as it has been overwritten in the
6355 // meantime.
6356 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006357 GenerateReferenceLoadTwoRegisters(
6358 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006359 __ jmp(type_check_slow_path->GetEntryLabel());
6360
6361 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006362 if (cls.IsRegister()) {
6363 __ cmpl(temp, cls.AsRegister<Register>());
6364 } else {
6365 DCHECK(cls.IsStackSlot()) << cls;
6366 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6367 }
6368 __ j(kNotEqual, &loop);
6369 break;
6370 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006371
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006372 case TypeCheckKind::kClassHierarchyCheck: {
6373 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006374 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006375 __ Bind(&loop);
6376 if (cls.IsRegister()) {
6377 __ cmpl(temp, cls.AsRegister<Register>());
6378 } else {
6379 DCHECK(cls.IsStackSlot()) << cls;
6380 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6381 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006382 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006383
Roland Levillain0d5a2812015-11-13 10:07:31 +00006384 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006385 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006386
6387 // If the class reference currently in `temp` is not null, jump
6388 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006389 __ testl(temp, temp);
6390 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006391 // Otherwise, jump to the slow path to throw the exception.
6392 //
6393 // But before, move back the object's class into `temp` before
6394 // going into the slow path, as it has been overwritten in the
6395 // meantime.
6396 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006397 GenerateReferenceLoadTwoRegisters(
6398 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006399 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006400 break;
6401 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006402
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006403 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006404 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006405 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006406 if (cls.IsRegister()) {
6407 __ cmpl(temp, cls.AsRegister<Register>());
6408 } else {
6409 DCHECK(cls.IsStackSlot()) << cls;
6410 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6411 }
6412 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006413
6414 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006415 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006416 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006417
6418 // If the component type is not null (i.e. the object is indeed
6419 // an array), jump to label `check_non_primitive_component_type`
6420 // to further check that this component type is not a primitive
6421 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006422 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006423 __ j(kNotEqual, &check_non_primitive_component_type);
6424 // Otherwise, jump to the slow path to throw the exception.
6425 //
6426 // But before, move back the object's class into `temp` before
6427 // going into the slow path, as it has been overwritten in the
6428 // meantime.
6429 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006430 GenerateReferenceLoadTwoRegisters(
6431 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006432 __ jmp(type_check_slow_path->GetEntryLabel());
6433
6434 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006435 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006436 __ j(kEqual, &done);
6437 // Same comment as above regarding `temp` and the slow path.
6438 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006439 GenerateReferenceLoadTwoRegisters(
6440 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006441 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006442 break;
6443 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006444
Calin Juravle98893e12015-10-02 21:05:03 +01006445 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006446 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006447 // We always go into the type check slow path for the unresolved
6448 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449 //
6450 // We cannot directly call the CheckCast runtime entry point
6451 // without resorting to a type checking slow path here (i.e. by
6452 // calling InvokeRuntime directly), as it would require to
6453 // assign fixed registers for the inputs of this HInstanceOf
6454 // instruction (following the runtime calling convention), which
6455 // might be cluttered by the potential first read barrier
6456 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006457 //
6458 // TODO: Introduce a new runtime entry point taking the object
6459 // to test (instead of its class) as argument, and let it deal
6460 // with the read barrier issues. This will let us refactor this
6461 // case of the `switch` code as it was previously (with a direct
6462 // call to the runtime not using a type checking slow path).
6463 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006464 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006465 break;
6466 }
6467 __ Bind(&done);
6468
Roland Levillain0d5a2812015-11-13 10:07:31 +00006469 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006470}
6471
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006472void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6473 LocationSummary* locations =
6474 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6475 InvokeRuntimeCallingConvention calling_convention;
6476 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6477}
6478
6479void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006480 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6481 : QUICK_ENTRY_POINT(pUnlockObject),
6482 instruction,
6483 instruction->GetDexPc(),
6484 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006485 if (instruction->IsEnter()) {
6486 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6487 } else {
6488 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6489 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006490}
6491
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006492void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6493void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6494void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6495
6496void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6497 LocationSummary* locations =
6498 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6499 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6500 || instruction->GetResultType() == Primitive::kPrimLong);
6501 locations->SetInAt(0, Location::RequiresRegister());
6502 locations->SetInAt(1, Location::Any());
6503 locations->SetOut(Location::SameAsFirstInput());
6504}
6505
6506void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6507 HandleBitwiseOperation(instruction);
6508}
6509
6510void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6511 HandleBitwiseOperation(instruction);
6512}
6513
6514void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6515 HandleBitwiseOperation(instruction);
6516}
6517
6518void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6519 LocationSummary* locations = instruction->GetLocations();
6520 Location first = locations->InAt(0);
6521 Location second = locations->InAt(1);
6522 DCHECK(first.Equals(locations->Out()));
6523
6524 if (instruction->GetResultType() == Primitive::kPrimInt) {
6525 if (second.IsRegister()) {
6526 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006527 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006528 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006529 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006530 } else {
6531 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006532 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006533 }
6534 } else if (second.IsConstant()) {
6535 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006536 __ andl(first.AsRegister<Register>(),
6537 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006538 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006539 __ orl(first.AsRegister<Register>(),
6540 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006541 } else {
6542 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006543 __ xorl(first.AsRegister<Register>(),
6544 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006545 }
6546 } else {
6547 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006548 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006549 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006550 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006551 } else {
6552 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006553 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006554 }
6555 }
6556 } else {
6557 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6558 if (second.IsRegisterPair()) {
6559 if (instruction->IsAnd()) {
6560 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6561 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6562 } else if (instruction->IsOr()) {
6563 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6564 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6565 } else {
6566 DCHECK(instruction->IsXor());
6567 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6568 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6569 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006570 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006571 if (instruction->IsAnd()) {
6572 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6573 __ andl(first.AsRegisterPairHigh<Register>(),
6574 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6575 } else if (instruction->IsOr()) {
6576 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6577 __ orl(first.AsRegisterPairHigh<Register>(),
6578 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6579 } else {
6580 DCHECK(instruction->IsXor());
6581 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6582 __ xorl(first.AsRegisterPairHigh<Register>(),
6583 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6584 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006585 } else {
6586 DCHECK(second.IsConstant()) << second;
6587 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006588 int32_t low_value = Low32Bits(value);
6589 int32_t high_value = High32Bits(value);
6590 Immediate low(low_value);
6591 Immediate high(high_value);
6592 Register first_low = first.AsRegisterPairLow<Register>();
6593 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006594 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006595 if (low_value == 0) {
6596 __ xorl(first_low, first_low);
6597 } else if (low_value != -1) {
6598 __ andl(first_low, low);
6599 }
6600 if (high_value == 0) {
6601 __ xorl(first_high, first_high);
6602 } else if (high_value != -1) {
6603 __ andl(first_high, high);
6604 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006605 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006606 if (low_value != 0) {
6607 __ orl(first_low, low);
6608 }
6609 if (high_value != 0) {
6610 __ orl(first_high, high);
6611 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006612 } else {
6613 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006614 if (low_value != 0) {
6615 __ xorl(first_low, low);
6616 }
6617 if (high_value != 0) {
6618 __ xorl(first_high, high);
6619 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006620 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006621 }
6622 }
6623}
6624
Roland Levillain7c1559a2015-12-15 10:55:36 +00006625void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6626 Location out,
6627 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006628 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006629 Register out_reg = out.AsRegister<Register>();
6630 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006631 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006632 if (kUseBakerReadBarrier) {
6633 // Load with fast path based Baker's read barrier.
6634 // /* HeapReference<Object> */ out = *(out + offset)
6635 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006636 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006637 } else {
6638 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006639 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006640 // in the following move operation, as we will need it for the
6641 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006642 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006643 // /* HeapReference<Object> */ out = *(out + offset)
6644 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006645 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006646 }
6647 } else {
6648 // Plain load with no read barrier.
6649 // /* HeapReference<Object> */ out = *(out + offset)
6650 __ movl(out_reg, Address(out_reg, offset));
6651 __ MaybeUnpoisonHeapReference(out_reg);
6652 }
6653}
6654
6655void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6656 Location out,
6657 Location obj,
6658 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006659 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006660 Register out_reg = out.AsRegister<Register>();
6661 Register obj_reg = obj.AsRegister<Register>();
6662 if (kEmitCompilerReadBarrier) {
6663 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006664 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006665 // Load with fast path based Baker's read barrier.
6666 // /* HeapReference<Object> */ out = *(obj + offset)
6667 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006668 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006669 } else {
6670 // Load with slow path based read barrier.
6671 // /* HeapReference<Object> */ out = *(obj + offset)
6672 __ movl(out_reg, Address(obj_reg, offset));
6673 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6674 }
6675 } else {
6676 // Plain load with no read barrier.
6677 // /* HeapReference<Object> */ out = *(obj + offset)
6678 __ movl(out_reg, Address(obj_reg, offset));
6679 __ MaybeUnpoisonHeapReference(out_reg);
6680 }
6681}
6682
6683void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6684 Location root,
6685 Register obj,
6686 uint32_t offset) {
6687 Register root_reg = root.AsRegister<Register>();
6688 if (kEmitCompilerReadBarrier) {
6689 if (kUseBakerReadBarrier) {
6690 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6691 // Baker's read barrier are used:
6692 //
6693 // root = obj.field;
6694 // if (Thread::Current()->GetIsGcMarking()) {
6695 // root = ReadBarrier::Mark(root)
6696 // }
6697
6698 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6699 __ movl(root_reg, Address(obj, offset));
6700 static_assert(
6701 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6702 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6703 "have different sizes.");
6704 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6705 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6706 "have different sizes.");
6707
6708 // Slow path used to mark the GC root `root`.
6709 SlowPathCode* slow_path =
6710 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6711 codegen_->AddSlowPath(slow_path);
6712
6713 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6714 Immediate(0));
6715 __ j(kNotEqual, slow_path->GetEntryLabel());
6716 __ Bind(slow_path->GetExitLabel());
6717 } else {
6718 // GC root loaded through a slow path for read barriers other
6719 // than Baker's.
6720 // /* GcRoot<mirror::Object>* */ root = obj + offset
6721 __ leal(root_reg, Address(obj, offset));
6722 // /* mirror::Object* */ root = root->Read()
6723 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6724 }
6725 } else {
6726 // Plain GC root load with no read barrier.
6727 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6728 __ movl(root_reg, Address(obj, offset));
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006729 // Note that GC roots are not affected by heap poisoning, thus we
6730 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006731 }
6732}
6733
6734void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6735 Location ref,
6736 Register obj,
6737 uint32_t offset,
6738 Location temp,
6739 bool needs_null_check) {
6740 DCHECK(kEmitCompilerReadBarrier);
6741 DCHECK(kUseBakerReadBarrier);
6742
6743 // /* HeapReference<Object> */ ref = *(obj + offset)
6744 Address src(obj, offset);
6745 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6746}
6747
6748void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6749 Location ref,
6750 Register obj,
6751 uint32_t data_offset,
6752 Location index,
6753 Location temp,
6754 bool needs_null_check) {
6755 DCHECK(kEmitCompilerReadBarrier);
6756 DCHECK(kUseBakerReadBarrier);
6757
6758 // /* HeapReference<Object> */ ref =
6759 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6760 Address src = index.IsConstant() ?
6761 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6762 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6763 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6764}
6765
6766void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6767 Location ref,
6768 Register obj,
6769 const Address& src,
6770 Location temp,
6771 bool needs_null_check) {
6772 DCHECK(kEmitCompilerReadBarrier);
6773 DCHECK(kUseBakerReadBarrier);
6774
6775 // In slow path based read barriers, the read barrier call is
6776 // inserted after the original load. However, in fast path based
6777 // Baker's read barriers, we need to perform the load of
6778 // mirror::Object::monitor_ *before* the original reference load.
6779 // This load-load ordering is required by the read barrier.
6780 // The fast path/slow path (for Baker's algorithm) should look like:
6781 //
6782 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6783 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6784 // HeapReference<Object> ref = *src; // Original reference load.
6785 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6786 // if (is_gray) {
6787 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6788 // }
6789 //
6790 // Note: the original implementation in ReadBarrier::Barrier is
6791 // slightly more complex as:
6792 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006793 // the high-bits of rb_state, which are expected to be all zeroes
6794 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6795 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006796 // - it performs additional checks that we do not do here for
6797 // performance reasons.
6798
6799 Register ref_reg = ref.AsRegister<Register>();
6800 Register temp_reg = temp.AsRegister<Register>();
6801 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6802
6803 // /* int32_t */ monitor = obj->monitor_
6804 __ movl(temp_reg, Address(obj, monitor_offset));
6805 if (needs_null_check) {
6806 MaybeRecordImplicitNullCheck(instruction);
6807 }
6808 // /* LockWord */ lock_word = LockWord(monitor)
6809 static_assert(sizeof(LockWord) == sizeof(int32_t),
6810 "art::LockWord and int32_t have different sizes.");
6811 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6812 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6813 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6814 static_assert(
6815 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6816 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6817
6818 // Load fence to prevent load-load reordering.
6819 // Note that this is a no-op, thanks to the x86 memory model.
6820 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6821
6822 // The actual reference load.
6823 // /* HeapReference<Object> */ ref = *src
6824 __ movl(ref_reg, src);
6825
6826 // Object* ref = ref_addr->AsMirrorPtr()
6827 __ MaybeUnpoisonHeapReference(ref_reg);
6828
6829 // Slow path used to mark the object `ref` when it is gray.
6830 SlowPathCode* slow_path =
6831 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6832 AddSlowPath(slow_path);
6833
6834 // if (rb_state == ReadBarrier::gray_ptr_)
6835 // ref = ReadBarrier::Mark(ref);
6836 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6837 __ j(kEqual, slow_path->GetEntryLabel());
6838 __ Bind(slow_path->GetExitLabel());
6839}
6840
6841void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6842 Location out,
6843 Location ref,
6844 Location obj,
6845 uint32_t offset,
6846 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006847 DCHECK(kEmitCompilerReadBarrier);
6848
Roland Levillain7c1559a2015-12-15 10:55:36 +00006849 // Insert a slow path based read barrier *after* the reference load.
6850 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006851 // If heap poisoning is enabled, the unpoisoning of the loaded
6852 // reference will be carried out by the runtime within the slow
6853 // path.
6854 //
6855 // Note that `ref` currently does not get unpoisoned (when heap
6856 // poisoning is enabled), which is alright as the `ref` argument is
6857 // not used by the artReadBarrierSlow entry point.
6858 //
6859 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6860 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6861 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6862 AddSlowPath(slow_path);
6863
Roland Levillain0d5a2812015-11-13 10:07:31 +00006864 __ jmp(slow_path->GetEntryLabel());
6865 __ Bind(slow_path->GetExitLabel());
6866}
6867
Roland Levillain7c1559a2015-12-15 10:55:36 +00006868void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6869 Location out,
6870 Location ref,
6871 Location obj,
6872 uint32_t offset,
6873 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006874 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006875 // Baker's read barriers shall be handled by the fast path
6876 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6877 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006878 // If heap poisoning is enabled, unpoisoning will be taken care of
6879 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006880 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006881 } else if (kPoisonHeapReferences) {
6882 __ UnpoisonHeapReference(out.AsRegister<Register>());
6883 }
6884}
6885
Roland Levillain7c1559a2015-12-15 10:55:36 +00006886void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6887 Location out,
6888 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006889 DCHECK(kEmitCompilerReadBarrier);
6890
Roland Levillain7c1559a2015-12-15 10:55:36 +00006891 // Insert a slow path based read barrier *after* the GC root load.
6892 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006893 // Note that GC roots are not affected by heap poisoning, so we do
6894 // not need to do anything special for this here.
6895 SlowPathCode* slow_path =
6896 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6897 AddSlowPath(slow_path);
6898
Roland Levillain0d5a2812015-11-13 10:07:31 +00006899 __ jmp(slow_path->GetEntryLabel());
6900 __ Bind(slow_path->GetExitLabel());
6901}
6902
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006903void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006904 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006905 LOG(FATAL) << "Unreachable";
6906}
6907
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006908void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006909 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006910 LOG(FATAL) << "Unreachable";
6911}
6912
Mark Mendellfe57faa2015-09-18 09:26:15 -04006913// Simple implementation of packed switch - generate cascaded compare/jumps.
6914void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6915 LocationSummary* locations =
6916 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6917 locations->SetInAt(0, Location::RequiresRegister());
6918}
6919
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006920void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6921 int32_t lower_bound,
6922 uint32_t num_entries,
6923 HBasicBlock* switch_block,
6924 HBasicBlock* default_block) {
6925 // Figure out the correct compare values and jump conditions.
6926 // Handle the first compare/branch as a special case because it might
6927 // jump to the default case.
6928 DCHECK_GT(num_entries, 2u);
6929 Condition first_condition;
6930 uint32_t index;
6931 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6932 if (lower_bound != 0) {
6933 first_condition = kLess;
6934 __ cmpl(value_reg, Immediate(lower_bound));
6935 __ j(first_condition, codegen_->GetLabelOf(default_block));
6936 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006937
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006938 index = 1;
6939 } else {
6940 // Handle all the compare/jumps below.
6941 first_condition = kBelow;
6942 index = 0;
6943 }
6944
6945 // Handle the rest of the compare/jumps.
6946 for (; index + 1 < num_entries; index += 2) {
6947 int32_t compare_to_value = lower_bound + index + 1;
6948 __ cmpl(value_reg, Immediate(compare_to_value));
6949 // Jump to successors[index] if value < case_value[index].
6950 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6951 // Jump to successors[index + 1] if value == case_value[index + 1].
6952 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6953 }
6954
6955 if (index != num_entries) {
6956 // There are an odd number of entries. Handle the last one.
6957 DCHECK_EQ(index + 1, num_entries);
6958 __ cmpl(value_reg, Immediate(lower_bound + index));
6959 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006960 }
6961
6962 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006963 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6964 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006965 }
6966}
6967
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006968void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6969 int32_t lower_bound = switch_instr->GetStartValue();
6970 uint32_t num_entries = switch_instr->GetNumEntries();
6971 LocationSummary* locations = switch_instr->GetLocations();
6972 Register value_reg = locations->InAt(0).AsRegister<Register>();
6973
6974 GenPackedSwitchWithCompares(value_reg,
6975 lower_bound,
6976 num_entries,
6977 switch_instr->GetBlock(),
6978 switch_instr->GetDefaultBlock());
6979}
6980
Mark Mendell805b3b52015-09-18 14:10:29 -04006981void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6982 LocationSummary* locations =
6983 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6984 locations->SetInAt(0, Location::RequiresRegister());
6985
6986 // Constant area pointer.
6987 locations->SetInAt(1, Location::RequiresRegister());
6988
6989 // And the temporary we need.
6990 locations->AddTemp(Location::RequiresRegister());
6991}
6992
6993void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6994 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006995 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006996 LocationSummary* locations = switch_instr->GetLocations();
6997 Register value_reg = locations->InAt(0).AsRegister<Register>();
6998 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6999
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007000 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7001 GenPackedSwitchWithCompares(value_reg,
7002 lower_bound,
7003 num_entries,
7004 switch_instr->GetBlock(),
7005 default_block);
7006 return;
7007 }
7008
Mark Mendell805b3b52015-09-18 14:10:29 -04007009 // Optimizing has a jump area.
7010 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7011 Register constant_area = locations->InAt(1).AsRegister<Register>();
7012
7013 // Remove the bias, if needed.
7014 if (lower_bound != 0) {
7015 __ leal(temp_reg, Address(value_reg, -lower_bound));
7016 value_reg = temp_reg;
7017 }
7018
7019 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007020 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007021 __ cmpl(value_reg, Immediate(num_entries - 1));
7022 __ j(kAbove, codegen_->GetLabelOf(default_block));
7023
7024 // We are in the range of the table.
7025 // Load (target-constant_area) from the jump table, indexing by the value.
7026 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7027
7028 // Compute the actual target address by adding in constant_area.
7029 __ addl(temp_reg, constant_area);
7030
7031 // And jump.
7032 __ jmp(temp_reg);
7033}
7034
Mark Mendell0616ae02015-04-17 12:49:27 -04007035void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7036 HX86ComputeBaseMethodAddress* insn) {
7037 LocationSummary* locations =
7038 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7039 locations->SetOut(Location::RequiresRegister());
7040}
7041
7042void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7043 HX86ComputeBaseMethodAddress* insn) {
7044 LocationSummary* locations = insn->GetLocations();
7045 Register reg = locations->Out().AsRegister<Register>();
7046
7047 // Generate call to next instruction.
7048 Label next_instruction;
7049 __ call(&next_instruction);
7050 __ Bind(&next_instruction);
7051
7052 // Remember this offset for later use with constant area.
7053 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7054
7055 // Grab the return address off the stack.
7056 __ popl(reg);
7057}
7058
7059void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7060 HX86LoadFromConstantTable* insn) {
7061 LocationSummary* locations =
7062 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7063
7064 locations->SetInAt(0, Location::RequiresRegister());
7065 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7066
7067 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007068 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007069 return;
7070 }
7071
7072 switch (insn->GetType()) {
7073 case Primitive::kPrimFloat:
7074 case Primitive::kPrimDouble:
7075 locations->SetOut(Location::RequiresFpuRegister());
7076 break;
7077
7078 case Primitive::kPrimInt:
7079 locations->SetOut(Location::RequiresRegister());
7080 break;
7081
7082 default:
7083 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7084 }
7085}
7086
7087void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007088 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007089 return;
7090 }
7091
7092 LocationSummary* locations = insn->GetLocations();
7093 Location out = locations->Out();
7094 Register const_area = locations->InAt(0).AsRegister<Register>();
7095 HConstant *value = insn->GetConstant();
7096
7097 switch (insn->GetType()) {
7098 case Primitive::kPrimFloat:
7099 __ movss(out.AsFpuRegister<XmmRegister>(),
7100 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7101 break;
7102
7103 case Primitive::kPrimDouble:
7104 __ movsd(out.AsFpuRegister<XmmRegister>(),
7105 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7106 break;
7107
7108 case Primitive::kPrimInt:
7109 __ movl(out.AsRegister<Register>(),
7110 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7111 break;
7112
7113 default:
7114 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7115 }
7116}
7117
Mark Mendell0616ae02015-04-17 12:49:27 -04007118/**
7119 * Class to handle late fixup of offsets into constant area.
7120 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007121class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007122 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007123 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7124 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7125
7126 protected:
7127 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7128
7129 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007130
7131 private:
7132 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7133 // Patch the correct offset for the instruction. The place to patch is the
7134 // last 4 bytes of the instruction.
7135 // The value to patch is the distance from the offset in the constant area
7136 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007137 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7138 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007139
7140 // Patch in the right value.
7141 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7142 }
7143
Mark Mendell0616ae02015-04-17 12:49:27 -04007144 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007145 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007146};
7147
Mark Mendell805b3b52015-09-18 14:10:29 -04007148/**
7149 * Class to handle late fixup of offsets to a jump table that will be created in the
7150 * constant area.
7151 */
7152class JumpTableRIPFixup : public RIPFixup {
7153 public:
7154 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7155 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7156
7157 void CreateJumpTable() {
7158 X86Assembler* assembler = codegen_->GetAssembler();
7159
7160 // Ensure that the reference to the jump table has the correct offset.
7161 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7162 SetOffset(offset_in_constant_table);
7163
7164 // The label values in the jump table are computed relative to the
7165 // instruction addressing the constant area.
7166 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7167
7168 // Populate the jump table with the correct values for the jump table.
7169 int32_t num_entries = switch_instr_->GetNumEntries();
7170 HBasicBlock* block = switch_instr_->GetBlock();
7171 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7172 // The value that we want is the target offset - the position of the table.
7173 for (int32_t i = 0; i < num_entries; i++) {
7174 HBasicBlock* b = successors[i];
7175 Label* l = codegen_->GetLabelOf(b);
7176 DCHECK(l->IsBound());
7177 int32_t offset_to_block = l->Position() - relative_offset;
7178 assembler->AppendInt32(offset_to_block);
7179 }
7180 }
7181
7182 private:
7183 const HX86PackedSwitch* switch_instr_;
7184};
7185
7186void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7187 // Generate the constant area if needed.
7188 X86Assembler* assembler = GetAssembler();
7189 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7190 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7191 // byte values.
7192 assembler->Align(4, 0);
7193 constant_area_start_ = assembler->CodeSize();
7194
7195 // Populate any jump tables.
7196 for (auto jump_table : fixups_to_jump_tables_) {
7197 jump_table->CreateJumpTable();
7198 }
7199
7200 // And now add the constant area to the generated code.
7201 assembler->AddConstantArea();
7202 }
7203
7204 // And finish up.
7205 CodeGenerator::Finalize(allocator);
7206}
7207
Mark Mendell0616ae02015-04-17 12:49:27 -04007208Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7209 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7210 return Address(reg, kDummy32BitOffset, fixup);
7211}
7212
7213Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7214 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7215 return Address(reg, kDummy32BitOffset, fixup);
7216}
7217
7218Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7219 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7220 return Address(reg, kDummy32BitOffset, fixup);
7221}
7222
7223Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7224 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7225 return Address(reg, kDummy32BitOffset, fixup);
7226}
7227
Aart Bika19616e2016-02-01 18:57:58 -08007228void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7229 if (value == 0) {
7230 __ xorl(dest, dest);
7231 } else {
7232 __ movl(dest, Immediate(value));
7233 }
7234}
7235
7236void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7237 if (value == 0) {
7238 __ testl(dest, dest);
7239 } else {
7240 __ cmpl(dest, Immediate(value));
7241 }
7242}
7243
Mark Mendell805b3b52015-09-18 14:10:29 -04007244Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7245 Register reg,
7246 Register value) {
7247 // Create a fixup to be used to create and address the jump table.
7248 JumpTableRIPFixup* table_fixup =
7249 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7250
7251 // We have to populate the jump tables.
7252 fixups_to_jump_tables_.push_back(table_fixup);
7253
7254 // We want a scaled address, as we are extracting the correct offset from the table.
7255 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7256}
7257
Andreas Gampe85b62f22015-09-09 13:15:38 -07007258// TODO: target as memory.
7259void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7260 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007261 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007262 return;
7263 }
7264
7265 DCHECK_NE(type, Primitive::kPrimVoid);
7266
7267 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7268 if (target.Equals(return_loc)) {
7269 return;
7270 }
7271
7272 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7273 // with the else branch.
7274 if (type == Primitive::kPrimLong) {
7275 HParallelMove parallel_move(GetGraph()->GetArena());
7276 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7277 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7278 GetMoveResolver()->EmitNativeCode(&parallel_move);
7279 } else {
7280 // Let the parallel move resolver take care of all of this.
7281 HParallelMove parallel_move(GetGraph()->GetArena());
7282 parallel_move.AddMove(return_loc, target, type, nullptr);
7283 GetMoveResolver()->EmitNativeCode(&parallel_move);
7284 }
7285}
7286
Roland Levillain4d027112015-07-01 15:41:14 +01007287#undef __
7288
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007289} // namespace x86
7290} // namespace art