blob: 94d2f0c0a53e34f6aca362306dbbf0c1bc2ad522 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010029#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010033#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace x86 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050044static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Roland Levillain62a46b22015-06-01 18:24:13 +010050#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010051#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052
Andreas Gampe85b62f22015-09-09 13:15:38 -070053class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000055 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056
Alexandre Rames2ed20af2015-03-06 13:55:35 +000057 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010058 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000060 if (instruction_->CanThrowIntoCatchBlock()) {
61 // Live registers will be restored in the catch block if caught.
62 SaveLiveRegisters(codegen, instruction_->GetLocations());
63 }
Alexandre Rames8158f282015-08-07 10:26:17 +010064 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
65 instruction_,
66 instruction_->GetDexPc(),
67 this);
Roland Levillain888d0672015-11-23 18:53:50 +000068 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069 }
70
Alexandre Rames8158f282015-08-07 10:26:17 +010071 bool IsFatal() const OVERRIDE { return true; }
72
Alexandre Rames9931f312015-06-19 14:47:01 +010073 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
74
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
77};
78
Andreas Gampe85b62f22015-09-09 13:15:38 -070079class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000080 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000081 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000082
Alexandre Rames2ed20af2015-03-06 13:55:35 +000083 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010084 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000085 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000086 if (instruction_->CanThrowIntoCatchBlock()) {
87 // Live registers will be restored in the catch block if caught.
88 SaveLiveRegisters(codegen, instruction_->GetLocations());
89 }
Alexandre Rames8158f282015-08-07 10:26:17 +010090 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
91 instruction_,
92 instruction_->GetDexPc(),
93 this);
Roland Levillain888d0672015-11-23 18:53:50 +000094 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000095 }
96
Alexandre Rames8158f282015-08-07 10:26:17 +010097 bool IsFatal() const OVERRIDE { return true; }
98
Alexandre Rames9931f312015-06-19 14:47:01 +010099 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
100
Calin Juravled0d48522014-11-04 16:40:20 +0000101 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000102 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
103};
104
Andreas Gampe85b62f22015-09-09 13:15:38 -0700105class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000106 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000107 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
108 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000109
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000110 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000111 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000112 if (is_div_) {
113 __ negl(reg_);
114 } else {
115 __ movl(reg_, Immediate(0));
116 }
Calin Juravled0d48522014-11-04 16:40:20 +0000117 __ jmp(GetExitLabel());
118 }
119
Alexandre Rames9931f312015-06-19 14:47:01 +0100120 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
121
Calin Juravled0d48522014-11-04 16:40:20 +0000122 private:
123 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000124 bool is_div_;
125 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000126};
127
Andreas Gampe85b62f22015-09-09 13:15:38 -0700128class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000130 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100131
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000132 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100133 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100134 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100135 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000136 // We're moving two locations to locations that could overlap, so we need a parallel
137 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000138 if (instruction_->CanThrowIntoCatchBlock()) {
139 // Live registers will be restored in the catch block if caught.
140 SaveLiveRegisters(codegen, instruction_->GetLocations());
141 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000143 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100144 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000145 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100146 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100147 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100148 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
149 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100150 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
151 instruction_,
152 instruction_->GetDexPc(),
153 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000154 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155 }
156
Alexandre Rames8158f282015-08-07 10:26:17 +0100157 bool IsFatal() const OVERRIDE { return true; }
158
Alexandre Rames9931f312015-06-19 14:47:01 +0100159 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
160
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100161 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100162 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
163};
164
Andreas Gampe85b62f22015-09-09 13:15:38 -0700165class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000166 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000167 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000168 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000169
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000170 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100171 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000172 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000173 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100174 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
175 instruction_,
176 instruction_->GetDexPc(),
177 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000178 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000179 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100180 if (successor_ == nullptr) {
181 __ jmp(GetReturnLabel());
182 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100183 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100184 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000185 }
186
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100187 Label* GetReturnLabel() {
188 DCHECK(successor_ == nullptr);
189 return &return_label_;
190 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000191
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100192 HBasicBlock* GetSuccessor() const {
193 return successor_;
194 }
195
Alexandre Rames9931f312015-06-19 14:47:01 +0100196 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
197
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000198 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100199 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000200 Label return_label_;
201
202 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
203};
204
Andreas Gampe85b62f22015-09-09 13:15:38 -0700205class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000206 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000207 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000208
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000209 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000210 LocationSummary* locations = instruction_->GetLocations();
211 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
212
213 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
214 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000215 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000216
217 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000218 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
219 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
Alexandre Rames8158f282015-08-07 10:26:17 +0100220 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
221 instruction_,
222 instruction_->GetDexPc(),
223 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000224 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000225 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000226 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000227
228 __ jmp(GetExitLabel());
229 }
230
Alexandre Rames9931f312015-06-19 14:47:01 +0100231 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
232
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000233 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000234 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
235};
236
Andreas Gampe85b62f22015-09-09 13:15:38 -0700237class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000238 public:
239 LoadClassSlowPathX86(HLoadClass* cls,
240 HInstruction* at,
241 uint32_t dex_pc,
242 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000243 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
245 }
246
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000247 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000248 LocationSummary* locations = at_->GetLocations();
249 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
250 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000251 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000252
253 InvokeRuntimeCallingConvention calling_convention;
254 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100255 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
256 : QUICK_ENTRY_POINT(pInitializeType),
257 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000258 if (do_clinit_) {
259 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
260 } else {
261 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
262 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263
264 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000265 Location out = locations->Out();
266 if (out.IsValid()) {
267 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
268 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000270
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000271 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000272 __ jmp(GetExitLabel());
273 }
274
Alexandre Rames9931f312015-06-19 14:47:01 +0100275 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
276
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277 private:
278 // The class this slow path will load.
279 HLoadClass* const cls_;
280
281 // The instruction where this slow path is happening.
282 // (Might be the load class or an initialization check).
283 HInstruction* const at_;
284
285 // The dex PC of `at_`.
286 const uint32_t dex_pc_;
287
288 // Whether to initialize the class.
289 const bool do_clinit_;
290
291 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
292};
293
Andreas Gampe85b62f22015-09-09 13:15:38 -0700294class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000295 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000296 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000297 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000299 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100301 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
302 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000303 DCHECK(instruction_->IsCheckCast()
304 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305
306 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
307 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000308
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000309 if (!is_fatal_) {
310 SaveLiveRegisters(codegen, locations);
311 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312
313 // We're moving two locations to locations that could overlap, so we need a parallel
314 // move resolver.
315 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000316 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100317 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000318 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100319 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100320 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100321 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
322 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000324 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100325 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
326 instruction_,
327 instruction_->GetDexPc(),
328 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000329 CheckEntrypointTypes<
330 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000331 } else {
332 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100333 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
334 instruction_,
335 instruction_->GetDexPc(),
336 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000337 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000338 }
339
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000340 if (!is_fatal_) {
341 if (instruction_->IsInstanceOf()) {
342 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
343 }
344 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000345
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000346 __ jmp(GetExitLabel());
347 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000348 }
349
Alexandre Rames9931f312015-06-19 14:47:01 +0100350 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000351 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100352
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000354 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000355
356 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
357};
358
Andreas Gampe85b62f22015-09-09 13:15:38 -0700359class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700360 public:
Aart Bik42249c32016-01-07 15:33:50 -0800361 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000362 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700363
364 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100365 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700366 __ Bind(GetEntryLabel());
367 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100368 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
369 instruction_,
370 instruction_->GetDexPc(),
371 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000372 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700373 }
374
Alexandre Rames9931f312015-06-19 14:47:01 +0100375 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
376
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700377 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
379};
380
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100381class ArraySetSlowPathX86 : public SlowPathCode {
382 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000383 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100384
385 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
386 LocationSummary* locations = instruction_->GetLocations();
387 __ Bind(GetEntryLabel());
388 SaveLiveRegisters(codegen, locations);
389
390 InvokeRuntimeCallingConvention calling_convention;
391 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
392 parallel_move.AddMove(
393 locations->InAt(0),
394 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
395 Primitive::kPrimNot,
396 nullptr);
397 parallel_move.AddMove(
398 locations->InAt(1),
399 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
400 Primitive::kPrimInt,
401 nullptr);
402 parallel_move.AddMove(
403 locations->InAt(2),
404 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
405 Primitive::kPrimNot,
406 nullptr);
407 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
408
409 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
410 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
411 instruction_,
412 instruction_->GetDexPc(),
413 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000414 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100415 RestoreLiveRegisters(codegen, locations);
416 __ jmp(GetExitLabel());
417 }
418
419 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
420
421 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100422 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
423};
424
Roland Levillain7c1559a2015-12-15 10:55:36 +0000425// Slow path marking an object during a read barrier.
426class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
427 public:
428 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000429 : SlowPathCode(instruction), out_(out), obj_(obj) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000430 DCHECK(kEmitCompilerReadBarrier);
431 }
432
433 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
434
435 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
436 LocationSummary* locations = instruction_->GetLocations();
437 Register reg_out = out_.AsRegister<Register>();
438 DCHECK(locations->CanCall());
439 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
440 DCHECK(instruction_->IsInstanceFieldGet() ||
441 instruction_->IsStaticFieldGet() ||
442 instruction_->IsArrayGet() ||
443 instruction_->IsLoadClass() ||
444 instruction_->IsLoadString() ||
445 instruction_->IsInstanceOf() ||
446 instruction_->IsCheckCast())
447 << "Unexpected instruction in read barrier marking slow path: "
448 << instruction_->DebugName();
449
450 __ Bind(GetEntryLabel());
451 SaveLiveRegisters(codegen, locations);
452
453 InvokeRuntimeCallingConvention calling_convention;
454 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
455 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
456 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
457 instruction_,
458 instruction_->GetDexPc(),
459 this);
460 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
461 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
462
463 RestoreLiveRegisters(codegen, locations);
464 __ jmp(GetExitLabel());
465 }
466
467 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000468 const Location out_;
469 const Location obj_;
470
471 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
472};
473
Roland Levillain0d5a2812015-11-13 10:07:31 +0000474// Slow path generating a read barrier for a heap reference.
475class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
476 public:
477 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
478 Location out,
479 Location ref,
480 Location obj,
481 uint32_t offset,
482 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000483 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000484 out_(out),
485 ref_(ref),
486 obj_(obj),
487 offset_(offset),
488 index_(index) {
489 DCHECK(kEmitCompilerReadBarrier);
490 // If `obj` is equal to `out` or `ref`, it means the initial object
491 // has been overwritten by (or after) the heap object reference load
492 // to be instrumented, e.g.:
493 //
494 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000495 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000496 //
497 // In that case, we have lost the information about the original
498 // object, and the emitted read barrier cannot work properly.
499 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
500 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
501 }
502
503 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
504 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
505 LocationSummary* locations = instruction_->GetLocations();
506 Register reg_out = out_.AsRegister<Register>();
507 DCHECK(locations->CanCall());
508 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
509 DCHECK(!instruction_->IsInvoke() ||
510 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000511 instruction_->GetLocations()->Intrinsified()))
512 << "Unexpected instruction in read barrier for heap reference slow path: "
513 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000514
515 __ Bind(GetEntryLabel());
516 SaveLiveRegisters(codegen, locations);
517
518 // We may have to change the index's value, but as `index_` is a
519 // constant member (like other "inputs" of this slow path),
520 // introduce a copy of it, `index`.
521 Location index = index_;
522 if (index_.IsValid()) {
523 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
524 if (instruction_->IsArrayGet()) {
525 // Compute the actual memory offset and store it in `index`.
526 Register index_reg = index_.AsRegister<Register>();
527 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
528 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
529 // We are about to change the value of `index_reg` (see the
530 // calls to art::x86::X86Assembler::shll and
531 // art::x86::X86Assembler::AddImmediate below), but it has
532 // not been saved by the previous call to
533 // art::SlowPathCode::SaveLiveRegisters, as it is a
534 // callee-save register --
535 // art::SlowPathCode::SaveLiveRegisters does not consider
536 // callee-save registers, as it has been designed with the
537 // assumption that callee-save registers are supposed to be
538 // handled by the called function. So, as a callee-save
539 // register, `index_reg` _would_ eventually be saved onto
540 // the stack, but it would be too late: we would have
541 // changed its value earlier. Therefore, we manually save
542 // it here into another freely available register,
543 // `free_reg`, chosen of course among the caller-save
544 // registers (as a callee-save `free_reg` register would
545 // exhibit the same problem).
546 //
547 // Note we could have requested a temporary register from
548 // the register allocator instead; but we prefer not to, as
549 // this is a slow path, and we know we can find a
550 // caller-save register that is available.
551 Register free_reg = FindAvailableCallerSaveRegister(codegen);
552 __ movl(free_reg, index_reg);
553 index_reg = free_reg;
554 index = Location::RegisterLocation(index_reg);
555 } else {
556 // The initial register stored in `index_` has already been
557 // saved in the call to art::SlowPathCode::SaveLiveRegisters
558 // (as it is not a callee-save register), so we can freely
559 // use it.
560 }
561 // Shifting the index value contained in `index_reg` by the scale
562 // factor (2) cannot overflow in practice, as the runtime is
563 // unable to allocate object arrays with a size larger than
564 // 2^26 - 1 (that is, 2^28 - 4 bytes).
565 __ shll(index_reg, Immediate(TIMES_4));
566 static_assert(
567 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
568 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
569 __ AddImmediate(index_reg, Immediate(offset_));
570 } else {
571 DCHECK(instruction_->IsInvoke());
572 DCHECK(instruction_->GetLocations()->Intrinsified());
573 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
574 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
575 << instruction_->AsInvoke()->GetIntrinsic();
576 DCHECK_EQ(offset_, 0U);
577 DCHECK(index_.IsRegisterPair());
578 // UnsafeGet's offset location is a register pair, the low
579 // part contains the correct offset.
580 index = index_.ToLow();
581 }
582 }
583
584 // We're moving two or three locations to locations that could
585 // overlap, so we need a parallel move resolver.
586 InvokeRuntimeCallingConvention calling_convention;
587 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
588 parallel_move.AddMove(ref_,
589 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
590 Primitive::kPrimNot,
591 nullptr);
592 parallel_move.AddMove(obj_,
593 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
594 Primitive::kPrimNot,
595 nullptr);
596 if (index.IsValid()) {
597 parallel_move.AddMove(index,
598 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
599 Primitive::kPrimInt,
600 nullptr);
601 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
602 } else {
603 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
604 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
605 }
606 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
607 instruction_,
608 instruction_->GetDexPc(),
609 this);
610 CheckEntrypointTypes<
611 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
612 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
613
614 RestoreLiveRegisters(codegen, locations);
615 __ jmp(GetExitLabel());
616 }
617
618 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
619
620 private:
621 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
622 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
623 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
624 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
625 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
626 return static_cast<Register>(i);
627 }
628 }
629 // We shall never fail to find a free caller-save register, as
630 // there are more than two core caller-save registers on x86
631 // (meaning it is possible to find one which is different from
632 // `ref` and `obj`).
633 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
634 LOG(FATAL) << "Could not find a free caller-save register";
635 UNREACHABLE();
636 }
637
Roland Levillain0d5a2812015-11-13 10:07:31 +0000638 const Location out_;
639 const Location ref_;
640 const Location obj_;
641 const uint32_t offset_;
642 // An additional location containing an index to an array.
643 // Only used for HArrayGet and the UnsafeGetObject &
644 // UnsafeGetObjectVolatile intrinsics.
645 const Location index_;
646
647 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
648};
649
650// Slow path generating a read barrier for a GC root.
651class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
652 public:
653 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000654 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000655 DCHECK(kEmitCompilerReadBarrier);
656 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000657
658 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
659 LocationSummary* locations = instruction_->GetLocations();
660 Register reg_out = out_.AsRegister<Register>();
661 DCHECK(locations->CanCall());
662 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000663 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
664 << "Unexpected instruction in read barrier for GC root slow path: "
665 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000666
667 __ Bind(GetEntryLabel());
668 SaveLiveRegisters(codegen, locations);
669
670 InvokeRuntimeCallingConvention calling_convention;
671 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
672 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
673 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
674 instruction_,
675 instruction_->GetDexPc(),
676 this);
677 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
678 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
679
680 RestoreLiveRegisters(codegen, locations);
681 __ jmp(GetExitLabel());
682 }
683
684 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
685
686 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000687 const Location out_;
688 const Location root_;
689
690 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
691};
692
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100693#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100694#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100695
Aart Bike9f37602015-10-09 11:15:55 -0700696inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700697 switch (cond) {
698 case kCondEQ: return kEqual;
699 case kCondNE: return kNotEqual;
700 case kCondLT: return kLess;
701 case kCondLE: return kLessEqual;
702 case kCondGT: return kGreater;
703 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700704 case kCondB: return kBelow;
705 case kCondBE: return kBelowEqual;
706 case kCondA: return kAbove;
707 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700708 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100709 LOG(FATAL) << "Unreachable";
710 UNREACHABLE();
711}
712
Aart Bike9f37602015-10-09 11:15:55 -0700713// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100714inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
715 switch (cond) {
716 case kCondEQ: return kEqual;
717 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700718 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100719 case kCondLT: return kBelow;
720 case kCondLE: return kBelowEqual;
721 case kCondGT: return kAbove;
722 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700723 // Unsigned remain unchanged.
724 case kCondB: return kBelow;
725 case kCondBE: return kBelowEqual;
726 case kCondA: return kAbove;
727 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100728 }
729 LOG(FATAL) << "Unreachable";
730 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700731}
732
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100733void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100734 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100735}
736
737void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100738 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100739}
740
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100741size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
742 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
743 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100744}
745
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100746size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
747 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
748 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100749}
750
Mark Mendell7c8d0092015-01-26 11:21:33 -0500751size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
752 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
753 return GetFloatingPointSpillSlotSize();
754}
755
756size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
757 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
758 return GetFloatingPointSpillSlotSize();
759}
760
Calin Juravle175dc732015-08-25 15:42:32 +0100761void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
762 HInstruction* instruction,
763 uint32_t dex_pc,
764 SlowPathCode* slow_path) {
765 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
766 instruction,
767 dex_pc,
768 slow_path);
769}
770
771void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100772 HInstruction* instruction,
773 uint32_t dex_pc,
774 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100775 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100776 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100777 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100778}
779
Mark Mendellfb8d2792015-03-31 22:16:59 -0400780CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000781 const X86InstructionSetFeatures& isa_features,
782 const CompilerOptions& compiler_options,
783 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500784 : CodeGenerator(graph,
785 kNumberOfCpuRegisters,
786 kNumberOfXmmRegisters,
787 kNumberOfRegisterPairs,
788 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
789 arraysize(kCoreCalleeSaves))
790 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100791 0,
792 compiler_options,
793 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100794 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100795 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100796 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400797 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000798 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100799 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400800 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000801 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000802 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
803 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400804 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000805 // Use a fake return address register to mimic Quick.
806 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100807}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100808
David Brazdil58282f42016-01-14 12:45:10 +0000809void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100810 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100811 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100812
813 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100814 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100815
Calin Juravle34bacdf2014-10-07 20:23:36 +0100816 UpdateBlockedPairRegisters();
817}
818
819void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
820 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
821 X86ManagedRegister current =
822 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
823 if (blocked_core_registers_[current.AsRegisterPairLow()]
824 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
825 blocked_register_pairs_[i] = true;
826 }
827 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100828}
829
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100830InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800831 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100832 assembler_(codegen->GetAssembler()),
833 codegen_(codegen) {}
834
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100835static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100836 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100837}
838
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000839void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100840 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000841 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000842 bool skip_overflow_check =
843 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000844 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000845
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000846 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100847 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100848 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100849 }
850
Mark Mendell5f874182015-03-04 15:42:45 -0500851 if (HasEmptyFrame()) {
852 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000853 }
Mark Mendell5f874182015-03-04 15:42:45 -0500854
855 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
856 Register reg = kCoreCalleeSaves[i];
857 if (allocated_registers_.ContainsCoreRegister(reg)) {
858 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100859 __ cfi().AdjustCFAOffset(kX86WordSize);
860 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500861 }
862 }
863
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100864 int adjust = GetFrameSize() - FrameEntrySpillSize();
865 __ subl(ESP, Immediate(adjust));
866 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100867 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000868}
869
870void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100871 __ cfi().RememberState();
872 if (!HasEmptyFrame()) {
873 int adjust = GetFrameSize() - FrameEntrySpillSize();
874 __ addl(ESP, Immediate(adjust));
875 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500876
David Srbeckyc34dc932015-04-12 09:27:43 +0100877 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
878 Register reg = kCoreCalleeSaves[i];
879 if (allocated_registers_.ContainsCoreRegister(reg)) {
880 __ popl(reg);
881 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
882 __ cfi().Restore(DWARFReg(reg));
883 }
Mark Mendell5f874182015-03-04 15:42:45 -0500884 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000885 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100886 __ ret();
887 __ cfi().RestoreState();
888 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000889}
890
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100891void CodeGeneratorX86::Bind(HBasicBlock* block) {
892 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000893}
894
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100895Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
896 switch (type) {
897 case Primitive::kPrimBoolean:
898 case Primitive::kPrimByte:
899 case Primitive::kPrimChar:
900 case Primitive::kPrimShort:
901 case Primitive::kPrimInt:
902 case Primitive::kPrimNot:
903 return Location::RegisterLocation(EAX);
904
905 case Primitive::kPrimLong:
906 return Location::RegisterPairLocation(EAX, EDX);
907
908 case Primitive::kPrimVoid:
909 return Location::NoLocation();
910
911 case Primitive::kPrimDouble:
912 case Primitive::kPrimFloat:
913 return Location::FpuRegisterLocation(XMM0);
914 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100915
916 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100917}
918
919Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
920 return Location::RegisterLocation(kMethodRegisterArgument);
921}
922
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100923Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100924 switch (type) {
925 case Primitive::kPrimBoolean:
926 case Primitive::kPrimByte:
927 case Primitive::kPrimChar:
928 case Primitive::kPrimShort:
929 case Primitive::kPrimInt:
930 case Primitive::kPrimNot: {
931 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000932 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100933 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100934 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100935 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000936 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100937 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100938 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100939
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000940 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100941 uint32_t index = gp_index_;
942 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000943 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100944 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100945 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
946 calling_convention.GetRegisterPairAt(index));
947 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100948 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000949 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
950 }
951 }
952
953 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100954 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000955 stack_index_++;
956 if (index < calling_convention.GetNumberOfFpuRegisters()) {
957 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
958 } else {
959 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
960 }
961 }
962
963 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100964 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000965 stack_index_ += 2;
966 if (index < calling_convention.GetNumberOfFpuRegisters()) {
967 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
968 } else {
969 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100970 }
971 }
972
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100973 case Primitive::kPrimVoid:
974 LOG(FATAL) << "Unexpected parameter type " << type;
975 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100976 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000977 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100978}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100979
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100980void CodeGeneratorX86::Move32(Location destination, Location source) {
981 if (source.Equals(destination)) {
982 return;
983 }
984 if (destination.IsRegister()) {
985 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000986 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100987 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000988 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100989 } else {
990 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000991 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100992 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100993 } else if (destination.IsFpuRegister()) {
994 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000995 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100996 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000997 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100998 } else {
999 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001000 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001001 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001003 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001004 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001005 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001006 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001007 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001008 } else if (source.IsConstant()) {
1009 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001010 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001011 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012 } else {
1013 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001014 __ pushl(Address(ESP, source.GetStackIndex()));
1015 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001016 }
1017 }
1018}
1019
1020void CodeGeneratorX86::Move64(Location destination, Location source) {
1021 if (source.Equals(destination)) {
1022 return;
1023 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001024 if (destination.IsRegisterPair()) {
1025 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001026 EmitParallelMoves(
1027 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1028 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001029 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001030 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001031 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1032 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001033 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001034 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1035 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1036 __ psrlq(src_reg, Immediate(32));
1037 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001038 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001039 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001040 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001041 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1042 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001043 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1044 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001045 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001046 if (source.IsFpuRegister()) {
1047 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1048 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001049 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001050 } else if (source.IsRegisterPair()) {
1051 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1052 // Create stack space for 2 elements.
1053 __ subl(ESP, Immediate(2 * elem_size));
1054 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1055 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1056 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1057 // And remove the temporary stack space we allocated.
1058 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001059 } else {
1060 LOG(FATAL) << "Unimplemented";
1061 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001062 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001063 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001064 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001065 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001066 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001068 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001069 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001070 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001071 } else if (source.IsConstant()) {
1072 HConstant* constant = source.GetConstant();
1073 int64_t value;
1074 if (constant->IsLongConstant()) {
1075 value = constant->AsLongConstant()->GetValue();
1076 } else {
1077 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001078 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001079 }
1080 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1081 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001082 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001083 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001084 EmitParallelMoves(
1085 Location::StackSlot(source.GetStackIndex()),
1086 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001087 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001088 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001089 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1090 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 }
1092 }
1093}
1094
Calin Juravle175dc732015-08-25 15:42:32 +01001095void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1096 DCHECK(location.IsRegister());
1097 __ movl(location.AsRegister<Register>(), Immediate(value));
1098}
1099
Calin Juravlee460d1d2015-09-29 04:52:17 +01001100void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001101 HParallelMove move(GetGraph()->GetArena());
1102 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1103 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1104 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001105 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001106 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001107 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001108 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001109}
1110
1111void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1112 if (location.IsRegister()) {
1113 locations->AddTemp(location);
1114 } else if (location.IsRegisterPair()) {
1115 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1116 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1117 } else {
1118 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1119 }
1120}
1121
David Brazdilfc6a86a2015-06-26 10:33:45 +00001122void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001123 DCHECK(!successor->IsExitBlock());
1124
1125 HBasicBlock* block = got->GetBlock();
1126 HInstruction* previous = got->GetPrevious();
1127
1128 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001129 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001130 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1131 return;
1132 }
1133
1134 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1135 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1136 }
1137 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001138 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001139 }
1140}
1141
David Brazdilfc6a86a2015-06-26 10:33:45 +00001142void LocationsBuilderX86::VisitGoto(HGoto* got) {
1143 got->SetLocations(nullptr);
1144}
1145
1146void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1147 HandleGoto(got, got->GetSuccessor());
1148}
1149
1150void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1151 try_boundary->SetLocations(nullptr);
1152}
1153
1154void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1155 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1156 if (!successor->IsExitBlock()) {
1157 HandleGoto(try_boundary, successor);
1158 }
1159}
1160
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001161void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001162 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001163}
1164
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001165void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001166}
1167
Mark Mendell152408f2015-12-31 12:28:50 -05001168template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001169void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001170 LabelType* true_label,
1171 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001172 if (cond->IsFPConditionTrueIfNaN()) {
1173 __ j(kUnordered, true_label);
1174 } else if (cond->IsFPConditionFalseIfNaN()) {
1175 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001176 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001177 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001178}
1179
Mark Mendell152408f2015-12-31 12:28:50 -05001180template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001181void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001182 LabelType* true_label,
1183 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001184 LocationSummary* locations = cond->GetLocations();
1185 Location left = locations->InAt(0);
1186 Location right = locations->InAt(1);
1187 IfCondition if_cond = cond->GetCondition();
1188
Mark Mendellc4701932015-04-10 13:18:51 -04001189 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001190 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001191 IfCondition true_high_cond = if_cond;
1192 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001193 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001194
1195 // Set the conditions for the test, remembering that == needs to be
1196 // decided using the low words.
1197 switch (if_cond) {
1198 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001199 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001200 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001201 break;
1202 case kCondLT:
1203 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001204 break;
1205 case kCondLE:
1206 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001207 break;
1208 case kCondGT:
1209 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001210 break;
1211 case kCondGE:
1212 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001213 break;
Aart Bike9f37602015-10-09 11:15:55 -07001214 case kCondB:
1215 false_high_cond = kCondA;
1216 break;
1217 case kCondBE:
1218 true_high_cond = kCondB;
1219 break;
1220 case kCondA:
1221 false_high_cond = kCondB;
1222 break;
1223 case kCondAE:
1224 true_high_cond = kCondA;
1225 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001226 }
1227
1228 if (right.IsConstant()) {
1229 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001230 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001231 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001232
Aart Bika19616e2016-02-01 18:57:58 -08001233 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001234 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001235 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001236 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001237 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001238 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001239 __ j(X86Condition(true_high_cond), true_label);
1240 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001241 }
1242 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001243 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001244 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001245 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001246 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001247
1248 __ cmpl(left_high, right_high);
1249 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001250 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001251 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001252 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001253 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001254 __ j(X86Condition(true_high_cond), true_label);
1255 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001256 }
1257 // Must be equal high, so compare the lows.
1258 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001259 } else {
1260 DCHECK(right.IsDoubleStackSlot());
1261 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1262 if (if_cond == kCondNE) {
1263 __ j(X86Condition(true_high_cond), true_label);
1264 } else if (if_cond == kCondEQ) {
1265 __ j(X86Condition(false_high_cond), false_label);
1266 } else {
1267 __ j(X86Condition(true_high_cond), true_label);
1268 __ j(X86Condition(false_high_cond), false_label);
1269 }
1270 // Must be equal high, so compare the lows.
1271 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001272 }
1273 // The last comparison might be unsigned.
1274 __ j(final_condition, true_label);
1275}
1276
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001277void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1278 Location rhs,
1279 HInstruction* insn,
1280 bool is_double) {
1281 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1282 if (is_double) {
1283 if (rhs.IsFpuRegister()) {
1284 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1285 } else if (const_area != nullptr) {
1286 DCHECK(const_area->IsEmittedAtUseSite());
1287 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1288 codegen_->LiteralDoubleAddress(
1289 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1290 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1291 } else {
1292 DCHECK(rhs.IsDoubleStackSlot());
1293 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1294 }
1295 } else {
1296 if (rhs.IsFpuRegister()) {
1297 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1298 } else if (const_area != nullptr) {
1299 DCHECK(const_area->IsEmittedAtUseSite());
1300 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1301 codegen_->LiteralFloatAddress(
1302 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1303 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1304 } else {
1305 DCHECK(rhs.IsStackSlot());
1306 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1307 }
1308 }
1309}
1310
Mark Mendell152408f2015-12-31 12:28:50 -05001311template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001312void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001313 LabelType* true_target_in,
1314 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001315 // Generated branching requires both targets to be explicit. If either of the
1316 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001317 LabelType fallthrough_target;
1318 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1319 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001320
Mark Mendellc4701932015-04-10 13:18:51 -04001321 LocationSummary* locations = condition->GetLocations();
1322 Location left = locations->InAt(0);
1323 Location right = locations->InAt(1);
1324
Mark Mendellc4701932015-04-10 13:18:51 -04001325 Primitive::Type type = condition->InputAt(0)->GetType();
1326 switch (type) {
1327 case Primitive::kPrimLong:
1328 GenerateLongComparesAndJumps(condition, true_target, false_target);
1329 break;
1330 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001331 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001332 GenerateFPJumps(condition, true_target, false_target);
1333 break;
1334 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001335 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001336 GenerateFPJumps(condition, true_target, false_target);
1337 break;
1338 default:
1339 LOG(FATAL) << "Unexpected compare type " << type;
1340 }
1341
David Brazdil0debae72015-11-12 18:37:00 +00001342 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001343 __ jmp(false_target);
1344 }
David Brazdil0debae72015-11-12 18:37:00 +00001345
1346 if (fallthrough_target.IsLinked()) {
1347 __ Bind(&fallthrough_target);
1348 }
Mark Mendellc4701932015-04-10 13:18:51 -04001349}
1350
David Brazdil0debae72015-11-12 18:37:00 +00001351static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1352 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1353 // are set only strictly before `branch`. We can't use the eflags on long/FP
1354 // conditions if they are materialized due to the complex branching.
1355 return cond->IsCondition() &&
1356 cond->GetNext() == branch &&
1357 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1358 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1359}
1360
Mark Mendell152408f2015-12-31 12:28:50 -05001361template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001362void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001363 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001364 LabelType* true_target,
1365 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001366 HInstruction* cond = instruction->InputAt(condition_input_index);
1367
1368 if (true_target == nullptr && false_target == nullptr) {
1369 // Nothing to do. The code always falls through.
1370 return;
1371 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001372 // Constant condition, statically compared against "true" (integer value 1).
1373 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001374 if (true_target != nullptr) {
1375 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001376 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001377 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001378 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001379 if (false_target != nullptr) {
1380 __ jmp(false_target);
1381 }
1382 }
1383 return;
1384 }
1385
1386 // The following code generates these patterns:
1387 // (1) true_target == nullptr && false_target != nullptr
1388 // - opposite condition true => branch to false_target
1389 // (2) true_target != nullptr && false_target == nullptr
1390 // - condition true => branch to true_target
1391 // (3) true_target != nullptr && false_target != nullptr
1392 // - condition true => branch to true_target
1393 // - branch to false_target
1394 if (IsBooleanValueOrMaterializedCondition(cond)) {
1395 if (AreEflagsSetFrom(cond, instruction)) {
1396 if (true_target == nullptr) {
1397 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1398 } else {
1399 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1400 }
1401 } else {
1402 // Materialized condition, compare against 0.
1403 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1404 if (lhs.IsRegister()) {
1405 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1406 } else {
1407 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1408 }
1409 if (true_target == nullptr) {
1410 __ j(kEqual, false_target);
1411 } else {
1412 __ j(kNotEqual, true_target);
1413 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001414 }
1415 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001416 // Condition has not been materialized, use its inputs as the comparison and
1417 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001418 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001419
1420 // If this is a long or FP comparison that has been folded into
1421 // the HCondition, generate the comparison directly.
1422 Primitive::Type type = condition->InputAt(0)->GetType();
1423 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1424 GenerateCompareTestAndBranch(condition, true_target, false_target);
1425 return;
1426 }
1427
1428 Location lhs = condition->GetLocations()->InAt(0);
1429 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001430 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001431 if (rhs.IsRegister()) {
1432 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1433 } else if (rhs.IsConstant()) {
1434 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001435 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001436 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001437 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1438 }
1439 if (true_target == nullptr) {
1440 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1441 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001442 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001443 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001444 }
David Brazdil0debae72015-11-12 18:37:00 +00001445
1446 // If neither branch falls through (case 3), the conditional branch to `true_target`
1447 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1448 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001449 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001450 }
1451}
1452
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001453void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001454 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1455 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001456 locations->SetInAt(0, Location::Any());
1457 }
1458}
1459
1460void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001461 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1462 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1463 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1464 nullptr : codegen_->GetLabelOf(true_successor);
1465 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1466 nullptr : codegen_->GetLabelOf(false_successor);
1467 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001468}
1469
1470void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1471 LocationSummary* locations = new (GetGraph()->GetArena())
1472 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001473 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001474 locations->SetInAt(0, Location::Any());
1475 }
1476}
1477
1478void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001479 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001480 GenerateTestAndBranch<Label>(deoptimize,
1481 /* condition_input_index */ 0,
1482 slow_path->GetEntryLabel(),
1483 /* false_target */ nullptr);
1484}
1485
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001486static bool SelectCanUseCMOV(HSelect* select) {
1487 // There are no conditional move instructions for XMMs.
1488 if (Primitive::IsFloatingPointType(select->GetType())) {
1489 return false;
1490 }
1491
1492 // A FP condition doesn't generate the single CC that we need.
1493 // In 32 bit mode, a long condition doesn't generate a single CC either.
1494 HInstruction* condition = select->GetCondition();
1495 if (condition->IsCondition()) {
1496 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1497 if (compare_type == Primitive::kPrimLong ||
1498 Primitive::IsFloatingPointType(compare_type)) {
1499 return false;
1500 }
1501 }
1502
1503 // We can generate a CMOV for this Select.
1504 return true;
1505}
1506
David Brazdil74eb1b22015-12-14 11:44:01 +00001507void LocationsBuilderX86::VisitSelect(HSelect* select) {
1508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001509 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001510 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001511 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001512 } else {
1513 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001514 if (SelectCanUseCMOV(select)) {
1515 if (select->InputAt(1)->IsConstant()) {
1516 // Cmov can't handle a constant value.
1517 locations->SetInAt(1, Location::RequiresRegister());
1518 } else {
1519 locations->SetInAt(1, Location::Any());
1520 }
1521 } else {
1522 locations->SetInAt(1, Location::Any());
1523 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001524 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001525 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1526 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001527 }
1528 locations->SetOut(Location::SameAsFirstInput());
1529}
1530
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001531void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1532 Register lhs_reg = lhs.AsRegister<Register>();
1533 if (rhs.IsConstant()) {
1534 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1535 codegen_->Compare32BitValue(lhs_reg, value);
1536 } else if (rhs.IsStackSlot()) {
1537 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1538 } else {
1539 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1540 }
1541}
1542
David Brazdil74eb1b22015-12-14 11:44:01 +00001543void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1544 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001545 DCHECK(locations->InAt(0).Equals(locations->Out()));
1546 if (SelectCanUseCMOV(select)) {
1547 // If both the condition and the source types are integer, we can generate
1548 // a CMOV to implement Select.
1549
1550 HInstruction* select_condition = select->GetCondition();
1551 Condition cond = kNotEqual;
1552
1553 // Figure out how to test the 'condition'.
1554 if (select_condition->IsCondition()) {
1555 HCondition* condition = select_condition->AsCondition();
1556 if (!condition->IsEmittedAtUseSite()) {
1557 // This was a previously materialized condition.
1558 // Can we use the existing condition code?
1559 if (AreEflagsSetFrom(condition, select)) {
1560 // Materialization was the previous instruction. Condition codes are right.
1561 cond = X86Condition(condition->GetCondition());
1562 } else {
1563 // No, we have to recreate the condition code.
1564 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1565 __ testl(cond_reg, cond_reg);
1566 }
1567 } else {
1568 // We can't handle FP or long here.
1569 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1570 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1571 LocationSummary* cond_locations = condition->GetLocations();
1572 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1573 cond = X86Condition(condition->GetCondition());
1574 }
1575 } else {
1576 // Must be a boolean condition, which needs to be compared to 0.
1577 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1578 __ testl(cond_reg, cond_reg);
1579 }
1580
1581 // If the condition is true, overwrite the output, which already contains false.
1582 Location false_loc = locations->InAt(0);
1583 Location true_loc = locations->InAt(1);
1584 if (select->GetType() == Primitive::kPrimLong) {
1585 // 64 bit conditional move.
1586 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1587 Register false_low = false_loc.AsRegisterPairLow<Register>();
1588 if (true_loc.IsRegisterPair()) {
1589 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1590 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1591 } else {
1592 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1593 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1594 }
1595 } else {
1596 // 32 bit conditional move.
1597 Register false_reg = false_loc.AsRegister<Register>();
1598 if (true_loc.IsRegister()) {
1599 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1600 } else {
1601 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1602 }
1603 }
1604 } else {
1605 NearLabel false_target;
1606 GenerateTestAndBranch<NearLabel>(
1607 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1608 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1609 __ Bind(&false_target);
1610 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001611}
1612
David Srbecky0cf44932015-12-09 14:09:59 +00001613void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1614 new (GetGraph()->GetArena()) LocationSummary(info);
1615}
1616
David Srbeckyd28f4a02016-03-14 17:14:24 +00001617void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1618 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001619}
1620
1621void CodeGeneratorX86::GenerateNop() {
1622 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001623}
1624
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001625void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001626 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001627 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001628 // Handle the long/FP comparisons made in instruction simplification.
1629 switch (cond->InputAt(0)->GetType()) {
1630 case Primitive::kPrimLong: {
1631 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001632 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001633 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001634 locations->SetOut(Location::RequiresRegister());
1635 }
1636 break;
1637 }
1638 case Primitive::kPrimFloat:
1639 case Primitive::kPrimDouble: {
1640 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001641 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1642 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1643 } else if (cond->InputAt(1)->IsConstant()) {
1644 locations->SetInAt(1, Location::RequiresFpuRegister());
1645 } else {
1646 locations->SetInAt(1, Location::Any());
1647 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001648 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001649 locations->SetOut(Location::RequiresRegister());
1650 }
1651 break;
1652 }
1653 default:
1654 locations->SetInAt(0, Location::RequiresRegister());
1655 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001656 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001657 // We need a byte register.
1658 locations->SetOut(Location::RegisterLocation(ECX));
1659 }
1660 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001661 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001662}
1663
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001664void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001665 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001666 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001667 }
Mark Mendellc4701932015-04-10 13:18:51 -04001668
1669 LocationSummary* locations = cond->GetLocations();
1670 Location lhs = locations->InAt(0);
1671 Location rhs = locations->InAt(1);
1672 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001673 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001674
1675 switch (cond->InputAt(0)->GetType()) {
1676 default: {
1677 // Integer case.
1678
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001679 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001680 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001681 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001682 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001683 return;
1684 }
1685 case Primitive::kPrimLong:
1686 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1687 break;
1688 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001689 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001690 GenerateFPJumps(cond, &true_label, &false_label);
1691 break;
1692 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001693 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001694 GenerateFPJumps(cond, &true_label, &false_label);
1695 break;
1696 }
1697
1698 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001699 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001700
Roland Levillain4fa13f62015-07-06 18:11:54 +01001701 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001702 __ Bind(&false_label);
1703 __ xorl(reg, reg);
1704 __ jmp(&done_label);
1705
Roland Levillain4fa13f62015-07-06 18:11:54 +01001706 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001707 __ Bind(&true_label);
1708 __ movl(reg, Immediate(1));
1709 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001710}
1711
1712void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001713 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001714}
1715
1716void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001717 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001718}
1719
1720void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001721 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001722}
1723
1724void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001725 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001726}
1727
1728void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001729 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001730}
1731
1732void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001733 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001734}
1735
1736void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001737 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001738}
1739
1740void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001741 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001742}
1743
1744void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001745 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001746}
1747
1748void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001749 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001750}
1751
1752void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001753 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001754}
1755
1756void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001757 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001758}
1759
Aart Bike9f37602015-10-09 11:15:55 -07001760void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001761 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001762}
1763
1764void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001765 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001766}
1767
1768void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001769 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001770}
1771
1772void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001774}
1775
1776void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001778}
1779
1780void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001782}
1783
1784void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001786}
1787
1788void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001790}
1791
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001792void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001793 LocationSummary* locations =
1794 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001795 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001796}
1797
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001798void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001799 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001800}
1801
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001802void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1803 LocationSummary* locations =
1804 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1805 locations->SetOut(Location::ConstantLocation(constant));
1806}
1807
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001808void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001809 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001810}
1811
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001812void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001813 LocationSummary* locations =
1814 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001815 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001816}
1817
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001818void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001819 // Will be generated at use site.
1820}
1821
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001822void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1823 LocationSummary* locations =
1824 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1825 locations->SetOut(Location::ConstantLocation(constant));
1826}
1827
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001828void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001829 // Will be generated at use site.
1830}
1831
1832void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1833 LocationSummary* locations =
1834 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1835 locations->SetOut(Location::ConstantLocation(constant));
1836}
1837
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001838void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001839 // Will be generated at use site.
1840}
1841
Calin Juravle27df7582015-04-17 19:12:31 +01001842void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1843 memory_barrier->SetLocations(nullptr);
1844}
1845
1846void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001847 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001848}
1849
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001850void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001851 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001852}
1853
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001854void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001855 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001856}
1857
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001858void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001859 LocationSummary* locations =
1860 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001861 switch (ret->InputAt(0)->GetType()) {
1862 case Primitive::kPrimBoolean:
1863 case Primitive::kPrimByte:
1864 case Primitive::kPrimChar:
1865 case Primitive::kPrimShort:
1866 case Primitive::kPrimInt:
1867 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001868 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869 break;
1870
1871 case Primitive::kPrimLong:
1872 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001873 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001874 break;
1875
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001876 case Primitive::kPrimFloat:
1877 case Primitive::kPrimDouble:
1878 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001879 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001880 break;
1881
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001882 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001883 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001884 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001885}
1886
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001887void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001888 if (kIsDebugBuild) {
1889 switch (ret->InputAt(0)->GetType()) {
1890 case Primitive::kPrimBoolean:
1891 case Primitive::kPrimByte:
1892 case Primitive::kPrimChar:
1893 case Primitive::kPrimShort:
1894 case Primitive::kPrimInt:
1895 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001896 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001897 break;
1898
1899 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001900 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1901 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001902 break;
1903
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001904 case Primitive::kPrimFloat:
1905 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001906 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001907 break;
1908
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001909 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001910 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001911 }
1912 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001913 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001914}
1915
Calin Juravle175dc732015-08-25 15:42:32 +01001916void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1917 // The trampoline uses the same calling convention as dex calling conventions,
1918 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1919 // the method_idx.
1920 HandleInvoke(invoke);
1921}
1922
1923void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1924 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1925}
1926
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001927void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001928 // Explicit clinit checks triggered by static invokes must have been pruned by
1929 // art::PrepareForRegisterAllocation.
1930 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001931
Mark Mendellfb8d2792015-03-31 22:16:59 -04001932 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001933 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001934 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001935 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001936 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001937 return;
1938 }
1939
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001940 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001941
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001942 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1943 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001944 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001945 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001946}
1947
Mark Mendell09ed1a32015-03-25 08:30:06 -04001948static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1949 if (invoke->GetLocations()->Intrinsified()) {
1950 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1951 intrinsic.Dispatch(invoke);
1952 return true;
1953 }
1954 return false;
1955}
1956
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001957void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001958 // Explicit clinit checks triggered by static invokes must have been pruned by
1959 // art::PrepareForRegisterAllocation.
1960 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001961
Mark Mendell09ed1a32015-03-25 08:30:06 -04001962 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1963 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001964 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001965
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001966 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001967 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001968 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001969 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001970}
1971
1972void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001973 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1974 if (intrinsic.TryDispatch(invoke)) {
1975 return;
1976 }
1977
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001978 HandleInvoke(invoke);
1979}
1980
1981void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001982 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001983 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001984}
1985
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001986void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001987 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1988 return;
1989 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001990
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001991 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001992 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001993 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001994}
1995
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001996void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001997 // This call to HandleInvoke allocates a temporary (core) register
1998 // which is also used to transfer the hidden argument from FP to
1999 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002000 HandleInvoke(invoke);
2001 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002002 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002003}
2004
2005void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2006 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002007 LocationSummary* locations = invoke->GetLocations();
2008 Register temp = locations->GetTemp(0).AsRegister<Register>();
2009 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002010 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2011 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002012 Location receiver = locations->InAt(0);
2013 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2014
Roland Levillain0d5a2812015-11-13 10:07:31 +00002015 // Set the hidden argument. This is safe to do this here, as XMM7
2016 // won't be modified thereafter, before the `call` instruction.
2017 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002018 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002019 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002020
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002021 if (receiver.IsStackSlot()) {
2022 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002023 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002024 __ movl(temp, Address(temp, class_offset));
2025 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002026 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002027 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002028 }
Roland Levillain4d027112015-07-01 15:41:14 +01002029 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002030 // Instead of simply (possibly) unpoisoning `temp` here, we should
2031 // emit a read barrier for the previous class reference load.
2032 // However this is not required in practice, as this is an
2033 // intermediate/temporary reference and because the current
2034 // concurrent copying collector keeps the from-space memory
2035 // intact/accessible until the end of the marking phase (the
2036 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002037 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002038 // temp = temp->GetImtEntryAt(method_offset);
2039 __ movl(temp, Address(temp, method_offset));
2040 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002041 __ call(Address(temp,
2042 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002043
2044 DCHECK(!codegen_->IsLeafMethod());
2045 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2046}
2047
Roland Levillain88cb1752014-10-20 16:36:47 +01002048void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2049 LocationSummary* locations =
2050 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2051 switch (neg->GetResultType()) {
2052 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002053 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002054 locations->SetInAt(0, Location::RequiresRegister());
2055 locations->SetOut(Location::SameAsFirstInput());
2056 break;
2057
Roland Levillain88cb1752014-10-20 16:36:47 +01002058 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002059 locations->SetInAt(0, Location::RequiresFpuRegister());
2060 locations->SetOut(Location::SameAsFirstInput());
2061 locations->AddTemp(Location::RequiresRegister());
2062 locations->AddTemp(Location::RequiresFpuRegister());
2063 break;
2064
Roland Levillain88cb1752014-10-20 16:36:47 +01002065 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002066 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002067 locations->SetOut(Location::SameAsFirstInput());
2068 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002069 break;
2070
2071 default:
2072 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2073 }
2074}
2075
2076void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2077 LocationSummary* locations = neg->GetLocations();
2078 Location out = locations->Out();
2079 Location in = locations->InAt(0);
2080 switch (neg->GetResultType()) {
2081 case Primitive::kPrimInt:
2082 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002083 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002084 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002085 break;
2086
2087 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002088 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002089 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002090 __ negl(out.AsRegisterPairLow<Register>());
2091 // Negation is similar to subtraction from zero. The least
2092 // significant byte triggers a borrow when it is different from
2093 // zero; to take it into account, add 1 to the most significant
2094 // byte if the carry flag (CF) is set to 1 after the first NEGL
2095 // operation.
2096 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2097 __ negl(out.AsRegisterPairHigh<Register>());
2098 break;
2099
Roland Levillain5368c212014-11-27 15:03:41 +00002100 case Primitive::kPrimFloat: {
2101 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002102 Register constant = locations->GetTemp(0).AsRegister<Register>();
2103 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002104 // Implement float negation with an exclusive or with value
2105 // 0x80000000 (mask for bit 31, representing the sign of a
2106 // single-precision floating-point number).
2107 __ movl(constant, Immediate(INT32_C(0x80000000)));
2108 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002109 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002110 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002111 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002112
Roland Levillain5368c212014-11-27 15:03:41 +00002113 case Primitive::kPrimDouble: {
2114 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002115 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002116 // Implement double negation with an exclusive or with value
2117 // 0x8000000000000000 (mask for bit 63, representing the sign of
2118 // a double-precision floating-point number).
2119 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002120 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002121 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002122 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002123
2124 default:
2125 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2126 }
2127}
2128
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002129void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2130 LocationSummary* locations =
2131 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2132 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2133 locations->SetInAt(0, Location::RequiresFpuRegister());
2134 locations->SetInAt(1, Location::RequiresRegister());
2135 locations->SetOut(Location::SameAsFirstInput());
2136 locations->AddTemp(Location::RequiresFpuRegister());
2137}
2138
2139void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2140 LocationSummary* locations = neg->GetLocations();
2141 Location out = locations->Out();
2142 DCHECK(locations->InAt(0).Equals(out));
2143
2144 Register constant_area = locations->InAt(1).AsRegister<Register>();
2145 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2146 if (neg->GetType() == Primitive::kPrimFloat) {
2147 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2148 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2149 } else {
2150 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2151 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2152 }
2153}
2154
Roland Levillaindff1f282014-11-05 14:15:05 +00002155void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002156 Primitive::Type result_type = conversion->GetResultType();
2157 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002158 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002159
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002160 // The float-to-long and double-to-long type conversions rely on a
2161 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002162 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002163 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2164 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002165 ? LocationSummary::kCall
2166 : LocationSummary::kNoCall;
2167 LocationSummary* locations =
2168 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2169
David Brazdilb2bd1c52015-03-25 11:17:37 +00002170 // The Java language does not allow treating boolean as an integral type but
2171 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002172
Roland Levillaindff1f282014-11-05 14:15:05 +00002173 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002174 case Primitive::kPrimByte:
2175 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002176 case Primitive::kPrimLong: {
2177 // Type conversion from long to byte is a result of code transformations.
2178 HInstruction* input = conversion->InputAt(0);
2179 Location input_location = input->IsConstant()
2180 ? Location::ConstantLocation(input->AsConstant())
2181 : Location::RegisterPairLocation(EAX, EDX);
2182 locations->SetInAt(0, input_location);
2183 // Make the output overlap to please the register allocator. This greatly simplifies
2184 // the validation of the linear scan implementation
2185 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2186 break;
2187 }
David Brazdil46e2a392015-03-16 17:31:52 +00002188 case Primitive::kPrimBoolean:
2189 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002190 case Primitive::kPrimShort:
2191 case Primitive::kPrimInt:
2192 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002193 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002194 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2195 // Make the output overlap to please the register allocator. This greatly simplifies
2196 // the validation of the linear scan implementation
2197 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002198 break;
2199
2200 default:
2201 LOG(FATAL) << "Unexpected type conversion from " << input_type
2202 << " to " << result_type;
2203 }
2204 break;
2205
Roland Levillain01a8d712014-11-14 16:27:39 +00002206 case Primitive::kPrimShort:
2207 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002208 case Primitive::kPrimLong:
2209 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002210 case Primitive::kPrimBoolean:
2211 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002212 case Primitive::kPrimByte:
2213 case Primitive::kPrimInt:
2214 case Primitive::kPrimChar:
2215 // Processing a Dex `int-to-short' instruction.
2216 locations->SetInAt(0, Location::Any());
2217 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2218 break;
2219
2220 default:
2221 LOG(FATAL) << "Unexpected type conversion from " << input_type
2222 << " to " << result_type;
2223 }
2224 break;
2225
Roland Levillain946e1432014-11-11 17:35:19 +00002226 case Primitive::kPrimInt:
2227 switch (input_type) {
2228 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002229 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002230 locations->SetInAt(0, Location::Any());
2231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2232 break;
2233
2234 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002235 // Processing a Dex `float-to-int' instruction.
2236 locations->SetInAt(0, Location::RequiresFpuRegister());
2237 locations->SetOut(Location::RequiresRegister());
2238 locations->AddTemp(Location::RequiresFpuRegister());
2239 break;
2240
Roland Levillain946e1432014-11-11 17:35:19 +00002241 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002242 // Processing a Dex `double-to-int' instruction.
2243 locations->SetInAt(0, Location::RequiresFpuRegister());
2244 locations->SetOut(Location::RequiresRegister());
2245 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002246 break;
2247
2248 default:
2249 LOG(FATAL) << "Unexpected type conversion from " << input_type
2250 << " to " << result_type;
2251 }
2252 break;
2253
Roland Levillaindff1f282014-11-05 14:15:05 +00002254 case Primitive::kPrimLong:
2255 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002256 case Primitive::kPrimBoolean:
2257 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002258 case Primitive::kPrimByte:
2259 case Primitive::kPrimShort:
2260 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002261 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002262 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002263 locations->SetInAt(0, Location::RegisterLocation(EAX));
2264 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2265 break;
2266
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002267 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002268 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002269 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002270 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002271 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2272 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2273
Vladimir Marko949c91f2015-01-27 10:48:44 +00002274 // The runtime helper puts the result in EAX, EDX.
2275 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002276 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002277 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002278
2279 default:
2280 LOG(FATAL) << "Unexpected type conversion from " << input_type
2281 << " to " << result_type;
2282 }
2283 break;
2284
Roland Levillain981e4542014-11-14 11:47:14 +00002285 case Primitive::kPrimChar:
2286 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002287 case Primitive::kPrimLong:
2288 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002289 case Primitive::kPrimBoolean:
2290 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002291 case Primitive::kPrimByte:
2292 case Primitive::kPrimShort:
2293 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002294 // Processing a Dex `int-to-char' instruction.
2295 locations->SetInAt(0, Location::Any());
2296 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2297 break;
2298
2299 default:
2300 LOG(FATAL) << "Unexpected type conversion from " << input_type
2301 << " to " << result_type;
2302 }
2303 break;
2304
Roland Levillaindff1f282014-11-05 14:15:05 +00002305 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002306 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002307 case Primitive::kPrimBoolean:
2308 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002309 case Primitive::kPrimByte:
2310 case Primitive::kPrimShort:
2311 case Primitive::kPrimInt:
2312 case Primitive::kPrimChar:
2313 // Processing a Dex `int-to-float' instruction.
2314 locations->SetInAt(0, Location::RequiresRegister());
2315 locations->SetOut(Location::RequiresFpuRegister());
2316 break;
2317
2318 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002319 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002320 locations->SetInAt(0, Location::Any());
2321 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002322 break;
2323
Roland Levillaincff13742014-11-17 14:32:17 +00002324 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002325 // Processing a Dex `double-to-float' instruction.
2326 locations->SetInAt(0, Location::RequiresFpuRegister());
2327 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002328 break;
2329
2330 default:
2331 LOG(FATAL) << "Unexpected type conversion from " << input_type
2332 << " to " << result_type;
2333 };
2334 break;
2335
Roland Levillaindff1f282014-11-05 14:15:05 +00002336 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002337 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002338 case Primitive::kPrimBoolean:
2339 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002340 case Primitive::kPrimByte:
2341 case Primitive::kPrimShort:
2342 case Primitive::kPrimInt:
2343 case Primitive::kPrimChar:
2344 // Processing a Dex `int-to-double' instruction.
2345 locations->SetInAt(0, Location::RequiresRegister());
2346 locations->SetOut(Location::RequiresFpuRegister());
2347 break;
2348
2349 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002350 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002351 locations->SetInAt(0, Location::Any());
2352 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002353 break;
2354
Roland Levillaincff13742014-11-17 14:32:17 +00002355 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002356 // Processing a Dex `float-to-double' instruction.
2357 locations->SetInAt(0, Location::RequiresFpuRegister());
2358 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002359 break;
2360
2361 default:
2362 LOG(FATAL) << "Unexpected type conversion from " << input_type
2363 << " to " << result_type;
2364 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002365 break;
2366
2367 default:
2368 LOG(FATAL) << "Unexpected type conversion from " << input_type
2369 << " to " << result_type;
2370 }
2371}
2372
2373void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2374 LocationSummary* locations = conversion->GetLocations();
2375 Location out = locations->Out();
2376 Location in = locations->InAt(0);
2377 Primitive::Type result_type = conversion->GetResultType();
2378 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002379 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002380 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002381 case Primitive::kPrimByte:
2382 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002383 case Primitive::kPrimLong:
2384 // Type conversion from long to byte is a result of code transformations.
2385 if (in.IsRegisterPair()) {
2386 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2387 } else {
2388 DCHECK(in.GetConstant()->IsLongConstant());
2389 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2390 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2391 }
2392 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002393 case Primitive::kPrimBoolean:
2394 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002395 case Primitive::kPrimShort:
2396 case Primitive::kPrimInt:
2397 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002398 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002399 if (in.IsRegister()) {
2400 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002401 } else {
2402 DCHECK(in.GetConstant()->IsIntConstant());
2403 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2404 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2405 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002406 break;
2407
2408 default:
2409 LOG(FATAL) << "Unexpected type conversion from " << input_type
2410 << " to " << result_type;
2411 }
2412 break;
2413
Roland Levillain01a8d712014-11-14 16:27:39 +00002414 case Primitive::kPrimShort:
2415 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002416 case Primitive::kPrimLong:
2417 // Type conversion from long to short is a result of code transformations.
2418 if (in.IsRegisterPair()) {
2419 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2420 } else if (in.IsDoubleStackSlot()) {
2421 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2422 } else {
2423 DCHECK(in.GetConstant()->IsLongConstant());
2424 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2425 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2426 }
2427 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002428 case Primitive::kPrimBoolean:
2429 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002430 case Primitive::kPrimByte:
2431 case Primitive::kPrimInt:
2432 case Primitive::kPrimChar:
2433 // Processing a Dex `int-to-short' instruction.
2434 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002435 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002436 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002437 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002438 } else {
2439 DCHECK(in.GetConstant()->IsIntConstant());
2440 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002441 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002442 }
2443 break;
2444
2445 default:
2446 LOG(FATAL) << "Unexpected type conversion from " << input_type
2447 << " to " << result_type;
2448 }
2449 break;
2450
Roland Levillain946e1432014-11-11 17:35:19 +00002451 case Primitive::kPrimInt:
2452 switch (input_type) {
2453 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002454 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002455 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002456 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002457 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002458 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002459 } else {
2460 DCHECK(in.IsConstant());
2461 DCHECK(in.GetConstant()->IsLongConstant());
2462 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002463 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002464 }
2465 break;
2466
Roland Levillain3f8f9362014-12-02 17:45:01 +00002467 case Primitive::kPrimFloat: {
2468 // Processing a Dex `float-to-int' instruction.
2469 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2470 Register output = out.AsRegister<Register>();
2471 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002472 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002473
2474 __ movl(output, Immediate(kPrimIntMax));
2475 // temp = int-to-float(output)
2476 __ cvtsi2ss(temp, output);
2477 // if input >= temp goto done
2478 __ comiss(input, temp);
2479 __ j(kAboveEqual, &done);
2480 // if input == NaN goto nan
2481 __ j(kUnordered, &nan);
2482 // output = float-to-int-truncate(input)
2483 __ cvttss2si(output, input);
2484 __ jmp(&done);
2485 __ Bind(&nan);
2486 // output = 0
2487 __ xorl(output, output);
2488 __ Bind(&done);
2489 break;
2490 }
2491
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002492 case Primitive::kPrimDouble: {
2493 // Processing a Dex `double-to-int' instruction.
2494 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2495 Register output = out.AsRegister<Register>();
2496 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002497 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002498
2499 __ movl(output, Immediate(kPrimIntMax));
2500 // temp = int-to-double(output)
2501 __ cvtsi2sd(temp, output);
2502 // if input >= temp goto done
2503 __ comisd(input, temp);
2504 __ j(kAboveEqual, &done);
2505 // if input == NaN goto nan
2506 __ j(kUnordered, &nan);
2507 // output = double-to-int-truncate(input)
2508 __ cvttsd2si(output, input);
2509 __ jmp(&done);
2510 __ Bind(&nan);
2511 // output = 0
2512 __ xorl(output, output);
2513 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002514 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002515 }
Roland Levillain946e1432014-11-11 17:35:19 +00002516
2517 default:
2518 LOG(FATAL) << "Unexpected type conversion from " << input_type
2519 << " to " << result_type;
2520 }
2521 break;
2522
Roland Levillaindff1f282014-11-05 14:15:05 +00002523 case Primitive::kPrimLong:
2524 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002525 case Primitive::kPrimBoolean:
2526 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002527 case Primitive::kPrimByte:
2528 case Primitive::kPrimShort:
2529 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002530 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002531 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002532 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2533 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002534 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002535 __ cdq();
2536 break;
2537
2538 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002539 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002540 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2541 conversion,
2542 conversion->GetDexPc(),
2543 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002544 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002545 break;
2546
Roland Levillaindff1f282014-11-05 14:15:05 +00002547 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002548 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002549 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2550 conversion,
2551 conversion->GetDexPc(),
2552 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002553 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002554 break;
2555
2556 default:
2557 LOG(FATAL) << "Unexpected type conversion from " << input_type
2558 << " to " << result_type;
2559 }
2560 break;
2561
Roland Levillain981e4542014-11-14 11:47:14 +00002562 case Primitive::kPrimChar:
2563 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002564 case Primitive::kPrimLong:
2565 // Type conversion from long to short is a result of code transformations.
2566 if (in.IsRegisterPair()) {
2567 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2568 } else if (in.IsDoubleStackSlot()) {
2569 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2570 } else {
2571 DCHECK(in.GetConstant()->IsLongConstant());
2572 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2573 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2574 }
2575 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002576 case Primitive::kPrimBoolean:
2577 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002578 case Primitive::kPrimByte:
2579 case Primitive::kPrimShort:
2580 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002581 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2582 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002583 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002584 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002585 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002586 } else {
2587 DCHECK(in.GetConstant()->IsIntConstant());
2588 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002589 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002590 }
2591 break;
2592
2593 default:
2594 LOG(FATAL) << "Unexpected type conversion from " << input_type
2595 << " to " << result_type;
2596 }
2597 break;
2598
Roland Levillaindff1f282014-11-05 14:15:05 +00002599 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002600 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002601 case Primitive::kPrimBoolean:
2602 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002603 case Primitive::kPrimByte:
2604 case Primitive::kPrimShort:
2605 case Primitive::kPrimInt:
2606 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002607 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002608 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002609 break;
2610
Roland Levillain6d0e4832014-11-27 18:31:21 +00002611 case Primitive::kPrimLong: {
2612 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002613 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002614
Roland Levillain232ade02015-04-20 15:14:36 +01002615 // Create stack space for the call to
2616 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2617 // TODO: enhance register allocator to ask for stack temporaries.
2618 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2619 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2620 __ subl(ESP, Immediate(adjustment));
2621 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002622
Roland Levillain232ade02015-04-20 15:14:36 +01002623 // Load the value to the FP stack, using temporaries if needed.
2624 PushOntoFPStack(in, 0, adjustment, false, true);
2625
2626 if (out.IsStackSlot()) {
2627 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2628 } else {
2629 __ fstps(Address(ESP, 0));
2630 Location stack_temp = Location::StackSlot(0);
2631 codegen_->Move32(out, stack_temp);
2632 }
2633
2634 // Remove the temporary stack space we allocated.
2635 if (adjustment != 0) {
2636 __ addl(ESP, Immediate(adjustment));
2637 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002638 break;
2639 }
2640
Roland Levillaincff13742014-11-17 14:32:17 +00002641 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002642 // Processing a Dex `double-to-float' instruction.
2643 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002644 break;
2645
2646 default:
2647 LOG(FATAL) << "Unexpected type conversion from " << input_type
2648 << " to " << result_type;
2649 };
2650 break;
2651
Roland Levillaindff1f282014-11-05 14:15:05 +00002652 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002653 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002654 case Primitive::kPrimBoolean:
2655 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002656 case Primitive::kPrimByte:
2657 case Primitive::kPrimShort:
2658 case Primitive::kPrimInt:
2659 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002660 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002661 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002662 break;
2663
Roland Levillain647b9ed2014-11-27 12:06:00 +00002664 case Primitive::kPrimLong: {
2665 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002666 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002667
Roland Levillain232ade02015-04-20 15:14:36 +01002668 // Create stack space for the call to
2669 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2670 // TODO: enhance register allocator to ask for stack temporaries.
2671 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2672 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2673 __ subl(ESP, Immediate(adjustment));
2674 }
2675
2676 // Load the value to the FP stack, using temporaries if needed.
2677 PushOntoFPStack(in, 0, adjustment, false, true);
2678
2679 if (out.IsDoubleStackSlot()) {
2680 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2681 } else {
2682 __ fstpl(Address(ESP, 0));
2683 Location stack_temp = Location::DoubleStackSlot(0);
2684 codegen_->Move64(out, stack_temp);
2685 }
2686
2687 // Remove the temporary stack space we allocated.
2688 if (adjustment != 0) {
2689 __ addl(ESP, Immediate(adjustment));
2690 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002691 break;
2692 }
2693
Roland Levillaincff13742014-11-17 14:32:17 +00002694 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002695 // Processing a Dex `float-to-double' instruction.
2696 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002697 break;
2698
2699 default:
2700 LOG(FATAL) << "Unexpected type conversion from " << input_type
2701 << " to " << result_type;
2702 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002703 break;
2704
2705 default:
2706 LOG(FATAL) << "Unexpected type conversion from " << input_type
2707 << " to " << result_type;
2708 }
2709}
2710
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002711void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002712 LocationSummary* locations =
2713 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002714 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002715 case Primitive::kPrimInt: {
2716 locations->SetInAt(0, Location::RequiresRegister());
2717 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2718 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2719 break;
2720 }
2721
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002722 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002723 locations->SetInAt(0, Location::RequiresRegister());
2724 locations->SetInAt(1, Location::Any());
2725 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002726 break;
2727 }
2728
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002729 case Primitive::kPrimFloat:
2730 case Primitive::kPrimDouble: {
2731 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002732 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2733 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002734 } else if (add->InputAt(1)->IsConstant()) {
2735 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002736 } else {
2737 locations->SetInAt(1, Location::Any());
2738 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002739 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002740 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002741 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002742
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002743 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002744 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2745 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002746 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002747}
2748
2749void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2750 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002751 Location first = locations->InAt(0);
2752 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002753 Location out = locations->Out();
2754
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002755 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002756 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002757 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002758 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2759 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002760 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2761 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002762 } else {
2763 __ leal(out.AsRegister<Register>(), Address(
2764 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2765 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002766 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002767 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2768 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2769 __ addl(out.AsRegister<Register>(), Immediate(value));
2770 } else {
2771 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2772 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002773 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002774 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002775 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002776 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002777 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002778 }
2779
2780 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002781 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002782 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2783 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002784 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002785 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2786 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002787 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002788 } else {
2789 DCHECK(second.IsConstant()) << second;
2790 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2791 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2792 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002793 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002794 break;
2795 }
2796
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002797 case Primitive::kPrimFloat: {
2798 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002799 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002800 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2801 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002802 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002803 __ addss(first.AsFpuRegister<XmmRegister>(),
2804 codegen_->LiteralFloatAddress(
2805 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2806 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2807 } else {
2808 DCHECK(second.IsStackSlot());
2809 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002810 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002811 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002812 }
2813
2814 case Primitive::kPrimDouble: {
2815 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002816 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002817 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2818 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002819 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002820 __ addsd(first.AsFpuRegister<XmmRegister>(),
2821 codegen_->LiteralDoubleAddress(
2822 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2823 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2824 } else {
2825 DCHECK(second.IsDoubleStackSlot());
2826 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002827 }
2828 break;
2829 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002830
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002831 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002832 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002833 }
2834}
2835
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002836void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002837 LocationSummary* locations =
2838 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002839 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002840 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002841 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002842 locations->SetInAt(0, Location::RequiresRegister());
2843 locations->SetInAt(1, Location::Any());
2844 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002845 break;
2846 }
Calin Juravle11351682014-10-23 15:38:15 +01002847 case Primitive::kPrimFloat:
2848 case Primitive::kPrimDouble: {
2849 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002850 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2851 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002852 } else if (sub->InputAt(1)->IsConstant()) {
2853 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002854 } else {
2855 locations->SetInAt(1, Location::Any());
2856 }
Calin Juravle11351682014-10-23 15:38:15 +01002857 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002858 break;
Calin Juravle11351682014-10-23 15:38:15 +01002859 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002860
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002861 default:
Calin Juravle11351682014-10-23 15:38:15 +01002862 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002863 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002864}
2865
2866void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2867 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002868 Location first = locations->InAt(0);
2869 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002870 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002871 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002872 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002873 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002874 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002875 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002876 __ subl(first.AsRegister<Register>(),
2877 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002878 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002879 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002880 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002881 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002882 }
2883
2884 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002885 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002886 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2887 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002888 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002889 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002890 __ sbbl(first.AsRegisterPairHigh<Register>(),
2891 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002892 } else {
2893 DCHECK(second.IsConstant()) << second;
2894 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2895 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2896 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002897 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002898 break;
2899 }
2900
Calin Juravle11351682014-10-23 15:38:15 +01002901 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002902 if (second.IsFpuRegister()) {
2903 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2904 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2905 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002906 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002907 __ subss(first.AsFpuRegister<XmmRegister>(),
2908 codegen_->LiteralFloatAddress(
2909 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2910 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2911 } else {
2912 DCHECK(second.IsStackSlot());
2913 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2914 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002915 break;
Calin Juravle11351682014-10-23 15:38:15 +01002916 }
2917
2918 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002919 if (second.IsFpuRegister()) {
2920 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2921 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2922 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002923 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002924 __ subsd(first.AsFpuRegister<XmmRegister>(),
2925 codegen_->LiteralDoubleAddress(
2926 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2927 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2928 } else {
2929 DCHECK(second.IsDoubleStackSlot());
2930 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2931 }
Calin Juravle11351682014-10-23 15:38:15 +01002932 break;
2933 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002934
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002935 default:
Calin Juravle11351682014-10-23 15:38:15 +01002936 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002937 }
2938}
2939
Calin Juravle34bacdf2014-10-07 20:23:36 +01002940void LocationsBuilderX86::VisitMul(HMul* mul) {
2941 LocationSummary* locations =
2942 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2943 switch (mul->GetResultType()) {
2944 case Primitive::kPrimInt:
2945 locations->SetInAt(0, Location::RequiresRegister());
2946 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002947 if (mul->InputAt(1)->IsIntConstant()) {
2948 // Can use 3 operand multiply.
2949 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2950 } else {
2951 locations->SetOut(Location::SameAsFirstInput());
2952 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002953 break;
2954 case Primitive::kPrimLong: {
2955 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002956 locations->SetInAt(1, Location::Any());
2957 locations->SetOut(Location::SameAsFirstInput());
2958 // Needed for imul on 32bits with 64bits output.
2959 locations->AddTemp(Location::RegisterLocation(EAX));
2960 locations->AddTemp(Location::RegisterLocation(EDX));
2961 break;
2962 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002963 case Primitive::kPrimFloat:
2964 case Primitive::kPrimDouble: {
2965 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002966 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2967 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002968 } else if (mul->InputAt(1)->IsConstant()) {
2969 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002970 } else {
2971 locations->SetInAt(1, Location::Any());
2972 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002973 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002974 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002975 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002976
2977 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002978 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002979 }
2980}
2981
2982void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2983 LocationSummary* locations = mul->GetLocations();
2984 Location first = locations->InAt(0);
2985 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002986 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002987
2988 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002989 case Primitive::kPrimInt:
2990 // The constant may have ended up in a register, so test explicitly to avoid
2991 // problems where the output may not be the same as the first operand.
2992 if (mul->InputAt(1)->IsIntConstant()) {
2993 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2994 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2995 } else if (second.IsRegister()) {
2996 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002997 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002998 } else {
2999 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003000 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003001 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003002 }
3003 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003004
3005 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003006 Register in1_hi = first.AsRegisterPairHigh<Register>();
3007 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003008 Register eax = locations->GetTemp(0).AsRegister<Register>();
3009 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003010
3011 DCHECK_EQ(EAX, eax);
3012 DCHECK_EQ(EDX, edx);
3013
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003014 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003015 // output: in1
3016 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3017 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3018 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003019 if (second.IsConstant()) {
3020 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003021
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003022 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3023 int32_t low_value = Low32Bits(value);
3024 int32_t high_value = High32Bits(value);
3025 Immediate low(low_value);
3026 Immediate high(high_value);
3027
3028 __ movl(eax, high);
3029 // eax <- in1.lo * in2.hi
3030 __ imull(eax, in1_lo);
3031 // in1.hi <- in1.hi * in2.lo
3032 __ imull(in1_hi, low);
3033 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3034 __ addl(in1_hi, eax);
3035 // move in2_lo to eax to prepare for double precision
3036 __ movl(eax, low);
3037 // edx:eax <- in1.lo * in2.lo
3038 __ mull(in1_lo);
3039 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3040 __ addl(in1_hi, edx);
3041 // in1.lo <- (in1.lo * in2.lo)[31:0];
3042 __ movl(in1_lo, eax);
3043 } else if (second.IsRegisterPair()) {
3044 Register in2_hi = second.AsRegisterPairHigh<Register>();
3045 Register in2_lo = second.AsRegisterPairLow<Register>();
3046
3047 __ movl(eax, in2_hi);
3048 // eax <- in1.lo * in2.hi
3049 __ imull(eax, in1_lo);
3050 // in1.hi <- in1.hi * in2.lo
3051 __ imull(in1_hi, in2_lo);
3052 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3053 __ addl(in1_hi, eax);
3054 // move in1_lo to eax to prepare for double precision
3055 __ movl(eax, in1_lo);
3056 // edx:eax <- in1.lo * in2.lo
3057 __ mull(in2_lo);
3058 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3059 __ addl(in1_hi, edx);
3060 // in1.lo <- (in1.lo * in2.lo)[31:0];
3061 __ movl(in1_lo, eax);
3062 } else {
3063 DCHECK(second.IsDoubleStackSlot()) << second;
3064 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3065 Address in2_lo(ESP, second.GetStackIndex());
3066
3067 __ movl(eax, in2_hi);
3068 // eax <- in1.lo * in2.hi
3069 __ imull(eax, in1_lo);
3070 // in1.hi <- in1.hi * in2.lo
3071 __ imull(in1_hi, in2_lo);
3072 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3073 __ addl(in1_hi, eax);
3074 // move in1_lo to eax to prepare for double precision
3075 __ movl(eax, in1_lo);
3076 // edx:eax <- in1.lo * in2.lo
3077 __ mull(in2_lo);
3078 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3079 __ addl(in1_hi, edx);
3080 // in1.lo <- (in1.lo * in2.lo)[31:0];
3081 __ movl(in1_lo, eax);
3082 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003083
3084 break;
3085 }
3086
Calin Juravleb5bfa962014-10-21 18:02:24 +01003087 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003088 DCHECK(first.Equals(locations->Out()));
3089 if (second.IsFpuRegister()) {
3090 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3091 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3092 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003093 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003094 __ mulss(first.AsFpuRegister<XmmRegister>(),
3095 codegen_->LiteralFloatAddress(
3096 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3097 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3098 } else {
3099 DCHECK(second.IsStackSlot());
3100 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3101 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003102 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003103 }
3104
3105 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003106 DCHECK(first.Equals(locations->Out()));
3107 if (second.IsFpuRegister()) {
3108 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3109 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3110 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003111 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003112 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3113 codegen_->LiteralDoubleAddress(
3114 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3115 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3116 } else {
3117 DCHECK(second.IsDoubleStackSlot());
3118 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3119 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003120 break;
3121 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003122
3123 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003124 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003125 }
3126}
3127
Roland Levillain232ade02015-04-20 15:14:36 +01003128void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3129 uint32_t temp_offset,
3130 uint32_t stack_adjustment,
3131 bool is_fp,
3132 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003133 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003134 DCHECK(!is_wide);
3135 if (is_fp) {
3136 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3137 } else {
3138 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3139 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003140 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003141 DCHECK(is_wide);
3142 if (is_fp) {
3143 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3144 } else {
3145 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3146 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003147 } else {
3148 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003149 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003150 Location stack_temp = Location::StackSlot(temp_offset);
3151 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003152 if (is_fp) {
3153 __ flds(Address(ESP, temp_offset));
3154 } else {
3155 __ filds(Address(ESP, temp_offset));
3156 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003157 } else {
3158 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3159 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003160 if (is_fp) {
3161 __ fldl(Address(ESP, temp_offset));
3162 } else {
3163 __ fildl(Address(ESP, temp_offset));
3164 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003165 }
3166 }
3167}
3168
3169void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3170 Primitive::Type type = rem->GetResultType();
3171 bool is_float = type == Primitive::kPrimFloat;
3172 size_t elem_size = Primitive::ComponentSize(type);
3173 LocationSummary* locations = rem->GetLocations();
3174 Location first = locations->InAt(0);
3175 Location second = locations->InAt(1);
3176 Location out = locations->Out();
3177
3178 // Create stack space for 2 elements.
3179 // TODO: enhance register allocator to ask for stack temporaries.
3180 __ subl(ESP, Immediate(2 * elem_size));
3181
3182 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003183 const bool is_wide = !is_float;
3184 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3185 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003186
3187 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003188 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003189 __ Bind(&retry);
3190 __ fprem();
3191
3192 // Move FP status to AX.
3193 __ fstsw();
3194
3195 // And see if the argument reduction is complete. This is signaled by the
3196 // C2 FPU flag bit set to 0.
3197 __ andl(EAX, Immediate(kC2ConditionMask));
3198 __ j(kNotEqual, &retry);
3199
3200 // We have settled on the final value. Retrieve it into an XMM register.
3201 // Store FP top of stack to real stack.
3202 if (is_float) {
3203 __ fsts(Address(ESP, 0));
3204 } else {
3205 __ fstl(Address(ESP, 0));
3206 }
3207
3208 // Pop the 2 items from the FP stack.
3209 __ fucompp();
3210
3211 // Load the value from the stack into an XMM register.
3212 DCHECK(out.IsFpuRegister()) << out;
3213 if (is_float) {
3214 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3215 } else {
3216 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3217 }
3218
3219 // And remove the temporary stack space we allocated.
3220 __ addl(ESP, Immediate(2 * elem_size));
3221}
3222
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003223
3224void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3225 DCHECK(instruction->IsDiv() || instruction->IsRem());
3226
3227 LocationSummary* locations = instruction->GetLocations();
3228 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003229 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003230
3231 Register out_register = locations->Out().AsRegister<Register>();
3232 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003233 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003234
3235 DCHECK(imm == 1 || imm == -1);
3236
3237 if (instruction->IsRem()) {
3238 __ xorl(out_register, out_register);
3239 } else {
3240 __ movl(out_register, input_register);
3241 if (imm == -1) {
3242 __ negl(out_register);
3243 }
3244 }
3245}
3246
3247
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003248void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003249 LocationSummary* locations = instruction->GetLocations();
3250
3251 Register out_register = locations->Out().AsRegister<Register>();
3252 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003253 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003254 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3255 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003256
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003257 Register num = locations->GetTemp(0).AsRegister<Register>();
3258
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003259 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003260 __ testl(input_register, input_register);
3261 __ cmovl(kGreaterEqual, num, input_register);
3262 int shift = CTZ(imm);
3263 __ sarl(num, Immediate(shift));
3264
3265 if (imm < 0) {
3266 __ negl(num);
3267 }
3268
3269 __ movl(out_register, num);
3270}
3271
3272void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3273 DCHECK(instruction->IsDiv() || instruction->IsRem());
3274
3275 LocationSummary* locations = instruction->GetLocations();
3276 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3277
3278 Register eax = locations->InAt(0).AsRegister<Register>();
3279 Register out = locations->Out().AsRegister<Register>();
3280 Register num;
3281 Register edx;
3282
3283 if (instruction->IsDiv()) {
3284 edx = locations->GetTemp(0).AsRegister<Register>();
3285 num = locations->GetTemp(1).AsRegister<Register>();
3286 } else {
3287 edx = locations->Out().AsRegister<Register>();
3288 num = locations->GetTemp(0).AsRegister<Register>();
3289 }
3290
3291 DCHECK_EQ(EAX, eax);
3292 DCHECK_EQ(EDX, edx);
3293 if (instruction->IsDiv()) {
3294 DCHECK_EQ(EAX, out);
3295 } else {
3296 DCHECK_EQ(EDX, out);
3297 }
3298
3299 int64_t magic;
3300 int shift;
3301 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3302
Mark Mendell0c9497d2015-08-21 09:30:05 -04003303 NearLabel ndiv;
3304 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003305 // If numerator is 0, the result is 0, no computation needed.
3306 __ testl(eax, eax);
3307 __ j(kNotEqual, &ndiv);
3308
3309 __ xorl(out, out);
3310 __ jmp(&end);
3311
3312 __ Bind(&ndiv);
3313
3314 // Save the numerator.
3315 __ movl(num, eax);
3316
3317 // EAX = magic
3318 __ movl(eax, Immediate(magic));
3319
3320 // EDX:EAX = magic * numerator
3321 __ imull(num);
3322
3323 if (imm > 0 && magic < 0) {
3324 // EDX += num
3325 __ addl(edx, num);
3326 } else if (imm < 0 && magic > 0) {
3327 __ subl(edx, num);
3328 }
3329
3330 // Shift if needed.
3331 if (shift != 0) {
3332 __ sarl(edx, Immediate(shift));
3333 }
3334
3335 // EDX += 1 if EDX < 0
3336 __ movl(eax, edx);
3337 __ shrl(edx, Immediate(31));
3338 __ addl(edx, eax);
3339
3340 if (instruction->IsRem()) {
3341 __ movl(eax, num);
3342 __ imull(edx, Immediate(imm));
3343 __ subl(eax, edx);
3344 __ movl(edx, eax);
3345 } else {
3346 __ movl(eax, edx);
3347 }
3348 __ Bind(&end);
3349}
3350
Calin Juravlebacfec32014-11-14 15:54:36 +00003351void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3352 DCHECK(instruction->IsDiv() || instruction->IsRem());
3353
3354 LocationSummary* locations = instruction->GetLocations();
3355 Location out = locations->Out();
3356 Location first = locations->InAt(0);
3357 Location second = locations->InAt(1);
3358 bool is_div = instruction->IsDiv();
3359
3360 switch (instruction->GetResultType()) {
3361 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003362 DCHECK_EQ(EAX, first.AsRegister<Register>());
3363 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003364
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003365 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003366 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003367
3368 if (imm == 0) {
3369 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3370 } else if (imm == 1 || imm == -1) {
3371 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003372 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003373 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003374 } else {
3375 DCHECK(imm <= -2 || imm >= 2);
3376 GenerateDivRemWithAnyConstant(instruction);
3377 }
3378 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003379 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3380 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003381 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003382
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003383 Register second_reg = second.AsRegister<Register>();
3384 // 0x80000000/-1 triggers an arithmetic exception!
3385 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3386 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003387
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003388 __ cmpl(second_reg, Immediate(-1));
3389 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003390
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003391 // edx:eax <- sign-extended of eax
3392 __ cdq();
3393 // eax = quotient, edx = remainder
3394 __ idivl(second_reg);
3395 __ Bind(slow_path->GetExitLabel());
3396 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003397 break;
3398 }
3399
3400 case Primitive::kPrimLong: {
3401 InvokeRuntimeCallingConvention calling_convention;
3402 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3403 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3404 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3405 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3406 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3407 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3408
3409 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003410 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3411 instruction,
3412 instruction->GetDexPc(),
3413 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003414 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003415 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003416 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3417 instruction,
3418 instruction->GetDexPc(),
3419 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003420 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003421 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003422 break;
3423 }
3424
3425 default:
3426 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3427 }
3428}
3429
Calin Juravle7c4954d2014-10-28 16:57:40 +00003430void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003431 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003432 ? LocationSummary::kCall
3433 : LocationSummary::kNoCall;
3434 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3435
Calin Juravle7c4954d2014-10-28 16:57:40 +00003436 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003437 case Primitive::kPrimInt: {
3438 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003439 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003440 locations->SetOut(Location::SameAsFirstInput());
3441 // Intel uses edx:eax as the dividend.
3442 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003443 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3444 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3445 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003446 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003447 locations->AddTemp(Location::RequiresRegister());
3448 }
Calin Juravled0d48522014-11-04 16:40:20 +00003449 break;
3450 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003451 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003452 InvokeRuntimeCallingConvention calling_convention;
3453 locations->SetInAt(0, Location::RegisterPairLocation(
3454 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3455 locations->SetInAt(1, Location::RegisterPairLocation(
3456 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3457 // Runtime helper puts the result in EAX, EDX.
3458 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003459 break;
3460 }
3461 case Primitive::kPrimFloat:
3462 case Primitive::kPrimDouble: {
3463 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003464 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3465 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003466 } else if (div->InputAt(1)->IsConstant()) {
3467 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003468 } else {
3469 locations->SetInAt(1, Location::Any());
3470 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003471 locations->SetOut(Location::SameAsFirstInput());
3472 break;
3473 }
3474
3475 default:
3476 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3477 }
3478}
3479
3480void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3481 LocationSummary* locations = div->GetLocations();
3482 Location first = locations->InAt(0);
3483 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003484
3485 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003486 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003487 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003488 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003489 break;
3490 }
3491
3492 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003493 if (second.IsFpuRegister()) {
3494 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3495 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3496 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003497 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003498 __ divss(first.AsFpuRegister<XmmRegister>(),
3499 codegen_->LiteralFloatAddress(
3500 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3501 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3502 } else {
3503 DCHECK(second.IsStackSlot());
3504 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3505 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003506 break;
3507 }
3508
3509 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003510 if (second.IsFpuRegister()) {
3511 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3512 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3513 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003514 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003515 __ divsd(first.AsFpuRegister<XmmRegister>(),
3516 codegen_->LiteralDoubleAddress(
3517 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3518 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3519 } else {
3520 DCHECK(second.IsDoubleStackSlot());
3521 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3522 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003523 break;
3524 }
3525
3526 default:
3527 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3528 }
3529}
3530
Calin Juravlebacfec32014-11-14 15:54:36 +00003531void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003532 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003533
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003534 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3535 ? LocationSummary::kCall
3536 : LocationSummary::kNoCall;
3537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003538
Calin Juravled2ec87d2014-12-08 14:24:46 +00003539 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003540 case Primitive::kPrimInt: {
3541 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003542 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003543 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003544 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3545 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3546 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003547 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003548 locations->AddTemp(Location::RequiresRegister());
3549 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003550 break;
3551 }
3552 case Primitive::kPrimLong: {
3553 InvokeRuntimeCallingConvention calling_convention;
3554 locations->SetInAt(0, Location::RegisterPairLocation(
3555 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3556 locations->SetInAt(1, Location::RegisterPairLocation(
3557 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3558 // Runtime helper puts the result in EAX, EDX.
3559 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3560 break;
3561 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003562 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003563 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003564 locations->SetInAt(0, Location::Any());
3565 locations->SetInAt(1, Location::Any());
3566 locations->SetOut(Location::RequiresFpuRegister());
3567 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003568 break;
3569 }
3570
3571 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003572 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003573 }
3574}
3575
3576void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3577 Primitive::Type type = rem->GetResultType();
3578 switch (type) {
3579 case Primitive::kPrimInt:
3580 case Primitive::kPrimLong: {
3581 GenerateDivRemIntegral(rem);
3582 break;
3583 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003584 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003585 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003586 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003587 break;
3588 }
3589 default:
3590 LOG(FATAL) << "Unexpected rem type " << type;
3591 }
3592}
3593
Calin Juravled0d48522014-11-04 16:40:20 +00003594void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003595 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3596 ? LocationSummary::kCallOnSlowPath
3597 : LocationSummary::kNoCall;
3598 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003599 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003600 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003601 case Primitive::kPrimByte:
3602 case Primitive::kPrimChar:
3603 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003604 case Primitive::kPrimInt: {
3605 locations->SetInAt(0, Location::Any());
3606 break;
3607 }
3608 case Primitive::kPrimLong: {
3609 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3610 if (!instruction->IsConstant()) {
3611 locations->AddTemp(Location::RequiresRegister());
3612 }
3613 break;
3614 }
3615 default:
3616 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3617 }
Calin Juravled0d48522014-11-04 16:40:20 +00003618 if (instruction->HasUses()) {
3619 locations->SetOut(Location::SameAsFirstInput());
3620 }
3621}
3622
3623void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003624 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003625 codegen_->AddSlowPath(slow_path);
3626
3627 LocationSummary* locations = instruction->GetLocations();
3628 Location value = locations->InAt(0);
3629
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003630 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003631 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003632 case Primitive::kPrimByte:
3633 case Primitive::kPrimChar:
3634 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003635 case Primitive::kPrimInt: {
3636 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003637 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003638 __ j(kEqual, slow_path->GetEntryLabel());
3639 } else if (value.IsStackSlot()) {
3640 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3641 __ j(kEqual, slow_path->GetEntryLabel());
3642 } else {
3643 DCHECK(value.IsConstant()) << value;
3644 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3645 __ jmp(slow_path->GetEntryLabel());
3646 }
3647 }
3648 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003649 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003650 case Primitive::kPrimLong: {
3651 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003652 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003653 __ movl(temp, value.AsRegisterPairLow<Register>());
3654 __ orl(temp, value.AsRegisterPairHigh<Register>());
3655 __ j(kEqual, slow_path->GetEntryLabel());
3656 } else {
3657 DCHECK(value.IsConstant()) << value;
3658 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3659 __ jmp(slow_path->GetEntryLabel());
3660 }
3661 }
3662 break;
3663 }
3664 default:
3665 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003666 }
Calin Juravled0d48522014-11-04 16:40:20 +00003667}
3668
Calin Juravle9aec02f2014-11-18 23:06:35 +00003669void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3670 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3671
3672 LocationSummary* locations =
3673 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3674
3675 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003676 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003677 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003678 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003679 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003680 // The shift count needs to be in CL or a constant.
3681 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003682 locations->SetOut(Location::SameAsFirstInput());
3683 break;
3684 }
3685 default:
3686 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3687 }
3688}
3689
3690void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3691 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3692
3693 LocationSummary* locations = op->GetLocations();
3694 Location first = locations->InAt(0);
3695 Location second = locations->InAt(1);
3696 DCHECK(first.Equals(locations->Out()));
3697
3698 switch (op->GetResultType()) {
3699 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003700 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003701 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003702 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003703 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003704 DCHECK_EQ(ECX, second_reg);
3705 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003706 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003707 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003708 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003709 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003710 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003711 }
3712 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003713 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003714 if (shift == 0) {
3715 return;
3716 }
3717 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003718 if (op->IsShl()) {
3719 __ shll(first_reg, imm);
3720 } else if (op->IsShr()) {
3721 __ sarl(first_reg, imm);
3722 } else {
3723 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003724 }
3725 }
3726 break;
3727 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003728 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003729 if (second.IsRegister()) {
3730 Register second_reg = second.AsRegister<Register>();
3731 DCHECK_EQ(ECX, second_reg);
3732 if (op->IsShl()) {
3733 GenerateShlLong(first, second_reg);
3734 } else if (op->IsShr()) {
3735 GenerateShrLong(first, second_reg);
3736 } else {
3737 GenerateUShrLong(first, second_reg);
3738 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003739 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003740 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003741 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003742 // Nothing to do if the shift is 0, as the input is already the output.
3743 if (shift != 0) {
3744 if (op->IsShl()) {
3745 GenerateShlLong(first, shift);
3746 } else if (op->IsShr()) {
3747 GenerateShrLong(first, shift);
3748 } else {
3749 GenerateUShrLong(first, shift);
3750 }
3751 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003752 }
3753 break;
3754 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003755 default:
3756 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3757 }
3758}
3759
Mark P Mendell73945692015-04-29 14:56:17 +00003760void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3761 Register low = loc.AsRegisterPairLow<Register>();
3762 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003763 if (shift == 1) {
3764 // This is just an addition.
3765 __ addl(low, low);
3766 __ adcl(high, high);
3767 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003768 // Shift by 32 is easy. High gets low, and low gets 0.
3769 codegen_->EmitParallelMoves(
3770 loc.ToLow(),
3771 loc.ToHigh(),
3772 Primitive::kPrimInt,
3773 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3774 loc.ToLow(),
3775 Primitive::kPrimInt);
3776 } else if (shift > 32) {
3777 // Low part becomes 0. High part is low part << (shift-32).
3778 __ movl(high, low);
3779 __ shll(high, Immediate(shift - 32));
3780 __ xorl(low, low);
3781 } else {
3782 // Between 1 and 31.
3783 __ shld(high, low, Immediate(shift));
3784 __ shll(low, Immediate(shift));
3785 }
3786}
3787
Calin Juravle9aec02f2014-11-18 23:06:35 +00003788void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003789 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003790 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3791 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3792 __ testl(shifter, Immediate(32));
3793 __ j(kEqual, &done);
3794 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3795 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3796 __ Bind(&done);
3797}
3798
Mark P Mendell73945692015-04-29 14:56:17 +00003799void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3800 Register low = loc.AsRegisterPairLow<Register>();
3801 Register high = loc.AsRegisterPairHigh<Register>();
3802 if (shift == 32) {
3803 // Need to copy the sign.
3804 DCHECK_NE(low, high);
3805 __ movl(low, high);
3806 __ sarl(high, Immediate(31));
3807 } else if (shift > 32) {
3808 DCHECK_NE(low, high);
3809 // High part becomes sign. Low part is shifted by shift - 32.
3810 __ movl(low, high);
3811 __ sarl(high, Immediate(31));
3812 __ sarl(low, Immediate(shift - 32));
3813 } else {
3814 // Between 1 and 31.
3815 __ shrd(low, high, Immediate(shift));
3816 __ sarl(high, Immediate(shift));
3817 }
3818}
3819
Calin Juravle9aec02f2014-11-18 23:06:35 +00003820void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003821 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003822 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3823 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3824 __ testl(shifter, Immediate(32));
3825 __ j(kEqual, &done);
3826 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3827 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3828 __ Bind(&done);
3829}
3830
Mark P Mendell73945692015-04-29 14:56:17 +00003831void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3832 Register low = loc.AsRegisterPairLow<Register>();
3833 Register high = loc.AsRegisterPairHigh<Register>();
3834 if (shift == 32) {
3835 // Shift by 32 is easy. Low gets high, and high gets 0.
3836 codegen_->EmitParallelMoves(
3837 loc.ToHigh(),
3838 loc.ToLow(),
3839 Primitive::kPrimInt,
3840 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3841 loc.ToHigh(),
3842 Primitive::kPrimInt);
3843 } else if (shift > 32) {
3844 // Low part is high >> (shift - 32). High part becomes 0.
3845 __ movl(low, high);
3846 __ shrl(low, Immediate(shift - 32));
3847 __ xorl(high, high);
3848 } else {
3849 // Between 1 and 31.
3850 __ shrd(low, high, Immediate(shift));
3851 __ shrl(high, Immediate(shift));
3852 }
3853}
3854
Calin Juravle9aec02f2014-11-18 23:06:35 +00003855void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003856 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003857 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3858 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3859 __ testl(shifter, Immediate(32));
3860 __ j(kEqual, &done);
3861 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3862 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3863 __ Bind(&done);
3864}
3865
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003866void LocationsBuilderX86::VisitRor(HRor* ror) {
3867 LocationSummary* locations =
3868 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3869
3870 switch (ror->GetResultType()) {
3871 case Primitive::kPrimLong:
3872 // Add the temporary needed.
3873 locations->AddTemp(Location::RequiresRegister());
3874 FALLTHROUGH_INTENDED;
3875 case Primitive::kPrimInt:
3876 locations->SetInAt(0, Location::RequiresRegister());
3877 // The shift count needs to be in CL (unless it is a constant).
3878 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3879 locations->SetOut(Location::SameAsFirstInput());
3880 break;
3881 default:
3882 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3883 UNREACHABLE();
3884 }
3885}
3886
3887void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3888 LocationSummary* locations = ror->GetLocations();
3889 Location first = locations->InAt(0);
3890 Location second = locations->InAt(1);
3891
3892 if (ror->GetResultType() == Primitive::kPrimInt) {
3893 Register first_reg = first.AsRegister<Register>();
3894 if (second.IsRegister()) {
3895 Register second_reg = second.AsRegister<Register>();
3896 __ rorl(first_reg, second_reg);
3897 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003898 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003899 __ rorl(first_reg, imm);
3900 }
3901 return;
3902 }
3903
3904 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3905 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3906 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3907 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3908 if (second.IsRegister()) {
3909 Register second_reg = second.AsRegister<Register>();
3910 DCHECK_EQ(second_reg, ECX);
3911 __ movl(temp_reg, first_reg_hi);
3912 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3913 __ shrd(first_reg_lo, temp_reg, second_reg);
3914 __ movl(temp_reg, first_reg_hi);
3915 __ testl(second_reg, Immediate(32));
3916 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3917 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3918 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003919 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003920 if (shift_amt == 0) {
3921 // Already fine.
3922 return;
3923 }
3924 if (shift_amt == 32) {
3925 // Just swap.
3926 __ movl(temp_reg, first_reg_lo);
3927 __ movl(first_reg_lo, first_reg_hi);
3928 __ movl(first_reg_hi, temp_reg);
3929 return;
3930 }
3931
3932 Immediate imm(shift_amt);
3933 // Save the constents of the low value.
3934 __ movl(temp_reg, first_reg_lo);
3935
3936 // Shift right into low, feeding bits from high.
3937 __ shrd(first_reg_lo, first_reg_hi, imm);
3938
3939 // Shift right into high, feeding bits from the original low.
3940 __ shrd(first_reg_hi, temp_reg, imm);
3941
3942 // Swap if needed.
3943 if (shift_amt > 32) {
3944 __ movl(temp_reg, first_reg_lo);
3945 __ movl(first_reg_lo, first_reg_hi);
3946 __ movl(first_reg_hi, temp_reg);
3947 }
3948 }
3949}
3950
Calin Juravle9aec02f2014-11-18 23:06:35 +00003951void LocationsBuilderX86::VisitShl(HShl* shl) {
3952 HandleShift(shl);
3953}
3954
3955void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3956 HandleShift(shl);
3957}
3958
3959void LocationsBuilderX86::VisitShr(HShr* shr) {
3960 HandleShift(shr);
3961}
3962
3963void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3964 HandleShift(shr);
3965}
3966
3967void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3968 HandleShift(ushr);
3969}
3970
3971void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3972 HandleShift(ushr);
3973}
3974
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003975void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003976 LocationSummary* locations =
3977 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003978 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003979 if (instruction->IsStringAlloc()) {
3980 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3981 } else {
3982 InvokeRuntimeCallingConvention calling_convention;
3983 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3984 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3985 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003986}
3987
3988void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003989 // Note: if heap poisoning is enabled, the entry point takes cares
3990 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003991 if (instruction->IsStringAlloc()) {
3992 // String is allocated through StringFactory. Call NewEmptyString entry point.
3993 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3994 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
3995 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3996 __ call(Address(temp, code_offset.Int32Value()));
3997 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3998 } else {
3999 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4000 instruction,
4001 instruction->GetDexPc(),
4002 nullptr);
4003 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4004 DCHECK(!codegen_->IsLeafMethod());
4005 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004006}
4007
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004008void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4009 LocationSummary* locations =
4010 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4011 locations->SetOut(Location::RegisterLocation(EAX));
4012 InvokeRuntimeCallingConvention calling_convention;
4013 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004014 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004015 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004016}
4017
4018void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4019 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004020 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004021 // Note: if heap poisoning is enabled, the entry point takes cares
4022 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004023 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4024 instruction,
4025 instruction->GetDexPc(),
4026 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004027 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004028 DCHECK(!codegen_->IsLeafMethod());
4029}
4030
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004031void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004032 LocationSummary* locations =
4033 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004034 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4035 if (location.IsStackSlot()) {
4036 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4037 } else if (location.IsDoubleStackSlot()) {
4038 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004039 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004040 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004041}
4042
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004043void InstructionCodeGeneratorX86::VisitParameterValue(
4044 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4045}
4046
4047void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4048 LocationSummary* locations =
4049 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4050 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4051}
4052
4053void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004054}
4055
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004056void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4057 LocationSummary* locations =
4058 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4059 locations->SetInAt(0, Location::RequiresRegister());
4060 locations->SetOut(Location::RequiresRegister());
4061}
4062
4063void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4064 LocationSummary* locations = instruction->GetLocations();
4065 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004066 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004067 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4068 instruction->GetIndex(), kX86PointerSize).SizeValue();
4069 } else {
4070 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4071 instruction->GetIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
4072 }
4073 __ movl(locations->Out().AsRegister<Register>(),
4074 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4075}
4076
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004077void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004078 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004079 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004080 locations->SetInAt(0, Location::RequiresRegister());
4081 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004082}
4083
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004084void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4085 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004086 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004087 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004088 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004089 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004090 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004091 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004092 break;
4093
4094 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004095 __ notl(out.AsRegisterPairLow<Register>());
4096 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004097 break;
4098
4099 default:
4100 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4101 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004102}
4103
David Brazdil66d126e2015-04-03 16:02:44 +01004104void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4105 LocationSummary* locations =
4106 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4107 locations->SetInAt(0, Location::RequiresRegister());
4108 locations->SetOut(Location::SameAsFirstInput());
4109}
4110
4111void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004112 LocationSummary* locations = bool_not->GetLocations();
4113 Location in = locations->InAt(0);
4114 Location out = locations->Out();
4115 DCHECK(in.Equals(out));
4116 __ xorl(out.AsRegister<Register>(), Immediate(1));
4117}
4118
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004119void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004120 LocationSummary* locations =
4121 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004122 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004123 case Primitive::kPrimBoolean:
4124 case Primitive::kPrimByte:
4125 case Primitive::kPrimShort:
4126 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004127 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004128 case Primitive::kPrimLong: {
4129 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004130 locations->SetInAt(1, Location::Any());
4131 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4132 break;
4133 }
4134 case Primitive::kPrimFloat:
4135 case Primitive::kPrimDouble: {
4136 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004137 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4138 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4139 } else if (compare->InputAt(1)->IsConstant()) {
4140 locations->SetInAt(1, Location::RequiresFpuRegister());
4141 } else {
4142 locations->SetInAt(1, Location::Any());
4143 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004144 locations->SetOut(Location::RequiresRegister());
4145 break;
4146 }
4147 default:
4148 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4149 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004150}
4151
4152void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004153 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004154 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004155 Location left = locations->InAt(0);
4156 Location right = locations->InAt(1);
4157
Mark Mendell0c9497d2015-08-21 09:30:05 -04004158 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004159 Condition less_cond = kLess;
4160
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004161 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004162 case Primitive::kPrimBoolean:
4163 case Primitive::kPrimByte:
4164 case Primitive::kPrimShort:
4165 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004166 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004167 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004168 break;
4169 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004170 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004171 Register left_low = left.AsRegisterPairLow<Register>();
4172 Register left_high = left.AsRegisterPairHigh<Register>();
4173 int32_t val_low = 0;
4174 int32_t val_high = 0;
4175 bool right_is_const = false;
4176
4177 if (right.IsConstant()) {
4178 DCHECK(right.GetConstant()->IsLongConstant());
4179 right_is_const = true;
4180 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4181 val_low = Low32Bits(val);
4182 val_high = High32Bits(val);
4183 }
4184
Calin Juravleddb7df22014-11-25 20:56:51 +00004185 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004186 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004187 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004188 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004189 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004190 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004191 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004192 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004193 __ j(kLess, &less); // Signed compare.
4194 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004195 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004196 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004197 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004198 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004199 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004200 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004201 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004202 }
Aart Bika19616e2016-02-01 18:57:58 -08004203 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004204 break;
4205 }
4206 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004207 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004208 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004209 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004210 break;
4211 }
4212 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004213 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004214 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004215 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004216 break;
4217 }
4218 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004219 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004220 }
Aart Bika19616e2016-02-01 18:57:58 -08004221
Calin Juravleddb7df22014-11-25 20:56:51 +00004222 __ movl(out, Immediate(0));
4223 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004224 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004225
4226 __ Bind(&greater);
4227 __ movl(out, Immediate(1));
4228 __ jmp(&done);
4229
4230 __ Bind(&less);
4231 __ movl(out, Immediate(-1));
4232
4233 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004234}
4235
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004236void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004237 LocationSummary* locations =
4238 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004239 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4240 locations->SetInAt(i, Location::Any());
4241 }
4242 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004243}
4244
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004245void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004246 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004247}
4248
Roland Levillain7c1559a2015-12-15 10:55:36 +00004249void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004250 /*
4251 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4252 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4253 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4254 */
4255 switch (kind) {
4256 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004257 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004258 break;
4259 }
4260 case MemBarrierKind::kAnyStore:
4261 case MemBarrierKind::kLoadAny:
4262 case MemBarrierKind::kStoreStore: {
4263 // nop
4264 break;
4265 }
4266 default:
4267 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004268 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004269}
4270
Vladimir Markodc151b22015-10-15 18:02:30 +01004271HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4272 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4273 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004274 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4275
4276 // We disable pc-relative load when there is an irreducible loop, as the optimization
4277 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004278 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4279 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004280 if (GetGraph()->HasIrreducibleLoops() &&
4281 (dispatch_info.method_load_kind ==
4282 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4283 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4284 }
4285 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004286 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4287 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4288 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4289 // (Though the direct CALL ptr16:32 is available for consideration).
4290 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004291 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004292 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004293 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004294 0u
4295 };
4296 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004297 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004298 }
4299}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004300
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004301Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4302 Register temp) {
4303 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004304 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004305 if (!invoke->GetLocations()->Intrinsified()) {
4306 return location.AsRegister<Register>();
4307 }
4308 // For intrinsics we allow any location, so it may be on the stack.
4309 if (!location.IsRegister()) {
4310 __ movl(temp, Address(ESP, location.GetStackIndex()));
4311 return temp;
4312 }
4313 // For register locations, check if the register was saved. If so, get it from the stack.
4314 // Note: There is a chance that the register was saved but not overwritten, so we could
4315 // save one load. However, since this is just an intrinsic slow path we prefer this
4316 // simple and more robust approach rather that trying to determine if that's the case.
4317 SlowPathCode* slow_path = GetCurrentSlowPath();
4318 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4319 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4320 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4321 __ movl(temp, Address(ESP, stack_offset));
4322 return temp;
4323 }
4324 return location.AsRegister<Register>();
4325}
4326
Vladimir Marko58155012015-08-19 12:49:41 +00004327void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4328 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4329 switch (invoke->GetMethodLoadKind()) {
4330 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4331 // temp = thread->string_init_entrypoint
4332 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4333 break;
4334 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004335 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004336 break;
4337 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4338 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4339 break;
4340 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004341 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004342 method_patches_.emplace_back(invoke->GetTargetMethod());
4343 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4344 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004345 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4346 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4347 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004348 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004349 // Bind a new fixup label at the end of the "movl" insn.
4350 uint32_t offset = invoke->GetDexCacheArrayOffset();
4351 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004352 break;
4353 }
Vladimir Marko58155012015-08-19 12:49:41 +00004354 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004355 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004356 Register method_reg;
4357 Register reg = temp.AsRegister<Register>();
4358 if (current_method.IsRegister()) {
4359 method_reg = current_method.AsRegister<Register>();
4360 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004361 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004362 DCHECK(!current_method.IsValid());
4363 method_reg = reg;
4364 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4365 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004366 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004367 __ movl(reg, Address(method_reg,
4368 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004369 // temp = temp[index_in_cache]
4370 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4371 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4372 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004373 }
Vladimir Marko58155012015-08-19 12:49:41 +00004374 }
4375
4376 switch (invoke->GetCodePtrLocation()) {
4377 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4378 __ call(GetFrameEntryLabel());
4379 break;
4380 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4381 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4382 Label* label = &relative_call_patches_.back().label;
4383 __ call(label); // Bind to the patch label, override at link time.
4384 __ Bind(label); // Bind the label at the end of the "call" insn.
4385 break;
4386 }
4387 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4388 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004389 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4390 LOG(FATAL) << "Unsupported";
4391 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004392 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4393 // (callee_method + offset_of_quick_compiled_code)()
4394 __ call(Address(callee_method.AsRegister<Register>(),
4395 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4396 kX86WordSize).Int32Value()));
4397 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004398 }
4399
4400 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004401}
4402
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004403void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4404 Register temp = temp_in.AsRegister<Register>();
4405 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4406 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004407
4408 // Use the calling convention instead of the location of the receiver, as
4409 // intrinsics may have put the receiver in a different register. In the intrinsics
4410 // slow path, the arguments have been moved to the right place, so here we are
4411 // guaranteed that the receiver is the first register of the calling convention.
4412 InvokeDexCallingConvention calling_convention;
4413 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004414 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004415 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004416 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004417 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004418 // Instead of simply (possibly) unpoisoning `temp` here, we should
4419 // emit a read barrier for the previous class reference load.
4420 // However this is not required in practice, as this is an
4421 // intermediate/temporary reference and because the current
4422 // concurrent copying collector keeps the from-space memory
4423 // intact/accessible until the end of the marking phase (the
4424 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004425 __ MaybeUnpoisonHeapReference(temp);
4426 // temp = temp->GetMethodAt(method_offset);
4427 __ movl(temp, Address(temp, method_offset));
4428 // call temp->GetEntryPoint();
4429 __ call(Address(
4430 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4431}
4432
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004433void CodeGeneratorX86::RecordSimplePatch() {
4434 if (GetCompilerOptions().GetIncludePatchInformation()) {
4435 simple_patches_.emplace_back();
4436 __ Bind(&simple_patches_.back());
4437 }
4438}
4439
4440void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4441 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4442 __ Bind(&string_patches_.back().label);
4443}
4444
4445Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4446 uint32_t element_offset) {
4447 // Add the patch entry and bind its label at the end of the instruction.
4448 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4449 return &pc_relative_dex_cache_patches_.back().label;
4450}
4451
Vladimir Marko58155012015-08-19 12:49:41 +00004452void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4453 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004454 size_t size =
4455 method_patches_.size() +
4456 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004457 pc_relative_dex_cache_patches_.size() +
4458 simple_patches_.size() +
4459 string_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004460 linker_patches->reserve(size);
4461 // The label points to the end of the "movl" insn but the literal offset for method
4462 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4463 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004464 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004465 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004466 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4467 info.target_method.dex_file,
4468 info.target_method.dex_method_index));
4469 }
4470 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004471 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004472 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4473 info.target_method.dex_file,
4474 info.target_method.dex_method_index));
4475 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004476 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4477 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4478 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4479 &info.target_dex_file,
4480 GetMethodAddressOffset(),
4481 info.element_offset));
4482 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004483 for (const Label& label : simple_patches_) {
4484 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4485 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4486 }
4487 if (GetCompilerOptions().GetCompilePic()) {
4488 for (const StringPatchInfo<Label>& info : string_patches_) {
4489 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4490 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4491 &info.dex_file,
4492 GetMethodAddressOffset(),
4493 info.string_index));
4494 }
4495 } else {
4496 for (const StringPatchInfo<Label>& info : string_patches_) {
4497 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4498 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4499 &info.dex_file,
4500 info.string_index));
4501 }
4502 }
Vladimir Marko58155012015-08-19 12:49:41 +00004503}
4504
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004505void CodeGeneratorX86::MarkGCCard(Register temp,
4506 Register card,
4507 Register object,
4508 Register value,
4509 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004510 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004511 if (value_can_be_null) {
4512 __ testl(value, value);
4513 __ j(kEqual, &is_null);
4514 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004515 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4516 __ movl(temp, object);
4517 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004518 __ movb(Address(temp, card, TIMES_1, 0),
4519 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004520 if (value_can_be_null) {
4521 __ Bind(&is_null);
4522 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004523}
4524
Calin Juravle52c48962014-12-16 17:02:57 +00004525void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4526 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004527
4528 bool object_field_get_with_read_barrier =
4529 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004530 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004531 new (GetGraph()->GetArena()) LocationSummary(instruction,
4532 kEmitCompilerReadBarrier ?
4533 LocationSummary::kCallOnSlowPath :
4534 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004535 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004536
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004537 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4538 locations->SetOut(Location::RequiresFpuRegister());
4539 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004540 // The output overlaps in case of long: we don't want the low move
4541 // to overwrite the object's location. Likewise, in the case of
4542 // an object field get with read barriers enabled, we do not want
4543 // the move to overwrite the object's location, as we need it to emit
4544 // the read barrier.
4545 locations->SetOut(
4546 Location::RequiresRegister(),
4547 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4548 Location::kOutputOverlap :
4549 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004550 }
Calin Juravle52c48962014-12-16 17:02:57 +00004551
4552 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4553 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004554 // So we use an XMM register as a temp to achieve atomicity (first
4555 // load the temp into the XMM and then copy the XMM into the
4556 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004557 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004558 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4559 // We need a temporary register for the read barrier marking slow
4560 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4561 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004562 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004563}
4564
Calin Juravle52c48962014-12-16 17:02:57 +00004565void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4566 const FieldInfo& field_info) {
4567 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004568
Calin Juravle52c48962014-12-16 17:02:57 +00004569 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004570 Location base_loc = locations->InAt(0);
4571 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004572 Location out = locations->Out();
4573 bool is_volatile = field_info.IsVolatile();
4574 Primitive::Type field_type = field_info.GetFieldType();
4575 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4576
4577 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004578 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004579 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004580 break;
4581 }
4582
4583 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004584 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004585 break;
4586 }
4587
4588 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004589 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004590 break;
4591 }
4592
4593 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004594 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004595 break;
4596 }
4597
4598 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004599 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004600 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004601
4602 case Primitive::kPrimNot: {
4603 // /* HeapReference<Object> */ out = *(base + offset)
4604 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4605 Location temp_loc = locations->GetTemp(0);
4606 // Note that a potential implicit null check is handled in this
4607 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4608 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4609 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4610 if (is_volatile) {
4611 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4612 }
4613 } else {
4614 __ movl(out.AsRegister<Register>(), Address(base, offset));
4615 codegen_->MaybeRecordImplicitNullCheck(instruction);
4616 if (is_volatile) {
4617 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4618 }
4619 // If read barriers are enabled, emit read barriers other than
4620 // Baker's using a slow path (and also unpoison the loaded
4621 // reference, if heap poisoning is enabled).
4622 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4623 }
4624 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004625 }
4626
4627 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004628 if (is_volatile) {
4629 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4630 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004631 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004632 __ movd(out.AsRegisterPairLow<Register>(), temp);
4633 __ psrlq(temp, Immediate(32));
4634 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4635 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004636 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004637 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004638 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004639 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4640 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004641 break;
4642 }
4643
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004644 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004645 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004646 break;
4647 }
4648
4649 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004650 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004651 break;
4652 }
4653
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004654 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004655 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004656 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004657 }
Calin Juravle52c48962014-12-16 17:02:57 +00004658
Roland Levillain7c1559a2015-12-15 10:55:36 +00004659 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4660 // Potential implicit null checks, in the case of reference or
4661 // long fields, are handled in the previous switch statement.
4662 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004663 codegen_->MaybeRecordImplicitNullCheck(instruction);
4664 }
4665
Calin Juravle52c48962014-12-16 17:02:57 +00004666 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004667 if (field_type == Primitive::kPrimNot) {
4668 // Memory barriers, in the case of references, are also handled
4669 // in the previous switch statement.
4670 } else {
4671 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4672 }
Roland Levillain4d027112015-07-01 15:41:14 +01004673 }
Calin Juravle52c48962014-12-16 17:02:57 +00004674}
4675
4676void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4677 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4678
4679 LocationSummary* locations =
4680 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4681 locations->SetInAt(0, Location::RequiresRegister());
4682 bool is_volatile = field_info.IsVolatile();
4683 Primitive::Type field_type = field_info.GetFieldType();
4684 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4685 || (field_type == Primitive::kPrimByte);
4686
4687 // The register allocator does not support multiple
4688 // inputs that die at entry with one in a specific register.
4689 if (is_byte_type) {
4690 // Ensure the value is in a byte register.
4691 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004692 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004693 if (is_volatile && field_type == Primitive::kPrimDouble) {
4694 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4695 locations->SetInAt(1, Location::RequiresFpuRegister());
4696 } else {
4697 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4698 }
4699 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4700 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004701 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004702
Calin Juravle52c48962014-12-16 17:02:57 +00004703 // 64bits value can be atomically written to an address with movsd and an XMM register.
4704 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4705 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4706 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4707 // isolated cases when we need this it isn't worth adding the extra complexity.
4708 locations->AddTemp(Location::RequiresFpuRegister());
4709 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004710 } else {
4711 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4712
4713 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4714 // Temporary registers for the write barrier.
4715 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4716 // Ensure the card is in a byte register.
4717 locations->AddTemp(Location::RegisterLocation(ECX));
4718 }
Calin Juravle52c48962014-12-16 17:02:57 +00004719 }
4720}
4721
4722void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004723 const FieldInfo& field_info,
4724 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004725 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4726
4727 LocationSummary* locations = instruction->GetLocations();
4728 Register base = locations->InAt(0).AsRegister<Register>();
4729 Location value = locations->InAt(1);
4730 bool is_volatile = field_info.IsVolatile();
4731 Primitive::Type field_type = field_info.GetFieldType();
4732 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004733 bool needs_write_barrier =
4734 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004735
4736 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004737 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004738 }
4739
Mark Mendell81489372015-11-04 11:30:41 -05004740 bool maybe_record_implicit_null_check_done = false;
4741
Calin Juravle52c48962014-12-16 17:02:57 +00004742 switch (field_type) {
4743 case Primitive::kPrimBoolean:
4744 case Primitive::kPrimByte: {
4745 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4746 break;
4747 }
4748
4749 case Primitive::kPrimShort:
4750 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004751 if (value.IsConstant()) {
4752 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4753 __ movw(Address(base, offset), Immediate(v));
4754 } else {
4755 __ movw(Address(base, offset), value.AsRegister<Register>());
4756 }
Calin Juravle52c48962014-12-16 17:02:57 +00004757 break;
4758 }
4759
4760 case Primitive::kPrimInt:
4761 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004762 if (kPoisonHeapReferences && needs_write_barrier) {
4763 // Note that in the case where `value` is a null reference,
4764 // we do not enter this block, as the reference does not
4765 // need poisoning.
4766 DCHECK_EQ(field_type, Primitive::kPrimNot);
4767 Register temp = locations->GetTemp(0).AsRegister<Register>();
4768 __ movl(temp, value.AsRegister<Register>());
4769 __ PoisonHeapReference(temp);
4770 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004771 } else if (value.IsConstant()) {
4772 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4773 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004774 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004775 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004776 __ movl(Address(base, offset), value.AsRegister<Register>());
4777 }
Calin Juravle52c48962014-12-16 17:02:57 +00004778 break;
4779 }
4780
4781 case Primitive::kPrimLong: {
4782 if (is_volatile) {
4783 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4784 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4785 __ movd(temp1, value.AsRegisterPairLow<Register>());
4786 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4787 __ punpckldq(temp1, temp2);
4788 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004789 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004790 } else if (value.IsConstant()) {
4791 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4792 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4793 codegen_->MaybeRecordImplicitNullCheck(instruction);
4794 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004795 } else {
4796 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004797 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004798 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4799 }
Mark Mendell81489372015-11-04 11:30:41 -05004800 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004801 break;
4802 }
4803
4804 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004805 if (value.IsConstant()) {
4806 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4807 __ movl(Address(base, offset), Immediate(v));
4808 } else {
4809 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4810 }
Calin Juravle52c48962014-12-16 17:02:57 +00004811 break;
4812 }
4813
4814 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004815 if (value.IsConstant()) {
4816 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4817 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4818 codegen_->MaybeRecordImplicitNullCheck(instruction);
4819 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4820 maybe_record_implicit_null_check_done = true;
4821 } else {
4822 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4823 }
Calin Juravle52c48962014-12-16 17:02:57 +00004824 break;
4825 }
4826
4827 case Primitive::kPrimVoid:
4828 LOG(FATAL) << "Unreachable type " << field_type;
4829 UNREACHABLE();
4830 }
4831
Mark Mendell81489372015-11-04 11:30:41 -05004832 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004833 codegen_->MaybeRecordImplicitNullCheck(instruction);
4834 }
4835
Roland Levillain4d027112015-07-01 15:41:14 +01004836 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004837 Register temp = locations->GetTemp(0).AsRegister<Register>();
4838 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004839 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004840 }
4841
Calin Juravle52c48962014-12-16 17:02:57 +00004842 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004843 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004844 }
4845}
4846
4847void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4848 HandleFieldGet(instruction, instruction->GetFieldInfo());
4849}
4850
4851void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4852 HandleFieldGet(instruction, instruction->GetFieldInfo());
4853}
4854
4855void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4856 HandleFieldSet(instruction, instruction->GetFieldInfo());
4857}
4858
4859void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004860 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004861}
4862
4863void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4864 HandleFieldSet(instruction, instruction->GetFieldInfo());
4865}
4866
4867void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004868 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004869}
4870
4871void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4872 HandleFieldGet(instruction, instruction->GetFieldInfo());
4873}
4874
4875void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4876 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004877}
4878
Calin Juravlee460d1d2015-09-29 04:52:17 +01004879void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4880 HUnresolvedInstanceFieldGet* instruction) {
4881 FieldAccessCallingConventionX86 calling_convention;
4882 codegen_->CreateUnresolvedFieldLocationSummary(
4883 instruction, instruction->GetFieldType(), calling_convention);
4884}
4885
4886void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4887 HUnresolvedInstanceFieldGet* instruction) {
4888 FieldAccessCallingConventionX86 calling_convention;
4889 codegen_->GenerateUnresolvedFieldAccess(instruction,
4890 instruction->GetFieldType(),
4891 instruction->GetFieldIndex(),
4892 instruction->GetDexPc(),
4893 calling_convention);
4894}
4895
4896void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4897 HUnresolvedInstanceFieldSet* instruction) {
4898 FieldAccessCallingConventionX86 calling_convention;
4899 codegen_->CreateUnresolvedFieldLocationSummary(
4900 instruction, instruction->GetFieldType(), calling_convention);
4901}
4902
4903void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4904 HUnresolvedInstanceFieldSet* instruction) {
4905 FieldAccessCallingConventionX86 calling_convention;
4906 codegen_->GenerateUnresolvedFieldAccess(instruction,
4907 instruction->GetFieldType(),
4908 instruction->GetFieldIndex(),
4909 instruction->GetDexPc(),
4910 calling_convention);
4911}
4912
4913void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4914 HUnresolvedStaticFieldGet* instruction) {
4915 FieldAccessCallingConventionX86 calling_convention;
4916 codegen_->CreateUnresolvedFieldLocationSummary(
4917 instruction, instruction->GetFieldType(), calling_convention);
4918}
4919
4920void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4921 HUnresolvedStaticFieldGet* instruction) {
4922 FieldAccessCallingConventionX86 calling_convention;
4923 codegen_->GenerateUnresolvedFieldAccess(instruction,
4924 instruction->GetFieldType(),
4925 instruction->GetFieldIndex(),
4926 instruction->GetDexPc(),
4927 calling_convention);
4928}
4929
4930void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4931 HUnresolvedStaticFieldSet* instruction) {
4932 FieldAccessCallingConventionX86 calling_convention;
4933 codegen_->CreateUnresolvedFieldLocationSummary(
4934 instruction, instruction->GetFieldType(), calling_convention);
4935}
4936
4937void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4938 HUnresolvedStaticFieldSet* instruction) {
4939 FieldAccessCallingConventionX86 calling_convention;
4940 codegen_->GenerateUnresolvedFieldAccess(instruction,
4941 instruction->GetFieldType(),
4942 instruction->GetFieldIndex(),
4943 instruction->GetDexPc(),
4944 calling_convention);
4945}
4946
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004947void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004948 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4949 ? LocationSummary::kCallOnSlowPath
4950 : LocationSummary::kNoCall;
4951 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4952 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004953 ? Location::RequiresRegister()
4954 : Location::Any();
4955 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004956 if (instruction->HasUses()) {
4957 locations->SetOut(Location::SameAsFirstInput());
4958 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004959}
4960
Calin Juravle2ae48182016-03-16 14:05:09 +00004961void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4962 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004963 return;
4964 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004965 LocationSummary* locations = instruction->GetLocations();
4966 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004967
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004968 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004969 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004970}
4971
Calin Juravle2ae48182016-03-16 14:05:09 +00004972void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004973 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004974 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004975
4976 LocationSummary* locations = instruction->GetLocations();
4977 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004978
4979 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004980 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004981 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004982 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004983 } else {
4984 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004985 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004986 __ jmp(slow_path->GetEntryLabel());
4987 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004988 }
4989 __ j(kEqual, slow_path->GetEntryLabel());
4990}
4991
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004992void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004993 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004994}
4995
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004996void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004997 bool object_array_get_with_read_barrier =
4998 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004999 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005000 new (GetGraph()->GetArena()) LocationSummary(instruction,
5001 object_array_get_with_read_barrier ?
5002 LocationSummary::kCallOnSlowPath :
5003 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005004 locations->SetInAt(0, Location::RequiresRegister());
5005 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005006 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5007 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5008 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005009 // The output overlaps in case of long: we don't want the low move
5010 // to overwrite the array's location. Likewise, in the case of an
5011 // object array get with read barriers enabled, we do not want the
5012 // move to overwrite the array's location, as we need it to emit
5013 // the read barrier.
5014 locations->SetOut(
5015 Location::RequiresRegister(),
5016 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5017 Location::kOutputOverlap :
5018 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005019 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005020 // We need a temporary register for the read barrier marking slow
5021 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5022 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5023 locations->AddTemp(Location::RequiresRegister());
5024 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005025}
5026
5027void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5028 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005029 Location obj_loc = locations->InAt(0);
5030 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005031 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005032 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005033
Calin Juravle77520bc2015-01-12 18:45:46 +00005034 Primitive::Type type = instruction->GetType();
5035 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005036 case Primitive::kPrimBoolean: {
5037 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005038 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005039 if (index.IsConstant()) {
5040 __ movzxb(out, Address(obj,
5041 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5042 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005043 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005044 }
5045 break;
5046 }
5047
5048 case Primitive::kPrimByte: {
5049 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005050 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005051 if (index.IsConstant()) {
5052 __ movsxb(out, Address(obj,
5053 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5054 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005055 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005056 }
5057 break;
5058 }
5059
5060 case Primitive::kPrimShort: {
5061 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005062 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005063 if (index.IsConstant()) {
5064 __ movsxw(out, Address(obj,
5065 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5066 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005067 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005068 }
5069 break;
5070 }
5071
5072 case Primitive::kPrimChar: {
5073 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005074 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005075 if (index.IsConstant()) {
5076 __ movzxw(out, Address(obj,
5077 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5078 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005079 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005080 }
5081 break;
5082 }
5083
Roland Levillain7c1559a2015-12-15 10:55:36 +00005084 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005085 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005086 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005087 if (index.IsConstant()) {
5088 __ movl(out, Address(obj,
5089 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5090 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005091 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005092 }
5093 break;
5094 }
5095
Roland Levillain7c1559a2015-12-15 10:55:36 +00005096 case Primitive::kPrimNot: {
5097 static_assert(
5098 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5099 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5100 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5101 // /* HeapReference<Object> */ out =
5102 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5103 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5104 Location temp = locations->GetTemp(0);
5105 // Note that a potential implicit null check is handled in this
5106 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5107 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5108 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5109 } else {
5110 Register out = out_loc.AsRegister<Register>();
5111 if (index.IsConstant()) {
5112 uint32_t offset =
5113 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5114 __ movl(out, Address(obj, offset));
5115 codegen_->MaybeRecordImplicitNullCheck(instruction);
5116 // If read barriers are enabled, emit read barriers other than
5117 // Baker's using a slow path (and also unpoison the loaded
5118 // reference, if heap poisoning is enabled).
5119 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5120 } else {
5121 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5122 codegen_->MaybeRecordImplicitNullCheck(instruction);
5123 // If read barriers are enabled, emit read barriers other than
5124 // Baker's using a slow path (and also unpoison the loaded
5125 // reference, if heap poisoning is enabled).
5126 codegen_->MaybeGenerateReadBarrierSlow(
5127 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5128 }
5129 }
5130 break;
5131 }
5132
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005133 case Primitive::kPrimLong: {
5134 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005135 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005136 if (index.IsConstant()) {
5137 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005138 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005139 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005140 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005141 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005142 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005143 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005144 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005145 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005146 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005147 }
5148 break;
5149 }
5150
Mark Mendell7c8d0092015-01-26 11:21:33 -05005151 case Primitive::kPrimFloat: {
5152 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005153 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005154 if (index.IsConstant()) {
5155 __ movss(out, Address(obj,
5156 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5157 } else {
5158 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5159 }
5160 break;
5161 }
5162
5163 case Primitive::kPrimDouble: {
5164 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005165 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005166 if (index.IsConstant()) {
5167 __ movsd(out, Address(obj,
5168 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5169 } else {
5170 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5171 }
5172 break;
5173 }
5174
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005175 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005176 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005177 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005178 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005179
Roland Levillain7c1559a2015-12-15 10:55:36 +00005180 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5181 // Potential implicit null checks, in the case of reference or
5182 // long arrays, are handled in the previous switch statement.
5183 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005184 codegen_->MaybeRecordImplicitNullCheck(instruction);
5185 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005186}
5187
5188void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005189 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005190
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005191 bool needs_write_barrier =
5192 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005193 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5194 bool object_array_set_with_read_barrier =
5195 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005196
Nicolas Geoffray39468442014-09-02 15:17:15 +01005197 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5198 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005199 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5200 LocationSummary::kCallOnSlowPath :
5201 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005202
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005203 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5204 || (value_type == Primitive::kPrimByte);
5205 // We need the inputs to be different than the output in case of long operation.
5206 // In case of a byte operation, the register allocator does not support multiple
5207 // inputs that die at entry with one in a specific register.
5208 locations->SetInAt(0, Location::RequiresRegister());
5209 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5210 if (is_byte_type) {
5211 // Ensure the value is in a byte register.
5212 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5213 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005214 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005215 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005216 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5217 }
5218 if (needs_write_barrier) {
5219 // Temporary registers for the write barrier.
5220 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5221 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005222 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005223 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005224}
5225
5226void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5227 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005228 Location array_loc = locations->InAt(0);
5229 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005230 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005231 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005232 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005233 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5234 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5235 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005236 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005237 bool needs_write_barrier =
5238 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005239
5240 switch (value_type) {
5241 case Primitive::kPrimBoolean:
5242 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005243 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5244 Address address = index.IsConstant()
5245 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5246 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5247 if (value.IsRegister()) {
5248 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005249 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005250 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005251 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005252 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005253 break;
5254 }
5255
5256 case Primitive::kPrimShort:
5257 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005258 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5259 Address address = index.IsConstant()
5260 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5261 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5262 if (value.IsRegister()) {
5263 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005264 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005265 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005266 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005267 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005268 break;
5269 }
5270
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005271 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005272 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5273 Address address = index.IsConstant()
5274 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5275 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005276
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005277 if (!value.IsRegister()) {
5278 // Just setting null.
5279 DCHECK(instruction->InputAt(2)->IsNullConstant());
5280 DCHECK(value.IsConstant()) << value;
5281 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005282 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005283 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005284 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005285 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005286 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005287
5288 DCHECK(needs_write_barrier);
5289 Register register_value = value.AsRegister<Register>();
5290 NearLabel done, not_null, do_put;
5291 SlowPathCode* slow_path = nullptr;
5292 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005293 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005294 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5295 codegen_->AddSlowPath(slow_path);
5296 if (instruction->GetValueCanBeNull()) {
5297 __ testl(register_value, register_value);
5298 __ j(kNotEqual, &not_null);
5299 __ movl(address, Immediate(0));
5300 codegen_->MaybeRecordImplicitNullCheck(instruction);
5301 __ jmp(&done);
5302 __ Bind(&not_null);
5303 }
5304
Roland Levillain0d5a2812015-11-13 10:07:31 +00005305 if (kEmitCompilerReadBarrier) {
5306 // When read barriers are enabled, the type checking
5307 // instrumentation requires two read barriers:
5308 //
5309 // __ movl(temp2, temp);
5310 // // /* HeapReference<Class> */ temp = temp->component_type_
5311 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005312 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005313 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5314 //
5315 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5316 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005317 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005318 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5319 //
5320 // __ cmpl(temp, temp2);
5321 //
5322 // However, the second read barrier may trash `temp`, as it
5323 // is a temporary register, and as such would not be saved
5324 // along with live registers before calling the runtime (nor
5325 // restored afterwards). So in this case, we bail out and
5326 // delegate the work to the array set slow path.
5327 //
5328 // TODO: Extend the register allocator to support a new
5329 // "(locally) live temp" location so as to avoid always
5330 // going into the slow path when read barriers are enabled.
5331 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005332 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005333 // /* HeapReference<Class> */ temp = array->klass_
5334 __ movl(temp, Address(array, class_offset));
5335 codegen_->MaybeRecordImplicitNullCheck(instruction);
5336 __ MaybeUnpoisonHeapReference(temp);
5337
5338 // /* HeapReference<Class> */ temp = temp->component_type_
5339 __ movl(temp, Address(temp, component_offset));
5340 // If heap poisoning is enabled, no need to unpoison `temp`
5341 // nor the object reference in `register_value->klass`, as
5342 // we are comparing two poisoned references.
5343 __ cmpl(temp, Address(register_value, class_offset));
5344
5345 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5346 __ j(kEqual, &do_put);
5347 // If heap poisoning is enabled, the `temp` reference has
5348 // not been unpoisoned yet; unpoison it now.
5349 __ MaybeUnpoisonHeapReference(temp);
5350
5351 // /* HeapReference<Class> */ temp = temp->super_class_
5352 __ movl(temp, Address(temp, super_offset));
5353 // If heap poisoning is enabled, no need to unpoison
5354 // `temp`, as we are comparing against null below.
5355 __ testl(temp, temp);
5356 __ j(kNotEqual, slow_path->GetEntryLabel());
5357 __ Bind(&do_put);
5358 } else {
5359 __ j(kNotEqual, slow_path->GetEntryLabel());
5360 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005361 }
5362 }
5363
5364 if (kPoisonHeapReferences) {
5365 __ movl(temp, register_value);
5366 __ PoisonHeapReference(temp);
5367 __ movl(address, temp);
5368 } else {
5369 __ movl(address, register_value);
5370 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005371 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005372 codegen_->MaybeRecordImplicitNullCheck(instruction);
5373 }
5374
5375 Register card = locations->GetTemp(1).AsRegister<Register>();
5376 codegen_->MarkGCCard(
5377 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5378 __ Bind(&done);
5379
5380 if (slow_path != nullptr) {
5381 __ Bind(slow_path->GetExitLabel());
5382 }
5383
5384 break;
5385 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005386
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005387 case Primitive::kPrimInt: {
5388 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5389 Address address = index.IsConstant()
5390 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5391 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5392 if (value.IsRegister()) {
5393 __ movl(address, value.AsRegister<Register>());
5394 } else {
5395 DCHECK(value.IsConstant()) << value;
5396 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5397 __ movl(address, Immediate(v));
5398 }
5399 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005400 break;
5401 }
5402
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005403 case Primitive::kPrimLong: {
5404 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005405 if (index.IsConstant()) {
5406 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005407 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005408 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005409 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005410 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005411 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005412 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005413 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005414 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005415 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005416 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005417 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005418 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005419 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005420 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005421 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, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005424 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005425 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005426 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005427 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005428 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005429 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005430 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005431 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005432 Immediate(High32Bits(val)));
5433 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005434 }
5435 break;
5436 }
5437
Mark Mendell7c8d0092015-01-26 11:21:33 -05005438 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005439 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5440 Address address = index.IsConstant()
5441 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5442 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005443 if (value.IsFpuRegister()) {
5444 __ movss(address, value.AsFpuRegister<XmmRegister>());
5445 } else {
5446 DCHECK(value.IsConstant());
5447 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5448 __ movl(address, Immediate(v));
5449 }
5450 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005451 break;
5452 }
5453
5454 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005455 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5456 Address address = index.IsConstant()
5457 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5458 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005459 if (value.IsFpuRegister()) {
5460 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5461 } else {
5462 DCHECK(value.IsConstant());
5463 Address address_hi = index.IsConstant() ?
5464 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5465 offset + kX86WordSize) :
5466 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5467 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5468 __ movl(address, Immediate(Low32Bits(v)));
5469 codegen_->MaybeRecordImplicitNullCheck(instruction);
5470 __ movl(address_hi, Immediate(High32Bits(v)));
5471 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005472 break;
5473 }
5474
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005475 case Primitive::kPrimVoid:
5476 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005477 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005478 }
5479}
5480
5481void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5482 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005483 locations->SetInAt(0, Location::RequiresRegister());
5484 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005485}
5486
5487void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5488 LocationSummary* locations = instruction->GetLocations();
5489 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005490 Register obj = locations->InAt(0).AsRegister<Register>();
5491 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005492 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005493 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005494}
5495
5496void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005497 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5498 ? LocationSummary::kCallOnSlowPath
5499 : LocationSummary::kNoCall;
5500 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005501 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005502 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005503 if (instruction->HasUses()) {
5504 locations->SetOut(Location::SameAsFirstInput());
5505 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005506}
5507
5508void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5509 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005510 Location index_loc = locations->InAt(0);
5511 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005512 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005513 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005514
Mark Mendell99dbd682015-04-22 16:18:52 -04005515 if (length_loc.IsConstant()) {
5516 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5517 if (index_loc.IsConstant()) {
5518 // BCE will remove the bounds check if we are guarenteed to pass.
5519 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5520 if (index < 0 || index >= length) {
5521 codegen_->AddSlowPath(slow_path);
5522 __ jmp(slow_path->GetEntryLabel());
5523 } else {
5524 // Some optimization after BCE may have generated this, and we should not
5525 // generate a bounds check if it is a valid range.
5526 }
5527 return;
5528 }
5529
5530 // We have to reverse the jump condition because the length is the constant.
5531 Register index_reg = index_loc.AsRegister<Register>();
5532 __ cmpl(index_reg, Immediate(length));
5533 codegen_->AddSlowPath(slow_path);
5534 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005535 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005536 Register length = length_loc.AsRegister<Register>();
5537 if (index_loc.IsConstant()) {
5538 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5539 __ cmpl(length, Immediate(value));
5540 } else {
5541 __ cmpl(length, index_loc.AsRegister<Register>());
5542 }
5543 codegen_->AddSlowPath(slow_path);
5544 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005545 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005546}
5547
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005548void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005549 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005550}
5551
5552void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005553 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5554}
5555
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005556void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5557 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5558}
5559
5560void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005561 HBasicBlock* block = instruction->GetBlock();
5562 if (block->GetLoopInformation() != nullptr) {
5563 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5564 // The back edge will generate the suspend check.
5565 return;
5566 }
5567 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5568 // The goto will generate the suspend check.
5569 return;
5570 }
5571 GenerateSuspendCheck(instruction, nullptr);
5572}
5573
5574void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5575 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005576 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005577 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5578 if (slow_path == nullptr) {
5579 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5580 instruction->SetSlowPath(slow_path);
5581 codegen_->AddSlowPath(slow_path);
5582 if (successor != nullptr) {
5583 DCHECK(successor->IsLoopHeader());
5584 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5585 }
5586 } else {
5587 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5588 }
5589
Roland Levillain7c1559a2015-12-15 10:55:36 +00005590 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5591 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005592 if (successor == nullptr) {
5593 __ j(kNotEqual, slow_path->GetEntryLabel());
5594 __ Bind(slow_path->GetReturnLabel());
5595 } else {
5596 __ j(kEqual, codegen_->GetLabelOf(successor));
5597 __ jmp(slow_path->GetEntryLabel());
5598 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005599}
5600
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005601X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5602 return codegen_->GetAssembler();
5603}
5604
Mark Mendell7c8d0092015-01-26 11:21:33 -05005605void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005606 ScratchRegisterScope ensure_scratch(
5607 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5608 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5609 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5610 __ movl(temp_reg, Address(ESP, src + stack_offset));
5611 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005612}
5613
5614void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005615 ScratchRegisterScope ensure_scratch(
5616 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5617 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5618 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5619 __ movl(temp_reg, Address(ESP, src + stack_offset));
5620 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5621 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5622 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005623}
5624
5625void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005626 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005627 Location source = move->GetSource();
5628 Location destination = move->GetDestination();
5629
5630 if (source.IsRegister()) {
5631 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005632 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005633 } else if (destination.IsFpuRegister()) {
5634 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005635 } else {
5636 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005637 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005638 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005639 } else if (source.IsRegisterPair()) {
5640 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5641 // Create stack space for 2 elements.
5642 __ subl(ESP, Immediate(2 * elem_size));
5643 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5644 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5645 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5646 // And remove the temporary stack space we allocated.
5647 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005648 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005649 if (destination.IsRegister()) {
5650 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5651 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005652 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005653 } else if (destination.IsRegisterPair()) {
5654 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5655 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5656 __ psrlq(src_reg, Immediate(32));
5657 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005658 } else if (destination.IsStackSlot()) {
5659 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5660 } else {
5661 DCHECK(destination.IsDoubleStackSlot());
5662 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5663 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005664 } else if (source.IsStackSlot()) {
5665 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005666 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005667 } else if (destination.IsFpuRegister()) {
5668 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005669 } else {
5670 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005671 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5672 }
5673 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005674 if (destination.IsRegisterPair()) {
5675 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5676 __ movl(destination.AsRegisterPairHigh<Register>(),
5677 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5678 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005679 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5680 } else {
5681 DCHECK(destination.IsDoubleStackSlot()) << destination;
5682 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005683 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005684 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005685 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005686 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005687 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005688 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005689 if (value == 0) {
5690 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5691 } else {
5692 __ movl(destination.AsRegister<Register>(), Immediate(value));
5693 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005694 } else {
5695 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005696 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005697 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005698 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005699 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005700 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005701 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005702 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005703 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5704 if (value == 0) {
5705 // Easy handling of 0.0.
5706 __ xorps(dest, dest);
5707 } else {
5708 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005709 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5710 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5711 __ movl(temp, Immediate(value));
5712 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005713 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005714 } else {
5715 DCHECK(destination.IsStackSlot()) << destination;
5716 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5717 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005718 } else if (constant->IsLongConstant()) {
5719 int64_t value = constant->AsLongConstant()->GetValue();
5720 int32_t low_value = Low32Bits(value);
5721 int32_t high_value = High32Bits(value);
5722 Immediate low(low_value);
5723 Immediate high(high_value);
5724 if (destination.IsDoubleStackSlot()) {
5725 __ movl(Address(ESP, destination.GetStackIndex()), low);
5726 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5727 } else {
5728 __ movl(destination.AsRegisterPairLow<Register>(), low);
5729 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5730 }
5731 } else {
5732 DCHECK(constant->IsDoubleConstant());
5733 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005734 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005735 int32_t low_value = Low32Bits(value);
5736 int32_t high_value = High32Bits(value);
5737 Immediate low(low_value);
5738 Immediate high(high_value);
5739 if (destination.IsFpuRegister()) {
5740 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5741 if (value == 0) {
5742 // Easy handling of 0.0.
5743 __ xorpd(dest, dest);
5744 } else {
5745 __ pushl(high);
5746 __ pushl(low);
5747 __ movsd(dest, Address(ESP, 0));
5748 __ addl(ESP, Immediate(8));
5749 }
5750 } else {
5751 DCHECK(destination.IsDoubleStackSlot()) << destination;
5752 __ movl(Address(ESP, destination.GetStackIndex()), low);
5753 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5754 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005755 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005756 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005757 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005758 }
5759}
5760
Mark Mendella5c19ce2015-04-01 12:51:05 -04005761void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005762 Register suggested_scratch = reg == EAX ? EBX : EAX;
5763 ScratchRegisterScope ensure_scratch(
5764 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5765
5766 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5767 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5768 __ movl(Address(ESP, mem + stack_offset), reg);
5769 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005770}
5771
Mark Mendell7c8d0092015-01-26 11:21:33 -05005772void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005773 ScratchRegisterScope ensure_scratch(
5774 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5775
5776 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5777 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5778 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5779 __ movss(Address(ESP, mem + stack_offset), reg);
5780 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005781}
5782
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005783void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005784 ScratchRegisterScope ensure_scratch1(
5785 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005786
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005787 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5788 ScratchRegisterScope ensure_scratch2(
5789 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005790
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005791 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5792 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5793 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5794 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5795 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5796 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005797}
5798
5799void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005800 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005801 Location source = move->GetSource();
5802 Location destination = move->GetDestination();
5803
5804 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005805 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5806 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5807 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5808 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5809 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005810 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005811 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005812 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005813 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005814 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5815 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005816 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5817 // Use XOR Swap algorithm to avoid a temporary.
5818 DCHECK_NE(source.reg(), destination.reg());
5819 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5820 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5821 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5822 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5823 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5824 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5825 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005826 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5827 // Take advantage of the 16 bytes in the XMM register.
5828 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5829 Address stack(ESP, destination.GetStackIndex());
5830 // Load the double into the high doubleword.
5831 __ movhpd(reg, stack);
5832
5833 // Store the low double into the destination.
5834 __ movsd(stack, reg);
5835
5836 // Move the high double to the low double.
5837 __ psrldq(reg, Immediate(8));
5838 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5839 // Take advantage of the 16 bytes in the XMM register.
5840 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5841 Address stack(ESP, source.GetStackIndex());
5842 // Load the double into the high doubleword.
5843 __ movhpd(reg, stack);
5844
5845 // Store the low double into the destination.
5846 __ movsd(stack, reg);
5847
5848 // Move the high double to the low double.
5849 __ psrldq(reg, Immediate(8));
5850 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5851 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5852 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005853 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005854 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005855 }
5856}
5857
5858void ParallelMoveResolverX86::SpillScratch(int reg) {
5859 __ pushl(static_cast<Register>(reg));
5860}
5861
5862void ParallelMoveResolverX86::RestoreScratch(int reg) {
5863 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005864}
5865
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005866void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005867 InvokeRuntimeCallingConvention calling_convention;
5868 CodeGenerator::CreateLoadClassLocationSummary(
5869 cls,
5870 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005871 Location::RegisterLocation(EAX),
5872 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005873}
5874
5875void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005876 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005877 if (cls->NeedsAccessCheck()) {
5878 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5879 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5880 cls,
5881 cls->GetDexPc(),
5882 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005883 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005884 return;
5885 }
5886
Roland Levillain0d5a2812015-11-13 10:07:31 +00005887 Location out_loc = locations->Out();
5888 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005889 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005890
Calin Juravle580b6092015-10-06 17:35:58 +01005891 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005892 DCHECK(!cls->CanCallRuntime());
5893 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005894 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5895 GenerateGcRootFieldLoad(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005896 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005897 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005898 // /* GcRoot<mirror::Class>[] */ out =
5899 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5900 __ movl(out, Address(current_method,
5901 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005902 // /* GcRoot<mirror::Class> */ out = out[type_index]
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005903 GenerateGcRootFieldLoad(
5904 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005905
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005906 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5907 DCHECK(cls->CanCallRuntime());
5908 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5909 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5910 codegen_->AddSlowPath(slow_path);
5911
5912 if (!cls->IsInDexCache()) {
5913 __ testl(out, out);
5914 __ j(kEqual, slow_path->GetEntryLabel());
5915 }
5916
5917 if (cls->MustGenerateClinitCheck()) {
5918 GenerateClassInitializationCheck(slow_path, out);
5919 } else {
5920 __ Bind(slow_path->GetExitLabel());
5921 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005922 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005923 }
5924}
5925
5926void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5927 LocationSummary* locations =
5928 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5929 locations->SetInAt(0, Location::RequiresRegister());
5930 if (check->HasUses()) {
5931 locations->SetOut(Location::SameAsFirstInput());
5932 }
5933}
5934
5935void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005936 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005937 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005938 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005939 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005940 GenerateClassInitializationCheck(slow_path,
5941 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005942}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005943
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005944void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005945 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005946 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5947 Immediate(mirror::Class::kStatusInitialized));
5948 __ j(kLess, slow_path->GetEntryLabel());
5949 __ Bind(slow_path->GetExitLabel());
5950 // No need for memory fence, thanks to the X86 memory model.
5951}
5952
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005953HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
5954 HLoadString::LoadKind desired_string_load_kind) {
5955 if (kEmitCompilerReadBarrier) {
5956 switch (desired_string_load_kind) {
5957 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5958 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5959 case HLoadString::LoadKind::kBootImageAddress:
5960 // TODO: Implement for read barrier.
5961 return HLoadString::LoadKind::kDexCacheViaMethod;
5962 default:
5963 break;
5964 }
5965 }
5966 switch (desired_string_load_kind) {
5967 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5968 DCHECK(!GetCompilerOptions().GetCompilePic());
5969 break;
5970 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5971 DCHECK(GetCompilerOptions().GetCompilePic());
5972 FALLTHROUGH_INTENDED;
5973 case HLoadString::LoadKind::kDexCachePcRelative:
5974 DCHECK(!Runtime::Current()->UseJit()); // Note: boot image is also non-JIT.
5975 // We disable pc-relative load when there is an irreducible loop, as the optimization
5976 // is incompatible with it.
5977 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5978 // with irreducible loops.
5979 if (GetGraph()->HasIrreducibleLoops()) {
5980 return HLoadString::LoadKind::kDexCacheViaMethod;
5981 }
5982 break;
5983 case HLoadString::LoadKind::kBootImageAddress:
5984 break;
5985 case HLoadString::LoadKind::kDexCacheAddress:
5986 DCHECK(Runtime::Current()->UseJit());
5987 break;
5988 case HLoadString::LoadKind::kDexCacheViaMethod:
5989 break;
5990 }
5991 return desired_string_load_kind;
5992}
5993
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005994void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005995 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005996 ? LocationSummary::kCallOnSlowPath
5997 : LocationSummary::kNoCall;
5998 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005999 HLoadString::LoadKind load_kind = load->GetLoadKind();
6000 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6001 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6002 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6003 locations->SetInAt(0, Location::RequiresRegister());
6004 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006005 locations->SetOut(Location::RequiresRegister());
6006}
6007
6008void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006009 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006010 Location out_loc = locations->Out();
6011 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006012
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006013 switch (load->GetLoadKind()) {
6014 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6015 DCHECK(!kEmitCompilerReadBarrier);
6016 __ movl(out, Immediate(/* placeholder */ 0));
6017 codegen_->RecordStringPatch(load);
6018 return; // No dex cache slow path.
6019 }
6020 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6021 DCHECK(!kEmitCompilerReadBarrier);
6022 Register method_address = locations->InAt(0).AsRegister<Register>();
6023 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6024 codegen_->RecordStringPatch(load);
6025 return; // No dex cache slow path.
6026 }
6027 case HLoadString::LoadKind::kBootImageAddress: {
6028 DCHECK(!kEmitCompilerReadBarrier);
6029 DCHECK_NE(load->GetAddress(), 0u);
6030 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6031 __ movl(out, Immediate(address));
6032 codegen_->RecordSimplePatch();
6033 return; // No dex cache slow path.
6034 }
6035 case HLoadString::LoadKind::kDexCacheAddress: {
6036 DCHECK_NE(load->GetAddress(), 0u);
6037 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6038 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6039 break;
6040 }
6041 case HLoadString::LoadKind::kDexCachePcRelative: {
6042 Register base_reg = locations->InAt(0).AsRegister<Register>();
6043 uint32_t offset = load->GetDexCacheElementOffset();
6044 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
6045 GenerateGcRootFieldLoad(
6046 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6047 break;
6048 }
6049 case HLoadString::LoadKind::kDexCacheViaMethod: {
6050 Register current_method = locations->InAt(0).AsRegister<Register>();
6051
6052 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6053 GenerateGcRootFieldLoad(
6054 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6055
6056 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6057 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6058 // /* GcRoot<mirror::String> */ out = out[string_index]
6059 GenerateGcRootFieldLoad(
6060 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6061 break;
6062 }
6063 default:
6064 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6065 UNREACHABLE();
6066 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006067
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006068 if (!load->IsInDexCache()) {
6069 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6070 codegen_->AddSlowPath(slow_path);
6071 __ testl(out, out);
6072 __ j(kEqual, slow_path->GetEntryLabel());
6073 __ Bind(slow_path->GetExitLabel());
6074 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006075}
6076
David Brazdilcb1c0552015-08-04 16:22:25 +01006077static Address GetExceptionTlsAddress() {
6078 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6079}
6080
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006081void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6082 LocationSummary* locations =
6083 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6084 locations->SetOut(Location::RequiresRegister());
6085}
6086
6087void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006088 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6089}
6090
6091void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6092 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6093}
6094
6095void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6096 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006097}
6098
6099void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6100 LocationSummary* locations =
6101 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6102 InvokeRuntimeCallingConvention calling_convention;
6103 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6104}
6105
6106void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006107 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6108 instruction,
6109 instruction->GetDexPc(),
6110 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006111 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006112}
6113
Roland Levillain7c1559a2015-12-15 10:55:36 +00006114static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6115 return kEmitCompilerReadBarrier &&
6116 (kUseBakerReadBarrier ||
6117 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6118 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6119 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6120}
6121
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006122void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006123 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006124 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6125 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006126 case TypeCheckKind::kExactCheck:
6127 case TypeCheckKind::kAbstractClassCheck:
6128 case TypeCheckKind::kClassHierarchyCheck:
6129 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006130 call_kind =
6131 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006132 break;
6133 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006134 case TypeCheckKind::kUnresolvedCheck:
6135 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006136 call_kind = LocationSummary::kCallOnSlowPath;
6137 break;
6138 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006139
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006140 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006141 locations->SetInAt(0, Location::RequiresRegister());
6142 locations->SetInAt(1, Location::Any());
6143 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6144 locations->SetOut(Location::RequiresRegister());
6145 // When read barriers are enabled, we need a temporary register for
6146 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006147 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006148 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006149 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006150}
6151
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006152void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006153 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006154 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006155 Location obj_loc = locations->InAt(0);
6156 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006157 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006158 Location out_loc = locations->Out();
6159 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006160 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006161 locations->GetTemp(0) :
6162 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006163 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006164 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6165 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6166 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006167 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006168 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006169
6170 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006171 // Avoid null check if we know obj is not null.
6172 if (instruction->MustDoNullCheck()) {
6173 __ testl(obj, obj);
6174 __ j(kEqual, &zero);
6175 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006176
Roland Levillain0d5a2812015-11-13 10:07:31 +00006177 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006178 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006179
Roland Levillain7c1559a2015-12-15 10:55:36 +00006180 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006181 case TypeCheckKind::kExactCheck: {
6182 if (cls.IsRegister()) {
6183 __ cmpl(out, cls.AsRegister<Register>());
6184 } else {
6185 DCHECK(cls.IsStackSlot()) << cls;
6186 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6187 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006188
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006189 // Classes must be equal for the instanceof to succeed.
6190 __ j(kNotEqual, &zero);
6191 __ movl(out, Immediate(1));
6192 __ jmp(&done);
6193 break;
6194 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006195
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006196 case TypeCheckKind::kAbstractClassCheck: {
6197 // If the class is abstract, we eagerly fetch the super class of the
6198 // object to avoid doing a comparison we know will fail.
6199 NearLabel loop;
6200 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006201 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006202 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006203 __ testl(out, out);
6204 // If `out` is null, we use it for the result, and jump to `done`.
6205 __ j(kEqual, &done);
6206 if (cls.IsRegister()) {
6207 __ cmpl(out, cls.AsRegister<Register>());
6208 } else {
6209 DCHECK(cls.IsStackSlot()) << cls;
6210 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6211 }
6212 __ j(kNotEqual, &loop);
6213 __ movl(out, Immediate(1));
6214 if (zero.IsLinked()) {
6215 __ jmp(&done);
6216 }
6217 break;
6218 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006219
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006220 case TypeCheckKind::kClassHierarchyCheck: {
6221 // Walk over the class hierarchy to find a match.
6222 NearLabel loop, success;
6223 __ Bind(&loop);
6224 if (cls.IsRegister()) {
6225 __ cmpl(out, cls.AsRegister<Register>());
6226 } else {
6227 DCHECK(cls.IsStackSlot()) << cls;
6228 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6229 }
6230 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006231 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006232 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006233 __ testl(out, out);
6234 __ j(kNotEqual, &loop);
6235 // If `out` is null, we use it for the result, and jump to `done`.
6236 __ jmp(&done);
6237 __ Bind(&success);
6238 __ movl(out, Immediate(1));
6239 if (zero.IsLinked()) {
6240 __ jmp(&done);
6241 }
6242 break;
6243 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006244
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006245 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006246 // Do an exact check.
6247 NearLabel exact_check;
6248 if (cls.IsRegister()) {
6249 __ cmpl(out, cls.AsRegister<Register>());
6250 } else {
6251 DCHECK(cls.IsStackSlot()) << cls;
6252 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6253 }
6254 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006255 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006256 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006257 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006258 __ testl(out, out);
6259 // If `out` is null, we use it for the result, and jump to `done`.
6260 __ j(kEqual, &done);
6261 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6262 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006263 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006264 __ movl(out, Immediate(1));
6265 __ jmp(&done);
6266 break;
6267 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006268
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006269 case TypeCheckKind::kArrayCheck: {
6270 if (cls.IsRegister()) {
6271 __ cmpl(out, cls.AsRegister<Register>());
6272 } else {
6273 DCHECK(cls.IsStackSlot()) << cls;
6274 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6275 }
6276 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006277 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6278 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006279 codegen_->AddSlowPath(slow_path);
6280 __ j(kNotEqual, slow_path->GetEntryLabel());
6281 __ movl(out, Immediate(1));
6282 if (zero.IsLinked()) {
6283 __ jmp(&done);
6284 }
6285 break;
6286 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006287
Calin Juravle98893e12015-10-02 21:05:03 +01006288 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006289 case TypeCheckKind::kInterfaceCheck: {
6290 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006291 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006292 // cases.
6293 //
6294 // We cannot directly call the InstanceofNonTrivial runtime
6295 // entry point without resorting to a type checking slow path
6296 // here (i.e. by calling InvokeRuntime directly), as it would
6297 // require to assign fixed registers for the inputs of this
6298 // HInstanceOf instruction (following the runtime calling
6299 // convention), which might be cluttered by the potential first
6300 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006301 //
6302 // TODO: Introduce a new runtime entry point taking the object
6303 // to test (instead of its class) as argument, and let it deal
6304 // with the read barrier issues. This will let us refactor this
6305 // case of the `switch` code as it was previously (with a direct
6306 // call to the runtime not using a type checking slow path).
6307 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006308 DCHECK(locations->OnlyCallsOnSlowPath());
6309 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6310 /* is_fatal */ false);
6311 codegen_->AddSlowPath(slow_path);
6312 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313 if (zero.IsLinked()) {
6314 __ jmp(&done);
6315 }
6316 break;
6317 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006318 }
6319
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006320 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006321 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006322 __ xorl(out, out);
6323 }
6324
6325 if (done.IsLinked()) {
6326 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006327 }
6328
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006329 if (slow_path != nullptr) {
6330 __ Bind(slow_path->GetExitLabel());
6331 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006332}
6333
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006334void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006335 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6336 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006337 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6338 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006339 case TypeCheckKind::kExactCheck:
6340 case TypeCheckKind::kAbstractClassCheck:
6341 case TypeCheckKind::kClassHierarchyCheck:
6342 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006343 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6344 LocationSummary::kCallOnSlowPath :
6345 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006346 break;
6347 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006348 case TypeCheckKind::kUnresolvedCheck:
6349 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006350 call_kind = LocationSummary::kCallOnSlowPath;
6351 break;
6352 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006353 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6354 locations->SetInAt(0, Location::RequiresRegister());
6355 locations->SetInAt(1, Location::Any());
6356 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6357 locations->AddTemp(Location::RequiresRegister());
6358 // When read barriers are enabled, we need an additional temporary
6359 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006360 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006361 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006362 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006363}
6364
6365void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006366 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006367 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006368 Location obj_loc = locations->InAt(0);
6369 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006370 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006371 Location temp_loc = locations->GetTemp(0);
6372 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006373 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006374 locations->GetTemp(1) :
6375 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006376 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6377 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6378 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6379 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006380
Roland Levillain0d5a2812015-11-13 10:07:31 +00006381 bool is_type_check_slow_path_fatal =
6382 (type_check_kind == TypeCheckKind::kExactCheck ||
6383 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6384 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6385 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6386 !instruction->CanThrowIntoCatchBlock();
6387 SlowPathCode* type_check_slow_path =
6388 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6389 is_type_check_slow_path_fatal);
6390 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006391
Roland Levillain0d5a2812015-11-13 10:07:31 +00006392 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006393 // Avoid null check if we know obj is not null.
6394 if (instruction->MustDoNullCheck()) {
6395 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006396 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006397 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006398
Roland Levillain0d5a2812015-11-13 10:07:31 +00006399 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006400 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006401
Roland Levillain0d5a2812015-11-13 10:07:31 +00006402 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006403 case TypeCheckKind::kExactCheck:
6404 case TypeCheckKind::kArrayCheck: {
6405 if (cls.IsRegister()) {
6406 __ cmpl(temp, cls.AsRegister<Register>());
6407 } else {
6408 DCHECK(cls.IsStackSlot()) << cls;
6409 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6410 }
6411 // Jump to slow path for throwing the exception or doing a
6412 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006413 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006414 break;
6415 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006416
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006417 case TypeCheckKind::kAbstractClassCheck: {
6418 // If the class is abstract, we eagerly fetch the super class of the
6419 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006420 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006421 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006422 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006423 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006424
6425 // If the class reference currently in `temp` is not null, jump
6426 // to the `compare_classes` label to compare it with the checked
6427 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006428 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006429 __ j(kNotEqual, &compare_classes);
6430 // Otherwise, jump to the slow path to throw the exception.
6431 //
6432 // But before, move back the object's class into `temp` before
6433 // going into the slow path, as it has been overwritten in the
6434 // meantime.
6435 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006436 GenerateReferenceLoadTwoRegisters(
6437 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006438 __ jmp(type_check_slow_path->GetEntryLabel());
6439
6440 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006441 if (cls.IsRegister()) {
6442 __ cmpl(temp, cls.AsRegister<Register>());
6443 } else {
6444 DCHECK(cls.IsStackSlot()) << cls;
6445 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6446 }
6447 __ j(kNotEqual, &loop);
6448 break;
6449 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006450
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006451 case TypeCheckKind::kClassHierarchyCheck: {
6452 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006453 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006454 __ Bind(&loop);
6455 if (cls.IsRegister()) {
6456 __ cmpl(temp, cls.AsRegister<Register>());
6457 } else {
6458 DCHECK(cls.IsStackSlot()) << cls;
6459 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6460 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006461 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006462
Roland Levillain0d5a2812015-11-13 10:07:31 +00006463 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006464 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006465
6466 // If the class reference currently in `temp` is not null, jump
6467 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006468 __ testl(temp, temp);
6469 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006470 // Otherwise, jump to the slow path to throw the exception.
6471 //
6472 // But before, move back the object's class into `temp` before
6473 // going into the slow path, as it has been overwritten in the
6474 // meantime.
6475 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006476 GenerateReferenceLoadTwoRegisters(
6477 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006478 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006479 break;
6480 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006481
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006482 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006483 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006484 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006485 if (cls.IsRegister()) {
6486 __ cmpl(temp, cls.AsRegister<Register>());
6487 } else {
6488 DCHECK(cls.IsStackSlot()) << cls;
6489 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6490 }
6491 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006492
6493 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006494 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006495 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006496
6497 // If the component type is not null (i.e. the object is indeed
6498 // an array), jump to label `check_non_primitive_component_type`
6499 // to further check that this component type is not a primitive
6500 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006501 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006502 __ j(kNotEqual, &check_non_primitive_component_type);
6503 // Otherwise, jump to the slow path to throw the exception.
6504 //
6505 // But before, move back the object's class into `temp` before
6506 // going into the slow path, as it has been overwritten in the
6507 // meantime.
6508 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006509 GenerateReferenceLoadTwoRegisters(
6510 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006511 __ jmp(type_check_slow_path->GetEntryLabel());
6512
6513 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006514 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006515 __ j(kEqual, &done);
6516 // Same comment as above regarding `temp` and the slow path.
6517 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006518 GenerateReferenceLoadTwoRegisters(
6519 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006520 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006521 break;
6522 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006523
Calin Juravle98893e12015-10-02 21:05:03 +01006524 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006525 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006526 // We always go into the type check slow path for the unresolved
6527 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006528 //
6529 // We cannot directly call the CheckCast runtime entry point
6530 // without resorting to a type checking slow path here (i.e. by
6531 // calling InvokeRuntime directly), as it would require to
6532 // assign fixed registers for the inputs of this HInstanceOf
6533 // instruction (following the runtime calling convention), which
6534 // might be cluttered by the potential first read barrier
6535 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006536 //
6537 // TODO: Introduce a new runtime entry point taking the object
6538 // to test (instead of its class) as argument, and let it deal
6539 // with the read barrier issues. This will let us refactor this
6540 // case of the `switch` code as it was previously (with a direct
6541 // call to the runtime not using a type checking slow path).
6542 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006543 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006544 break;
6545 }
6546 __ Bind(&done);
6547
Roland Levillain0d5a2812015-11-13 10:07:31 +00006548 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006549}
6550
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006551void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6552 LocationSummary* locations =
6553 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6554 InvokeRuntimeCallingConvention calling_convention;
6555 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6556}
6557
6558void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006559 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6560 : QUICK_ENTRY_POINT(pUnlockObject),
6561 instruction,
6562 instruction->GetDexPc(),
6563 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006564 if (instruction->IsEnter()) {
6565 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6566 } else {
6567 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6568 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006569}
6570
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006571void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6572void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6573void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6574
6575void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6576 LocationSummary* locations =
6577 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6578 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6579 || instruction->GetResultType() == Primitive::kPrimLong);
6580 locations->SetInAt(0, Location::RequiresRegister());
6581 locations->SetInAt(1, Location::Any());
6582 locations->SetOut(Location::SameAsFirstInput());
6583}
6584
6585void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6586 HandleBitwiseOperation(instruction);
6587}
6588
6589void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6590 HandleBitwiseOperation(instruction);
6591}
6592
6593void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6594 HandleBitwiseOperation(instruction);
6595}
6596
6597void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6598 LocationSummary* locations = instruction->GetLocations();
6599 Location first = locations->InAt(0);
6600 Location second = locations->InAt(1);
6601 DCHECK(first.Equals(locations->Out()));
6602
6603 if (instruction->GetResultType() == Primitive::kPrimInt) {
6604 if (second.IsRegister()) {
6605 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006606 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006607 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006608 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006609 } else {
6610 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006611 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006612 }
6613 } else if (second.IsConstant()) {
6614 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006615 __ andl(first.AsRegister<Register>(),
6616 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006617 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006618 __ orl(first.AsRegister<Register>(),
6619 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006620 } else {
6621 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006622 __ xorl(first.AsRegister<Register>(),
6623 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006624 }
6625 } else {
6626 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006627 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006628 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006629 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006630 } else {
6631 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006632 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006633 }
6634 }
6635 } else {
6636 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6637 if (second.IsRegisterPair()) {
6638 if (instruction->IsAnd()) {
6639 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6640 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6641 } else if (instruction->IsOr()) {
6642 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6643 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6644 } else {
6645 DCHECK(instruction->IsXor());
6646 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6647 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6648 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006649 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006650 if (instruction->IsAnd()) {
6651 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6652 __ andl(first.AsRegisterPairHigh<Register>(),
6653 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6654 } else if (instruction->IsOr()) {
6655 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6656 __ orl(first.AsRegisterPairHigh<Register>(),
6657 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6658 } else {
6659 DCHECK(instruction->IsXor());
6660 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6661 __ xorl(first.AsRegisterPairHigh<Register>(),
6662 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6663 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006664 } else {
6665 DCHECK(second.IsConstant()) << second;
6666 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006667 int32_t low_value = Low32Bits(value);
6668 int32_t high_value = High32Bits(value);
6669 Immediate low(low_value);
6670 Immediate high(high_value);
6671 Register first_low = first.AsRegisterPairLow<Register>();
6672 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006673 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006674 if (low_value == 0) {
6675 __ xorl(first_low, first_low);
6676 } else if (low_value != -1) {
6677 __ andl(first_low, low);
6678 }
6679 if (high_value == 0) {
6680 __ xorl(first_high, first_high);
6681 } else if (high_value != -1) {
6682 __ andl(first_high, high);
6683 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006684 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006685 if (low_value != 0) {
6686 __ orl(first_low, low);
6687 }
6688 if (high_value != 0) {
6689 __ orl(first_high, high);
6690 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006691 } else {
6692 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006693 if (low_value != 0) {
6694 __ xorl(first_low, low);
6695 }
6696 if (high_value != 0) {
6697 __ xorl(first_high, high);
6698 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006699 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006700 }
6701 }
6702}
6703
Roland Levillain7c1559a2015-12-15 10:55:36 +00006704void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6705 Location out,
6706 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006707 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006708 Register out_reg = out.AsRegister<Register>();
6709 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006710 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006711 if (kUseBakerReadBarrier) {
6712 // Load with fast path based Baker's read barrier.
6713 // /* HeapReference<Object> */ out = *(out + offset)
6714 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006715 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006716 } else {
6717 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006718 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006719 // in the following move operation, as we will need it for the
6720 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006721 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006722 // /* HeapReference<Object> */ out = *(out + offset)
6723 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006724 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006725 }
6726 } else {
6727 // Plain load with no read barrier.
6728 // /* HeapReference<Object> */ out = *(out + offset)
6729 __ movl(out_reg, Address(out_reg, offset));
6730 __ MaybeUnpoisonHeapReference(out_reg);
6731 }
6732}
6733
6734void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6735 Location out,
6736 Location obj,
6737 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006738 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006739 Register out_reg = out.AsRegister<Register>();
6740 Register obj_reg = obj.AsRegister<Register>();
6741 if (kEmitCompilerReadBarrier) {
6742 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006743 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006744 // Load with fast path based Baker's read barrier.
6745 // /* HeapReference<Object> */ out = *(obj + offset)
6746 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006747 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006748 } else {
6749 // Load with slow path based read barrier.
6750 // /* HeapReference<Object> */ out = *(obj + offset)
6751 __ movl(out_reg, Address(obj_reg, offset));
6752 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6753 }
6754 } else {
6755 // Plain load with no read barrier.
6756 // /* HeapReference<Object> */ out = *(obj + offset)
6757 __ movl(out_reg, Address(obj_reg, offset));
6758 __ MaybeUnpoisonHeapReference(out_reg);
6759 }
6760}
6761
6762void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6763 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006764 const Address& address,
6765 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006766 Register root_reg = root.AsRegister<Register>();
6767 if (kEmitCompilerReadBarrier) {
6768 if (kUseBakerReadBarrier) {
6769 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6770 // Baker's read barrier are used:
6771 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006772 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006773 // if (Thread::Current()->GetIsGcMarking()) {
6774 // root = ReadBarrier::Mark(root)
6775 // }
6776
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006777 // /* GcRoot<mirror::Object> */ root = *address
6778 __ movl(root_reg, address);
6779 if (fixup_label != nullptr) {
6780 __ Bind(fixup_label);
6781 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006782 static_assert(
6783 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6784 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6785 "have different sizes.");
6786 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6787 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6788 "have different sizes.");
6789
6790 // Slow path used to mark the GC root `root`.
6791 SlowPathCode* slow_path =
6792 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6793 codegen_->AddSlowPath(slow_path);
6794
6795 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6796 Immediate(0));
6797 __ j(kNotEqual, slow_path->GetEntryLabel());
6798 __ Bind(slow_path->GetExitLabel());
6799 } else {
6800 // GC root loaded through a slow path for read barriers other
6801 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006802 // /* GcRoot<mirror::Object>* */ root = address
6803 __ leal(root_reg, address);
6804 if (fixup_label != nullptr) {
6805 __ Bind(fixup_label);
6806 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006807 // /* mirror::Object* */ root = root->Read()
6808 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6809 }
6810 } else {
6811 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006812 // /* GcRoot<mirror::Object> */ root = *address
6813 __ movl(root_reg, address);
6814 if (fixup_label != nullptr) {
6815 __ Bind(fixup_label);
6816 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006817 // Note that GC roots are not affected by heap poisoning, thus we
6818 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006819 }
6820}
6821
6822void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6823 Location ref,
6824 Register obj,
6825 uint32_t offset,
6826 Location temp,
6827 bool needs_null_check) {
6828 DCHECK(kEmitCompilerReadBarrier);
6829 DCHECK(kUseBakerReadBarrier);
6830
6831 // /* HeapReference<Object> */ ref = *(obj + offset)
6832 Address src(obj, offset);
6833 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6834}
6835
6836void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6837 Location ref,
6838 Register obj,
6839 uint32_t data_offset,
6840 Location index,
6841 Location temp,
6842 bool needs_null_check) {
6843 DCHECK(kEmitCompilerReadBarrier);
6844 DCHECK(kUseBakerReadBarrier);
6845
6846 // /* HeapReference<Object> */ ref =
6847 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6848 Address src = index.IsConstant() ?
6849 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6850 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6851 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6852}
6853
6854void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6855 Location ref,
6856 Register obj,
6857 const Address& src,
6858 Location temp,
6859 bool needs_null_check) {
6860 DCHECK(kEmitCompilerReadBarrier);
6861 DCHECK(kUseBakerReadBarrier);
6862
6863 // In slow path based read barriers, the read barrier call is
6864 // inserted after the original load. However, in fast path based
6865 // Baker's read barriers, we need to perform the load of
6866 // mirror::Object::monitor_ *before* the original reference load.
6867 // This load-load ordering is required by the read barrier.
6868 // The fast path/slow path (for Baker's algorithm) should look like:
6869 //
6870 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6871 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6872 // HeapReference<Object> ref = *src; // Original reference load.
6873 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6874 // if (is_gray) {
6875 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6876 // }
6877 //
6878 // Note: the original implementation in ReadBarrier::Barrier is
6879 // slightly more complex as:
6880 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006881 // the high-bits of rb_state, which are expected to be all zeroes
6882 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6883 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006884 // - it performs additional checks that we do not do here for
6885 // performance reasons.
6886
6887 Register ref_reg = ref.AsRegister<Register>();
6888 Register temp_reg = temp.AsRegister<Register>();
6889 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6890
6891 // /* int32_t */ monitor = obj->monitor_
6892 __ movl(temp_reg, Address(obj, monitor_offset));
6893 if (needs_null_check) {
6894 MaybeRecordImplicitNullCheck(instruction);
6895 }
6896 // /* LockWord */ lock_word = LockWord(monitor)
6897 static_assert(sizeof(LockWord) == sizeof(int32_t),
6898 "art::LockWord and int32_t have different sizes.");
6899 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6900 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6901 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6902 static_assert(
6903 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6904 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6905
6906 // Load fence to prevent load-load reordering.
6907 // Note that this is a no-op, thanks to the x86 memory model.
6908 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6909
6910 // The actual reference load.
6911 // /* HeapReference<Object> */ ref = *src
6912 __ movl(ref_reg, src);
6913
6914 // Object* ref = ref_addr->AsMirrorPtr()
6915 __ MaybeUnpoisonHeapReference(ref_reg);
6916
6917 // Slow path used to mark the object `ref` when it is gray.
6918 SlowPathCode* slow_path =
6919 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6920 AddSlowPath(slow_path);
6921
6922 // if (rb_state == ReadBarrier::gray_ptr_)
6923 // ref = ReadBarrier::Mark(ref);
6924 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6925 __ j(kEqual, slow_path->GetEntryLabel());
6926 __ Bind(slow_path->GetExitLabel());
6927}
6928
6929void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6930 Location out,
6931 Location ref,
6932 Location obj,
6933 uint32_t offset,
6934 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006935 DCHECK(kEmitCompilerReadBarrier);
6936
Roland Levillain7c1559a2015-12-15 10:55:36 +00006937 // Insert a slow path based read barrier *after* the reference load.
6938 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006939 // If heap poisoning is enabled, the unpoisoning of the loaded
6940 // reference will be carried out by the runtime within the slow
6941 // path.
6942 //
6943 // Note that `ref` currently does not get unpoisoned (when heap
6944 // poisoning is enabled), which is alright as the `ref` argument is
6945 // not used by the artReadBarrierSlow entry point.
6946 //
6947 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6948 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6949 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6950 AddSlowPath(slow_path);
6951
Roland Levillain0d5a2812015-11-13 10:07:31 +00006952 __ jmp(slow_path->GetEntryLabel());
6953 __ Bind(slow_path->GetExitLabel());
6954}
6955
Roland Levillain7c1559a2015-12-15 10:55:36 +00006956void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6957 Location out,
6958 Location ref,
6959 Location obj,
6960 uint32_t offset,
6961 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006962 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006963 // Baker's read barriers shall be handled by the fast path
6964 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6965 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006966 // If heap poisoning is enabled, unpoisoning will be taken care of
6967 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006968 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006969 } else if (kPoisonHeapReferences) {
6970 __ UnpoisonHeapReference(out.AsRegister<Register>());
6971 }
6972}
6973
Roland Levillain7c1559a2015-12-15 10:55:36 +00006974void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6975 Location out,
6976 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006977 DCHECK(kEmitCompilerReadBarrier);
6978
Roland Levillain7c1559a2015-12-15 10:55:36 +00006979 // Insert a slow path based read barrier *after* the GC root load.
6980 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006981 // Note that GC roots are not affected by heap poisoning, so we do
6982 // not need to do anything special for this here.
6983 SlowPathCode* slow_path =
6984 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6985 AddSlowPath(slow_path);
6986
Roland Levillain0d5a2812015-11-13 10:07:31 +00006987 __ jmp(slow_path->GetEntryLabel());
6988 __ Bind(slow_path->GetExitLabel());
6989}
6990
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006991void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006992 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006993 LOG(FATAL) << "Unreachable";
6994}
6995
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006996void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006997 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006998 LOG(FATAL) << "Unreachable";
6999}
7000
Mark Mendellfe57faa2015-09-18 09:26:15 -04007001// Simple implementation of packed switch - generate cascaded compare/jumps.
7002void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7003 LocationSummary* locations =
7004 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7005 locations->SetInAt(0, Location::RequiresRegister());
7006}
7007
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007008void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7009 int32_t lower_bound,
7010 uint32_t num_entries,
7011 HBasicBlock* switch_block,
7012 HBasicBlock* default_block) {
7013 // Figure out the correct compare values and jump conditions.
7014 // Handle the first compare/branch as a special case because it might
7015 // jump to the default case.
7016 DCHECK_GT(num_entries, 2u);
7017 Condition first_condition;
7018 uint32_t index;
7019 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7020 if (lower_bound != 0) {
7021 first_condition = kLess;
7022 __ cmpl(value_reg, Immediate(lower_bound));
7023 __ j(first_condition, codegen_->GetLabelOf(default_block));
7024 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007025
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007026 index = 1;
7027 } else {
7028 // Handle all the compare/jumps below.
7029 first_condition = kBelow;
7030 index = 0;
7031 }
7032
7033 // Handle the rest of the compare/jumps.
7034 for (; index + 1 < num_entries; index += 2) {
7035 int32_t compare_to_value = lower_bound + index + 1;
7036 __ cmpl(value_reg, Immediate(compare_to_value));
7037 // Jump to successors[index] if value < case_value[index].
7038 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7039 // Jump to successors[index + 1] if value == case_value[index + 1].
7040 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7041 }
7042
7043 if (index != num_entries) {
7044 // There are an odd number of entries. Handle the last one.
7045 DCHECK_EQ(index + 1, num_entries);
7046 __ cmpl(value_reg, Immediate(lower_bound + index));
7047 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007048 }
7049
7050 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007051 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7052 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007053 }
7054}
7055
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007056void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7057 int32_t lower_bound = switch_instr->GetStartValue();
7058 uint32_t num_entries = switch_instr->GetNumEntries();
7059 LocationSummary* locations = switch_instr->GetLocations();
7060 Register value_reg = locations->InAt(0).AsRegister<Register>();
7061
7062 GenPackedSwitchWithCompares(value_reg,
7063 lower_bound,
7064 num_entries,
7065 switch_instr->GetBlock(),
7066 switch_instr->GetDefaultBlock());
7067}
7068
Mark Mendell805b3b52015-09-18 14:10:29 -04007069void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7070 LocationSummary* locations =
7071 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7072 locations->SetInAt(0, Location::RequiresRegister());
7073
7074 // Constant area pointer.
7075 locations->SetInAt(1, Location::RequiresRegister());
7076
7077 // And the temporary we need.
7078 locations->AddTemp(Location::RequiresRegister());
7079}
7080
7081void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7082 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007083 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007084 LocationSummary* locations = switch_instr->GetLocations();
7085 Register value_reg = locations->InAt(0).AsRegister<Register>();
7086 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7087
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007088 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7089 GenPackedSwitchWithCompares(value_reg,
7090 lower_bound,
7091 num_entries,
7092 switch_instr->GetBlock(),
7093 default_block);
7094 return;
7095 }
7096
Mark Mendell805b3b52015-09-18 14:10:29 -04007097 // Optimizing has a jump area.
7098 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7099 Register constant_area = locations->InAt(1).AsRegister<Register>();
7100
7101 // Remove the bias, if needed.
7102 if (lower_bound != 0) {
7103 __ leal(temp_reg, Address(value_reg, -lower_bound));
7104 value_reg = temp_reg;
7105 }
7106
7107 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007108 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007109 __ cmpl(value_reg, Immediate(num_entries - 1));
7110 __ j(kAbove, codegen_->GetLabelOf(default_block));
7111
7112 // We are in the range of the table.
7113 // Load (target-constant_area) from the jump table, indexing by the value.
7114 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7115
7116 // Compute the actual target address by adding in constant_area.
7117 __ addl(temp_reg, constant_area);
7118
7119 // And jump.
7120 __ jmp(temp_reg);
7121}
7122
Mark Mendell0616ae02015-04-17 12:49:27 -04007123void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7124 HX86ComputeBaseMethodAddress* insn) {
7125 LocationSummary* locations =
7126 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7127 locations->SetOut(Location::RequiresRegister());
7128}
7129
7130void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7131 HX86ComputeBaseMethodAddress* insn) {
7132 LocationSummary* locations = insn->GetLocations();
7133 Register reg = locations->Out().AsRegister<Register>();
7134
7135 // Generate call to next instruction.
7136 Label next_instruction;
7137 __ call(&next_instruction);
7138 __ Bind(&next_instruction);
7139
7140 // Remember this offset for later use with constant area.
7141 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7142
7143 // Grab the return address off the stack.
7144 __ popl(reg);
7145}
7146
7147void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7148 HX86LoadFromConstantTable* insn) {
7149 LocationSummary* locations =
7150 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7151
7152 locations->SetInAt(0, Location::RequiresRegister());
7153 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7154
7155 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007156 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007157 return;
7158 }
7159
7160 switch (insn->GetType()) {
7161 case Primitive::kPrimFloat:
7162 case Primitive::kPrimDouble:
7163 locations->SetOut(Location::RequiresFpuRegister());
7164 break;
7165
7166 case Primitive::kPrimInt:
7167 locations->SetOut(Location::RequiresRegister());
7168 break;
7169
7170 default:
7171 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7172 }
7173}
7174
7175void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007176 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007177 return;
7178 }
7179
7180 LocationSummary* locations = insn->GetLocations();
7181 Location out = locations->Out();
7182 Register const_area = locations->InAt(0).AsRegister<Register>();
7183 HConstant *value = insn->GetConstant();
7184
7185 switch (insn->GetType()) {
7186 case Primitive::kPrimFloat:
7187 __ movss(out.AsFpuRegister<XmmRegister>(),
7188 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7189 break;
7190
7191 case Primitive::kPrimDouble:
7192 __ movsd(out.AsFpuRegister<XmmRegister>(),
7193 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7194 break;
7195
7196 case Primitive::kPrimInt:
7197 __ movl(out.AsRegister<Register>(),
7198 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7199 break;
7200
7201 default:
7202 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7203 }
7204}
7205
Mark Mendell0616ae02015-04-17 12:49:27 -04007206/**
7207 * Class to handle late fixup of offsets into constant area.
7208 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007209class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007210 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007211 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7212 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7213
7214 protected:
7215 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7216
7217 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007218
7219 private:
7220 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7221 // Patch the correct offset for the instruction. The place to patch is the
7222 // last 4 bytes of the instruction.
7223 // The value to patch is the distance from the offset in the constant area
7224 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007225 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7226 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007227
7228 // Patch in the right value.
7229 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7230 }
7231
Mark Mendell0616ae02015-04-17 12:49:27 -04007232 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007233 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007234};
7235
Mark Mendell805b3b52015-09-18 14:10:29 -04007236/**
7237 * Class to handle late fixup of offsets to a jump table that will be created in the
7238 * constant area.
7239 */
7240class JumpTableRIPFixup : public RIPFixup {
7241 public:
7242 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7243 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7244
7245 void CreateJumpTable() {
7246 X86Assembler* assembler = codegen_->GetAssembler();
7247
7248 // Ensure that the reference to the jump table has the correct offset.
7249 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7250 SetOffset(offset_in_constant_table);
7251
7252 // The label values in the jump table are computed relative to the
7253 // instruction addressing the constant area.
7254 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7255
7256 // Populate the jump table with the correct values for the jump table.
7257 int32_t num_entries = switch_instr_->GetNumEntries();
7258 HBasicBlock* block = switch_instr_->GetBlock();
7259 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7260 // The value that we want is the target offset - the position of the table.
7261 for (int32_t i = 0; i < num_entries; i++) {
7262 HBasicBlock* b = successors[i];
7263 Label* l = codegen_->GetLabelOf(b);
7264 DCHECK(l->IsBound());
7265 int32_t offset_to_block = l->Position() - relative_offset;
7266 assembler->AppendInt32(offset_to_block);
7267 }
7268 }
7269
7270 private:
7271 const HX86PackedSwitch* switch_instr_;
7272};
7273
7274void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7275 // Generate the constant area if needed.
7276 X86Assembler* assembler = GetAssembler();
7277 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7278 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7279 // byte values.
7280 assembler->Align(4, 0);
7281 constant_area_start_ = assembler->CodeSize();
7282
7283 // Populate any jump tables.
7284 for (auto jump_table : fixups_to_jump_tables_) {
7285 jump_table->CreateJumpTable();
7286 }
7287
7288 // And now add the constant area to the generated code.
7289 assembler->AddConstantArea();
7290 }
7291
7292 // And finish up.
7293 CodeGenerator::Finalize(allocator);
7294}
7295
Mark Mendell0616ae02015-04-17 12:49:27 -04007296Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7297 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7298 return Address(reg, kDummy32BitOffset, fixup);
7299}
7300
7301Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7302 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7303 return Address(reg, kDummy32BitOffset, fixup);
7304}
7305
7306Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7307 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7308 return Address(reg, kDummy32BitOffset, fixup);
7309}
7310
7311Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7312 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7313 return Address(reg, kDummy32BitOffset, fixup);
7314}
7315
Aart Bika19616e2016-02-01 18:57:58 -08007316void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7317 if (value == 0) {
7318 __ xorl(dest, dest);
7319 } else {
7320 __ movl(dest, Immediate(value));
7321 }
7322}
7323
7324void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7325 if (value == 0) {
7326 __ testl(dest, dest);
7327 } else {
7328 __ cmpl(dest, Immediate(value));
7329 }
7330}
7331
Mark Mendell805b3b52015-09-18 14:10:29 -04007332Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7333 Register reg,
7334 Register value) {
7335 // Create a fixup to be used to create and address the jump table.
7336 JumpTableRIPFixup* table_fixup =
7337 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7338
7339 // We have to populate the jump tables.
7340 fixups_to_jump_tables_.push_back(table_fixup);
7341
7342 // We want a scaled address, as we are extracting the correct offset from the table.
7343 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7344}
7345
Andreas Gampe85b62f22015-09-09 13:15:38 -07007346// TODO: target as memory.
7347void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7348 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007349 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007350 return;
7351 }
7352
7353 DCHECK_NE(type, Primitive::kPrimVoid);
7354
7355 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7356 if (target.Equals(return_loc)) {
7357 return;
7358 }
7359
7360 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7361 // with the else branch.
7362 if (type == Primitive::kPrimLong) {
7363 HParallelMove parallel_move(GetGraph()->GetArena());
7364 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7365 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7366 GetMoveResolver()->EmitNativeCode(&parallel_move);
7367 } else {
7368 // Let the parallel move resolver take care of all of this.
7369 HParallelMove parallel_move(GetGraph()->GetArena());
7370 parallel_move.AddMove(return_loc, target, type, nullptr);
7371 GetMoveResolver()->EmitNativeCode(&parallel_move);
7372 }
7373}
7374
Roland Levillain4d027112015-07-01 15:41:14 +01007375#undef __
7376
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007377} // namespace x86
7378} // namespace art