blob: 304cf08b5135cf022ce44ae7de486356fe22ba46 [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 Marko40ecb122016-04-06 17:33:41 +01004369 // temp = temp[index_in_cache];
4370 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4371 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004372 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4373 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004374 }
Vladimir Marko58155012015-08-19 12:49:41 +00004375 }
4376
4377 switch (invoke->GetCodePtrLocation()) {
4378 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4379 __ call(GetFrameEntryLabel());
4380 break;
4381 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4382 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4383 Label* label = &relative_call_patches_.back().label;
4384 __ call(label); // Bind to the patch label, override at link time.
4385 __ Bind(label); // Bind the label at the end of the "call" insn.
4386 break;
4387 }
4388 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4389 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004390 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4391 LOG(FATAL) << "Unsupported";
4392 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004393 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4394 // (callee_method + offset_of_quick_compiled_code)()
4395 __ call(Address(callee_method.AsRegister<Register>(),
4396 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4397 kX86WordSize).Int32Value()));
4398 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004399 }
4400
4401 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004402}
4403
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004404void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4405 Register temp = temp_in.AsRegister<Register>();
4406 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4407 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004408
4409 // Use the calling convention instead of the location of the receiver, as
4410 // intrinsics may have put the receiver in a different register. In the intrinsics
4411 // slow path, the arguments have been moved to the right place, so here we are
4412 // guaranteed that the receiver is the first register of the calling convention.
4413 InvokeDexCallingConvention calling_convention;
4414 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004415 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004416 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004417 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004418 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004419 // Instead of simply (possibly) unpoisoning `temp` here, we should
4420 // emit a read barrier for the previous class reference load.
4421 // However this is not required in practice, as this is an
4422 // intermediate/temporary reference and because the current
4423 // concurrent copying collector keeps the from-space memory
4424 // intact/accessible until the end of the marking phase (the
4425 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004426 __ MaybeUnpoisonHeapReference(temp);
4427 // temp = temp->GetMethodAt(method_offset);
4428 __ movl(temp, Address(temp, method_offset));
4429 // call temp->GetEntryPoint();
4430 __ call(Address(
4431 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4432}
4433
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004434void CodeGeneratorX86::RecordSimplePatch() {
4435 if (GetCompilerOptions().GetIncludePatchInformation()) {
4436 simple_patches_.emplace_back();
4437 __ Bind(&simple_patches_.back());
4438 }
4439}
4440
4441void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4442 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4443 __ Bind(&string_patches_.back().label);
4444}
4445
4446Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4447 uint32_t element_offset) {
4448 // Add the patch entry and bind its label at the end of the instruction.
4449 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4450 return &pc_relative_dex_cache_patches_.back().label;
4451}
4452
Vladimir Marko58155012015-08-19 12:49:41 +00004453void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4454 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004455 size_t size =
4456 method_patches_.size() +
4457 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004458 pc_relative_dex_cache_patches_.size() +
4459 simple_patches_.size() +
4460 string_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004461 linker_patches->reserve(size);
4462 // The label points to the end of the "movl" insn but the literal offset for method
4463 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4464 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004465 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004466 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004467 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4468 info.target_method.dex_file,
4469 info.target_method.dex_method_index));
4470 }
4471 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004472 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004473 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4474 info.target_method.dex_file,
4475 info.target_method.dex_method_index));
4476 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004477 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4478 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4479 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4480 &info.target_dex_file,
4481 GetMethodAddressOffset(),
4482 info.element_offset));
4483 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004484 for (const Label& label : simple_patches_) {
4485 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4486 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4487 }
4488 if (GetCompilerOptions().GetCompilePic()) {
4489 for (const StringPatchInfo<Label>& info : string_patches_) {
4490 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4491 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4492 &info.dex_file,
4493 GetMethodAddressOffset(),
4494 info.string_index));
4495 }
4496 } else {
4497 for (const StringPatchInfo<Label>& info : string_patches_) {
4498 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4499 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4500 &info.dex_file,
4501 info.string_index));
4502 }
4503 }
Vladimir Marko58155012015-08-19 12:49:41 +00004504}
4505
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004506void CodeGeneratorX86::MarkGCCard(Register temp,
4507 Register card,
4508 Register object,
4509 Register value,
4510 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004511 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004512 if (value_can_be_null) {
4513 __ testl(value, value);
4514 __ j(kEqual, &is_null);
4515 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004516 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4517 __ movl(temp, object);
4518 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004519 __ movb(Address(temp, card, TIMES_1, 0),
4520 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004521 if (value_can_be_null) {
4522 __ Bind(&is_null);
4523 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004524}
4525
Calin Juravle52c48962014-12-16 17:02:57 +00004526void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4527 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004528
4529 bool object_field_get_with_read_barrier =
4530 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004531 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004532 new (GetGraph()->GetArena()) LocationSummary(instruction,
4533 kEmitCompilerReadBarrier ?
4534 LocationSummary::kCallOnSlowPath :
4535 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004536 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004537
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004538 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4539 locations->SetOut(Location::RequiresFpuRegister());
4540 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004541 // The output overlaps in case of long: we don't want the low move
4542 // to overwrite the object's location. Likewise, in the case of
4543 // an object field get with read barriers enabled, we do not want
4544 // the move to overwrite the object's location, as we need it to emit
4545 // the read barrier.
4546 locations->SetOut(
4547 Location::RequiresRegister(),
4548 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4549 Location::kOutputOverlap :
4550 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004551 }
Calin Juravle52c48962014-12-16 17:02:57 +00004552
4553 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4554 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004555 // So we use an XMM register as a temp to achieve atomicity (first
4556 // load the temp into the XMM and then copy the XMM into the
4557 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004558 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004559 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4560 // We need a temporary register for the read barrier marking slow
4561 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4562 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004563 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004564}
4565
Calin Juravle52c48962014-12-16 17:02:57 +00004566void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4567 const FieldInfo& field_info) {
4568 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004569
Calin Juravle52c48962014-12-16 17:02:57 +00004570 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004571 Location base_loc = locations->InAt(0);
4572 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004573 Location out = locations->Out();
4574 bool is_volatile = field_info.IsVolatile();
4575 Primitive::Type field_type = field_info.GetFieldType();
4576 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4577
4578 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004579 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004580 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004581 break;
4582 }
4583
4584 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004585 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004586 break;
4587 }
4588
4589 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004590 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004591 break;
4592 }
4593
4594 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004595 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004596 break;
4597 }
4598
4599 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004600 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004601 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004602
4603 case Primitive::kPrimNot: {
4604 // /* HeapReference<Object> */ out = *(base + offset)
4605 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4606 Location temp_loc = locations->GetTemp(0);
4607 // Note that a potential implicit null check is handled in this
4608 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4609 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4610 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4611 if (is_volatile) {
4612 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4613 }
4614 } else {
4615 __ movl(out.AsRegister<Register>(), Address(base, offset));
4616 codegen_->MaybeRecordImplicitNullCheck(instruction);
4617 if (is_volatile) {
4618 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4619 }
4620 // If read barriers are enabled, emit read barriers other than
4621 // Baker's using a slow path (and also unpoison the loaded
4622 // reference, if heap poisoning is enabled).
4623 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4624 }
4625 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004626 }
4627
4628 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004629 if (is_volatile) {
4630 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4631 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004632 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004633 __ movd(out.AsRegisterPairLow<Register>(), temp);
4634 __ psrlq(temp, Immediate(32));
4635 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4636 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004637 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004638 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004639 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004640 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4641 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004642 break;
4643 }
4644
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004645 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004646 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004647 break;
4648 }
4649
4650 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004651 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004652 break;
4653 }
4654
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004656 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004657 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004658 }
Calin Juravle52c48962014-12-16 17:02:57 +00004659
Roland Levillain7c1559a2015-12-15 10:55:36 +00004660 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4661 // Potential implicit null checks, in the case of reference or
4662 // long fields, are handled in the previous switch statement.
4663 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004664 codegen_->MaybeRecordImplicitNullCheck(instruction);
4665 }
4666
Calin Juravle52c48962014-12-16 17:02:57 +00004667 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004668 if (field_type == Primitive::kPrimNot) {
4669 // Memory barriers, in the case of references, are also handled
4670 // in the previous switch statement.
4671 } else {
4672 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4673 }
Roland Levillain4d027112015-07-01 15:41:14 +01004674 }
Calin Juravle52c48962014-12-16 17:02:57 +00004675}
4676
4677void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4678 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4679
4680 LocationSummary* locations =
4681 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4682 locations->SetInAt(0, Location::RequiresRegister());
4683 bool is_volatile = field_info.IsVolatile();
4684 Primitive::Type field_type = field_info.GetFieldType();
4685 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4686 || (field_type == Primitive::kPrimByte);
4687
4688 // The register allocator does not support multiple
4689 // inputs that die at entry with one in a specific register.
4690 if (is_byte_type) {
4691 // Ensure the value is in a byte register.
4692 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004693 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004694 if (is_volatile && field_type == Primitive::kPrimDouble) {
4695 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4696 locations->SetInAt(1, Location::RequiresFpuRegister());
4697 } else {
4698 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4699 }
4700 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4701 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004702 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004703
Calin Juravle52c48962014-12-16 17:02:57 +00004704 // 64bits value can be atomically written to an address with movsd and an XMM register.
4705 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4706 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4707 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4708 // isolated cases when we need this it isn't worth adding the extra complexity.
4709 locations->AddTemp(Location::RequiresFpuRegister());
4710 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004711 } else {
4712 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4713
4714 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4715 // Temporary registers for the write barrier.
4716 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4717 // Ensure the card is in a byte register.
4718 locations->AddTemp(Location::RegisterLocation(ECX));
4719 }
Calin Juravle52c48962014-12-16 17:02:57 +00004720 }
4721}
4722
4723void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004724 const FieldInfo& field_info,
4725 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004726 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4727
4728 LocationSummary* locations = instruction->GetLocations();
4729 Register base = locations->InAt(0).AsRegister<Register>();
4730 Location value = locations->InAt(1);
4731 bool is_volatile = field_info.IsVolatile();
4732 Primitive::Type field_type = field_info.GetFieldType();
4733 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004734 bool needs_write_barrier =
4735 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004736
4737 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004738 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004739 }
4740
Mark Mendell81489372015-11-04 11:30:41 -05004741 bool maybe_record_implicit_null_check_done = false;
4742
Calin Juravle52c48962014-12-16 17:02:57 +00004743 switch (field_type) {
4744 case Primitive::kPrimBoolean:
4745 case Primitive::kPrimByte: {
4746 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4747 break;
4748 }
4749
4750 case Primitive::kPrimShort:
4751 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004752 if (value.IsConstant()) {
4753 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4754 __ movw(Address(base, offset), Immediate(v));
4755 } else {
4756 __ movw(Address(base, offset), value.AsRegister<Register>());
4757 }
Calin Juravle52c48962014-12-16 17:02:57 +00004758 break;
4759 }
4760
4761 case Primitive::kPrimInt:
4762 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004763 if (kPoisonHeapReferences && needs_write_barrier) {
4764 // Note that in the case where `value` is a null reference,
4765 // we do not enter this block, as the reference does not
4766 // need poisoning.
4767 DCHECK_EQ(field_type, Primitive::kPrimNot);
4768 Register temp = locations->GetTemp(0).AsRegister<Register>();
4769 __ movl(temp, value.AsRegister<Register>());
4770 __ PoisonHeapReference(temp);
4771 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004772 } else if (value.IsConstant()) {
4773 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4774 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004775 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004776 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004777 __ movl(Address(base, offset), value.AsRegister<Register>());
4778 }
Calin Juravle52c48962014-12-16 17:02:57 +00004779 break;
4780 }
4781
4782 case Primitive::kPrimLong: {
4783 if (is_volatile) {
4784 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4785 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4786 __ movd(temp1, value.AsRegisterPairLow<Register>());
4787 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4788 __ punpckldq(temp1, temp2);
4789 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004790 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004791 } else if (value.IsConstant()) {
4792 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4793 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4794 codegen_->MaybeRecordImplicitNullCheck(instruction);
4795 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004796 } else {
4797 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004798 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004799 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4800 }
Mark Mendell81489372015-11-04 11:30:41 -05004801 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004802 break;
4803 }
4804
4805 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004806 if (value.IsConstant()) {
4807 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4808 __ movl(Address(base, offset), Immediate(v));
4809 } else {
4810 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4811 }
Calin Juravle52c48962014-12-16 17:02:57 +00004812 break;
4813 }
4814
4815 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004816 if (value.IsConstant()) {
4817 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4818 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4819 codegen_->MaybeRecordImplicitNullCheck(instruction);
4820 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4821 maybe_record_implicit_null_check_done = true;
4822 } else {
4823 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4824 }
Calin Juravle52c48962014-12-16 17:02:57 +00004825 break;
4826 }
4827
4828 case Primitive::kPrimVoid:
4829 LOG(FATAL) << "Unreachable type " << field_type;
4830 UNREACHABLE();
4831 }
4832
Mark Mendell81489372015-11-04 11:30:41 -05004833 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004834 codegen_->MaybeRecordImplicitNullCheck(instruction);
4835 }
4836
Roland Levillain4d027112015-07-01 15:41:14 +01004837 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004838 Register temp = locations->GetTemp(0).AsRegister<Register>();
4839 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004840 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004841 }
4842
Calin Juravle52c48962014-12-16 17:02:57 +00004843 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004844 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004845 }
4846}
4847
4848void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4849 HandleFieldGet(instruction, instruction->GetFieldInfo());
4850}
4851
4852void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4853 HandleFieldGet(instruction, instruction->GetFieldInfo());
4854}
4855
4856void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4857 HandleFieldSet(instruction, instruction->GetFieldInfo());
4858}
4859
4860void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004861 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004862}
4863
4864void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4865 HandleFieldSet(instruction, instruction->GetFieldInfo());
4866}
4867
4868void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004869 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004870}
4871
4872void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4873 HandleFieldGet(instruction, instruction->GetFieldInfo());
4874}
4875
4876void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4877 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004878}
4879
Calin Juravlee460d1d2015-09-29 04:52:17 +01004880void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4881 HUnresolvedInstanceFieldGet* instruction) {
4882 FieldAccessCallingConventionX86 calling_convention;
4883 codegen_->CreateUnresolvedFieldLocationSummary(
4884 instruction, instruction->GetFieldType(), calling_convention);
4885}
4886
4887void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4888 HUnresolvedInstanceFieldGet* instruction) {
4889 FieldAccessCallingConventionX86 calling_convention;
4890 codegen_->GenerateUnresolvedFieldAccess(instruction,
4891 instruction->GetFieldType(),
4892 instruction->GetFieldIndex(),
4893 instruction->GetDexPc(),
4894 calling_convention);
4895}
4896
4897void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4898 HUnresolvedInstanceFieldSet* instruction) {
4899 FieldAccessCallingConventionX86 calling_convention;
4900 codegen_->CreateUnresolvedFieldLocationSummary(
4901 instruction, instruction->GetFieldType(), calling_convention);
4902}
4903
4904void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4905 HUnresolvedInstanceFieldSet* instruction) {
4906 FieldAccessCallingConventionX86 calling_convention;
4907 codegen_->GenerateUnresolvedFieldAccess(instruction,
4908 instruction->GetFieldType(),
4909 instruction->GetFieldIndex(),
4910 instruction->GetDexPc(),
4911 calling_convention);
4912}
4913
4914void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4915 HUnresolvedStaticFieldGet* instruction) {
4916 FieldAccessCallingConventionX86 calling_convention;
4917 codegen_->CreateUnresolvedFieldLocationSummary(
4918 instruction, instruction->GetFieldType(), calling_convention);
4919}
4920
4921void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4922 HUnresolvedStaticFieldGet* instruction) {
4923 FieldAccessCallingConventionX86 calling_convention;
4924 codegen_->GenerateUnresolvedFieldAccess(instruction,
4925 instruction->GetFieldType(),
4926 instruction->GetFieldIndex(),
4927 instruction->GetDexPc(),
4928 calling_convention);
4929}
4930
4931void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4932 HUnresolvedStaticFieldSet* instruction) {
4933 FieldAccessCallingConventionX86 calling_convention;
4934 codegen_->CreateUnresolvedFieldLocationSummary(
4935 instruction, instruction->GetFieldType(), calling_convention);
4936}
4937
4938void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4939 HUnresolvedStaticFieldSet* instruction) {
4940 FieldAccessCallingConventionX86 calling_convention;
4941 codegen_->GenerateUnresolvedFieldAccess(instruction,
4942 instruction->GetFieldType(),
4943 instruction->GetFieldIndex(),
4944 instruction->GetDexPc(),
4945 calling_convention);
4946}
4947
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004948void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004949 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4950 ? LocationSummary::kCallOnSlowPath
4951 : LocationSummary::kNoCall;
4952 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4953 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004954 ? Location::RequiresRegister()
4955 : Location::Any();
4956 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004957 if (instruction->HasUses()) {
4958 locations->SetOut(Location::SameAsFirstInput());
4959 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004960}
4961
Calin Juravle2ae48182016-03-16 14:05:09 +00004962void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4963 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004964 return;
4965 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004966 LocationSummary* locations = instruction->GetLocations();
4967 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004968
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004969 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004970 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004971}
4972
Calin Juravle2ae48182016-03-16 14:05:09 +00004973void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004974 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004975 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004976
4977 LocationSummary* locations = instruction->GetLocations();
4978 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004979
4980 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004981 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004982 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004983 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004984 } else {
4985 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004986 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004987 __ jmp(slow_path->GetEntryLabel());
4988 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004989 }
4990 __ j(kEqual, slow_path->GetEntryLabel());
4991}
4992
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004993void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004994 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004995}
4996
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004997void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004998 bool object_array_get_with_read_barrier =
4999 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005000 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005001 new (GetGraph()->GetArena()) LocationSummary(instruction,
5002 object_array_get_with_read_barrier ?
5003 LocationSummary::kCallOnSlowPath :
5004 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005005 locations->SetInAt(0, Location::RequiresRegister());
5006 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005007 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5008 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5009 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005010 // The output overlaps in case of long: we don't want the low move
5011 // to overwrite the array's location. Likewise, in the case of an
5012 // object array get with read barriers enabled, we do not want the
5013 // move to overwrite the array's location, as we need it to emit
5014 // the read barrier.
5015 locations->SetOut(
5016 Location::RequiresRegister(),
5017 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5018 Location::kOutputOverlap :
5019 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005020 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005021 // We need a temporary register for the read barrier marking slow
5022 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5023 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5024 locations->AddTemp(Location::RequiresRegister());
5025 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005026}
5027
5028void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5029 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005030 Location obj_loc = locations->InAt(0);
5031 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005032 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005033 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005034
Calin Juravle77520bc2015-01-12 18:45:46 +00005035 Primitive::Type type = instruction->GetType();
5036 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005037 case Primitive::kPrimBoolean: {
5038 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005039 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040 if (index.IsConstant()) {
5041 __ movzxb(out, Address(obj,
5042 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5043 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005044 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005045 }
5046 break;
5047 }
5048
5049 case Primitive::kPrimByte: {
5050 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005051 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005052 if (index.IsConstant()) {
5053 __ movsxb(out, Address(obj,
5054 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5055 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005056 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005057 }
5058 break;
5059 }
5060
5061 case Primitive::kPrimShort: {
5062 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005063 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064 if (index.IsConstant()) {
5065 __ movsxw(out, Address(obj,
5066 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5067 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005068 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069 }
5070 break;
5071 }
5072
5073 case Primitive::kPrimChar: {
5074 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005075 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005076 if (index.IsConstant()) {
5077 __ movzxw(out, Address(obj,
5078 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5079 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005080 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081 }
5082 break;
5083 }
5084
Roland Levillain7c1559a2015-12-15 10:55:36 +00005085 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005086 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005087 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005088 if (index.IsConstant()) {
5089 __ movl(out, Address(obj,
5090 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5091 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005092 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005093 }
5094 break;
5095 }
5096
Roland Levillain7c1559a2015-12-15 10:55:36 +00005097 case Primitive::kPrimNot: {
5098 static_assert(
5099 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5100 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5101 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5102 // /* HeapReference<Object> */ out =
5103 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5104 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5105 Location temp = locations->GetTemp(0);
5106 // Note that a potential implicit null check is handled in this
5107 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5108 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5109 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5110 } else {
5111 Register out = out_loc.AsRegister<Register>();
5112 if (index.IsConstant()) {
5113 uint32_t offset =
5114 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5115 __ movl(out, Address(obj, offset));
5116 codegen_->MaybeRecordImplicitNullCheck(instruction);
5117 // If read barriers are enabled, emit read barriers other than
5118 // Baker's using a slow path (and also unpoison the loaded
5119 // reference, if heap poisoning is enabled).
5120 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5121 } else {
5122 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5123 codegen_->MaybeRecordImplicitNullCheck(instruction);
5124 // If read barriers are enabled, emit read barriers other than
5125 // Baker's using a slow path (and also unpoison the loaded
5126 // reference, if heap poisoning is enabled).
5127 codegen_->MaybeGenerateReadBarrierSlow(
5128 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5129 }
5130 }
5131 break;
5132 }
5133
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005134 case Primitive::kPrimLong: {
5135 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005136 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 if (index.IsConstant()) {
5138 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005139 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005140 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005141 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005142 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005143 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005144 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005145 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005146 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005147 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005148 }
5149 break;
5150 }
5151
Mark Mendell7c8d0092015-01-26 11:21:33 -05005152 case Primitive::kPrimFloat: {
5153 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005154 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005155 if (index.IsConstant()) {
5156 __ movss(out, Address(obj,
5157 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5158 } else {
5159 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5160 }
5161 break;
5162 }
5163
5164 case Primitive::kPrimDouble: {
5165 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005166 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005167 if (index.IsConstant()) {
5168 __ movsd(out, Address(obj,
5169 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5170 } else {
5171 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5172 }
5173 break;
5174 }
5175
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005176 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005177 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005178 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005179 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005180
Roland Levillain7c1559a2015-12-15 10:55:36 +00005181 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5182 // Potential implicit null checks, in the case of reference or
5183 // long arrays, are handled in the previous switch statement.
5184 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005185 codegen_->MaybeRecordImplicitNullCheck(instruction);
5186 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005187}
5188
5189void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005190 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005191
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005192 bool needs_write_barrier =
5193 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005194 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5195 bool object_array_set_with_read_barrier =
5196 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005197
Nicolas Geoffray39468442014-09-02 15:17:15 +01005198 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5199 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005200 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5201 LocationSummary::kCallOnSlowPath :
5202 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005203
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005204 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5205 || (value_type == Primitive::kPrimByte);
5206 // We need the inputs to be different than the output in case of long operation.
5207 // In case of a byte operation, the register allocator does not support multiple
5208 // inputs that die at entry with one in a specific register.
5209 locations->SetInAt(0, Location::RequiresRegister());
5210 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5211 if (is_byte_type) {
5212 // Ensure the value is in a byte register.
5213 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5214 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005215 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005217 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5218 }
5219 if (needs_write_barrier) {
5220 // Temporary registers for the write barrier.
5221 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5222 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005223 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005224 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005225}
5226
5227void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5228 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005229 Location array_loc = locations->InAt(0);
5230 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005232 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005233 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005234 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5235 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5236 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005237 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005238 bool needs_write_barrier =
5239 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005240
5241 switch (value_type) {
5242 case Primitive::kPrimBoolean:
5243 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005244 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5245 Address address = index.IsConstant()
5246 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5247 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5248 if (value.IsRegister()) {
5249 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005250 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005251 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005252 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005253 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 break;
5255 }
5256
5257 case Primitive::kPrimShort:
5258 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005259 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5260 Address address = index.IsConstant()
5261 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5262 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5263 if (value.IsRegister()) {
5264 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005265 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005266 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005267 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005268 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269 break;
5270 }
5271
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005272 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005273 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5274 Address address = index.IsConstant()
5275 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5276 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005277
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005278 if (!value.IsRegister()) {
5279 // Just setting null.
5280 DCHECK(instruction->InputAt(2)->IsNullConstant());
5281 DCHECK(value.IsConstant()) << value;
5282 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005283 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005284 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005285 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005286 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005287 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005288
5289 DCHECK(needs_write_barrier);
5290 Register register_value = value.AsRegister<Register>();
5291 NearLabel done, not_null, do_put;
5292 SlowPathCode* slow_path = nullptr;
5293 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005294 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005295 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5296 codegen_->AddSlowPath(slow_path);
5297 if (instruction->GetValueCanBeNull()) {
5298 __ testl(register_value, register_value);
5299 __ j(kNotEqual, &not_null);
5300 __ movl(address, Immediate(0));
5301 codegen_->MaybeRecordImplicitNullCheck(instruction);
5302 __ jmp(&done);
5303 __ Bind(&not_null);
5304 }
5305
Roland Levillain0d5a2812015-11-13 10:07:31 +00005306 if (kEmitCompilerReadBarrier) {
5307 // When read barriers are enabled, the type checking
5308 // instrumentation requires two read barriers:
5309 //
5310 // __ movl(temp2, temp);
5311 // // /* HeapReference<Class> */ temp = temp->component_type_
5312 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005313 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005314 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5315 //
5316 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5317 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005318 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005319 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5320 //
5321 // __ cmpl(temp, temp2);
5322 //
5323 // However, the second read barrier may trash `temp`, as it
5324 // is a temporary register, and as such would not be saved
5325 // along with live registers before calling the runtime (nor
5326 // restored afterwards). So in this case, we bail out and
5327 // delegate the work to the array set slow path.
5328 //
5329 // TODO: Extend the register allocator to support a new
5330 // "(locally) live temp" location so as to avoid always
5331 // going into the slow path when read barriers are enabled.
5332 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005333 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005334 // /* HeapReference<Class> */ temp = array->klass_
5335 __ movl(temp, Address(array, class_offset));
5336 codegen_->MaybeRecordImplicitNullCheck(instruction);
5337 __ MaybeUnpoisonHeapReference(temp);
5338
5339 // /* HeapReference<Class> */ temp = temp->component_type_
5340 __ movl(temp, Address(temp, component_offset));
5341 // If heap poisoning is enabled, no need to unpoison `temp`
5342 // nor the object reference in `register_value->klass`, as
5343 // we are comparing two poisoned references.
5344 __ cmpl(temp, Address(register_value, class_offset));
5345
5346 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5347 __ j(kEqual, &do_put);
5348 // If heap poisoning is enabled, the `temp` reference has
5349 // not been unpoisoned yet; unpoison it now.
5350 __ MaybeUnpoisonHeapReference(temp);
5351
5352 // /* HeapReference<Class> */ temp = temp->super_class_
5353 __ movl(temp, Address(temp, super_offset));
5354 // If heap poisoning is enabled, no need to unpoison
5355 // `temp`, as we are comparing against null below.
5356 __ testl(temp, temp);
5357 __ j(kNotEqual, slow_path->GetEntryLabel());
5358 __ Bind(&do_put);
5359 } else {
5360 __ j(kNotEqual, slow_path->GetEntryLabel());
5361 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005362 }
5363 }
5364
5365 if (kPoisonHeapReferences) {
5366 __ movl(temp, register_value);
5367 __ PoisonHeapReference(temp);
5368 __ movl(address, temp);
5369 } else {
5370 __ movl(address, register_value);
5371 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005372 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005373 codegen_->MaybeRecordImplicitNullCheck(instruction);
5374 }
5375
5376 Register card = locations->GetTemp(1).AsRegister<Register>();
5377 codegen_->MarkGCCard(
5378 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5379 __ Bind(&done);
5380
5381 if (slow_path != nullptr) {
5382 __ Bind(slow_path->GetExitLabel());
5383 }
5384
5385 break;
5386 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005387
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005388 case Primitive::kPrimInt: {
5389 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5390 Address address = index.IsConstant()
5391 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5392 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5393 if (value.IsRegister()) {
5394 __ movl(address, value.AsRegister<Register>());
5395 } else {
5396 DCHECK(value.IsConstant()) << value;
5397 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5398 __ movl(address, Immediate(v));
5399 }
5400 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005401 break;
5402 }
5403
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005404 case Primitive::kPrimLong: {
5405 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005406 if (index.IsConstant()) {
5407 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005408 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005409 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005410 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005411 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005412 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005413 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005414 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005415 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005416 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005417 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005418 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005419 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005420 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005421 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005422 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005423 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005424 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005425 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005426 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005427 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005428 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005429 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005430 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005431 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005432 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005433 Immediate(High32Bits(val)));
5434 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005435 }
5436 break;
5437 }
5438
Mark Mendell7c8d0092015-01-26 11:21:33 -05005439 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005440 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5441 Address address = index.IsConstant()
5442 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5443 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005444 if (value.IsFpuRegister()) {
5445 __ movss(address, value.AsFpuRegister<XmmRegister>());
5446 } else {
5447 DCHECK(value.IsConstant());
5448 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5449 __ movl(address, Immediate(v));
5450 }
5451 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005452 break;
5453 }
5454
5455 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005456 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5457 Address address = index.IsConstant()
5458 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5459 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005460 if (value.IsFpuRegister()) {
5461 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5462 } else {
5463 DCHECK(value.IsConstant());
5464 Address address_hi = index.IsConstant() ?
5465 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5466 offset + kX86WordSize) :
5467 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5468 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5469 __ movl(address, Immediate(Low32Bits(v)));
5470 codegen_->MaybeRecordImplicitNullCheck(instruction);
5471 __ movl(address_hi, Immediate(High32Bits(v)));
5472 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005473 break;
5474 }
5475
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005476 case Primitive::kPrimVoid:
5477 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005478 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005479 }
5480}
5481
5482void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5483 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005484 locations->SetInAt(0, Location::RequiresRegister());
5485 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005486}
5487
5488void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5489 LocationSummary* locations = instruction->GetLocations();
5490 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005491 Register obj = locations->InAt(0).AsRegister<Register>();
5492 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005493 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005494 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005495}
5496
5497void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005498 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5499 ? LocationSummary::kCallOnSlowPath
5500 : LocationSummary::kNoCall;
5501 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005502 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005503 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005504 if (instruction->HasUses()) {
5505 locations->SetOut(Location::SameAsFirstInput());
5506 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005507}
5508
5509void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5510 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005511 Location index_loc = locations->InAt(0);
5512 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005513 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005514 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005515
Mark Mendell99dbd682015-04-22 16:18:52 -04005516 if (length_loc.IsConstant()) {
5517 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5518 if (index_loc.IsConstant()) {
5519 // BCE will remove the bounds check if we are guarenteed to pass.
5520 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5521 if (index < 0 || index >= length) {
5522 codegen_->AddSlowPath(slow_path);
5523 __ jmp(slow_path->GetEntryLabel());
5524 } else {
5525 // Some optimization after BCE may have generated this, and we should not
5526 // generate a bounds check if it is a valid range.
5527 }
5528 return;
5529 }
5530
5531 // We have to reverse the jump condition because the length is the constant.
5532 Register index_reg = index_loc.AsRegister<Register>();
5533 __ cmpl(index_reg, Immediate(length));
5534 codegen_->AddSlowPath(slow_path);
5535 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005536 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005537 Register length = length_loc.AsRegister<Register>();
5538 if (index_loc.IsConstant()) {
5539 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5540 __ cmpl(length, Immediate(value));
5541 } else {
5542 __ cmpl(length, index_loc.AsRegister<Register>());
5543 }
5544 codegen_->AddSlowPath(slow_path);
5545 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005546 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005547}
5548
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005549void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005550 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005551}
5552
5553void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005554 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5555}
5556
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005557void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5558 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5559}
5560
5561void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005562 HBasicBlock* block = instruction->GetBlock();
5563 if (block->GetLoopInformation() != nullptr) {
5564 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5565 // The back edge will generate the suspend check.
5566 return;
5567 }
5568 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5569 // The goto will generate the suspend check.
5570 return;
5571 }
5572 GenerateSuspendCheck(instruction, nullptr);
5573}
5574
5575void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5576 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005577 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005578 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5579 if (slow_path == nullptr) {
5580 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5581 instruction->SetSlowPath(slow_path);
5582 codegen_->AddSlowPath(slow_path);
5583 if (successor != nullptr) {
5584 DCHECK(successor->IsLoopHeader());
5585 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5586 }
5587 } else {
5588 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5589 }
5590
Roland Levillain7c1559a2015-12-15 10:55:36 +00005591 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5592 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005593 if (successor == nullptr) {
5594 __ j(kNotEqual, slow_path->GetEntryLabel());
5595 __ Bind(slow_path->GetReturnLabel());
5596 } else {
5597 __ j(kEqual, codegen_->GetLabelOf(successor));
5598 __ jmp(slow_path->GetEntryLabel());
5599 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005600}
5601
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005602X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5603 return codegen_->GetAssembler();
5604}
5605
Mark Mendell7c8d0092015-01-26 11:21:33 -05005606void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005607 ScratchRegisterScope ensure_scratch(
5608 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5609 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5610 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5611 __ movl(temp_reg, Address(ESP, src + stack_offset));
5612 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005613}
5614
5615void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005616 ScratchRegisterScope ensure_scratch(
5617 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5618 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5619 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5620 __ movl(temp_reg, Address(ESP, src + stack_offset));
5621 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5622 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5623 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005624}
5625
5626void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005627 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005628 Location source = move->GetSource();
5629 Location destination = move->GetDestination();
5630
5631 if (source.IsRegister()) {
5632 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005633 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005634 } else if (destination.IsFpuRegister()) {
5635 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005636 } else {
5637 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005638 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005639 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005640 } else if (source.IsRegisterPair()) {
5641 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5642 // Create stack space for 2 elements.
5643 __ subl(ESP, Immediate(2 * elem_size));
5644 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5645 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5646 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5647 // And remove the temporary stack space we allocated.
5648 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005649 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005650 if (destination.IsRegister()) {
5651 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5652 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005653 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005654 } else if (destination.IsRegisterPair()) {
5655 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5656 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5657 __ psrlq(src_reg, Immediate(32));
5658 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005659 } else if (destination.IsStackSlot()) {
5660 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5661 } else {
5662 DCHECK(destination.IsDoubleStackSlot());
5663 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5664 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005665 } else if (source.IsStackSlot()) {
5666 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005667 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005668 } else if (destination.IsFpuRegister()) {
5669 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005670 } else {
5671 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005672 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5673 }
5674 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005675 if (destination.IsRegisterPair()) {
5676 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5677 __ movl(destination.AsRegisterPairHigh<Register>(),
5678 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5679 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005680 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5681 } else {
5682 DCHECK(destination.IsDoubleStackSlot()) << destination;
5683 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005684 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005685 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005686 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005687 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005688 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005689 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005690 if (value == 0) {
5691 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5692 } else {
5693 __ movl(destination.AsRegister<Register>(), Immediate(value));
5694 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005695 } else {
5696 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005697 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005698 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005699 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005700 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005701 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005702 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005703 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005704 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5705 if (value == 0) {
5706 // Easy handling of 0.0.
5707 __ xorps(dest, dest);
5708 } else {
5709 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005710 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5711 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5712 __ movl(temp, Immediate(value));
5713 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005714 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005715 } else {
5716 DCHECK(destination.IsStackSlot()) << destination;
5717 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5718 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005719 } else if (constant->IsLongConstant()) {
5720 int64_t value = constant->AsLongConstant()->GetValue();
5721 int32_t low_value = Low32Bits(value);
5722 int32_t high_value = High32Bits(value);
5723 Immediate low(low_value);
5724 Immediate high(high_value);
5725 if (destination.IsDoubleStackSlot()) {
5726 __ movl(Address(ESP, destination.GetStackIndex()), low);
5727 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5728 } else {
5729 __ movl(destination.AsRegisterPairLow<Register>(), low);
5730 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5731 }
5732 } else {
5733 DCHECK(constant->IsDoubleConstant());
5734 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005735 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005736 int32_t low_value = Low32Bits(value);
5737 int32_t high_value = High32Bits(value);
5738 Immediate low(low_value);
5739 Immediate high(high_value);
5740 if (destination.IsFpuRegister()) {
5741 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5742 if (value == 0) {
5743 // Easy handling of 0.0.
5744 __ xorpd(dest, dest);
5745 } else {
5746 __ pushl(high);
5747 __ pushl(low);
5748 __ movsd(dest, Address(ESP, 0));
5749 __ addl(ESP, Immediate(8));
5750 }
5751 } else {
5752 DCHECK(destination.IsDoubleStackSlot()) << destination;
5753 __ movl(Address(ESP, destination.GetStackIndex()), low);
5754 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5755 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005756 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005757 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005758 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005759 }
5760}
5761
Mark Mendella5c19ce2015-04-01 12:51:05 -04005762void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005763 Register suggested_scratch = reg == EAX ? EBX : EAX;
5764 ScratchRegisterScope ensure_scratch(
5765 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5766
5767 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5768 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5769 __ movl(Address(ESP, mem + stack_offset), reg);
5770 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005771}
5772
Mark Mendell7c8d0092015-01-26 11:21:33 -05005773void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005774 ScratchRegisterScope ensure_scratch(
5775 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5776
5777 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5778 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5779 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5780 __ movss(Address(ESP, mem + stack_offset), reg);
5781 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005782}
5783
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005784void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005785 ScratchRegisterScope ensure_scratch1(
5786 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005787
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005788 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5789 ScratchRegisterScope ensure_scratch2(
5790 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005791
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005792 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5793 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5794 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5795 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5796 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5797 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005798}
5799
5800void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005801 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005802 Location source = move->GetSource();
5803 Location destination = move->GetDestination();
5804
5805 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005806 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5807 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5808 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5809 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5810 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005811 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005812 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005813 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005814 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005815 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5816 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005817 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5818 // Use XOR Swap algorithm to avoid a temporary.
5819 DCHECK_NE(source.reg(), destination.reg());
5820 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5821 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5822 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5823 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5824 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5825 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5826 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005827 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5828 // Take advantage of the 16 bytes in the XMM register.
5829 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5830 Address stack(ESP, destination.GetStackIndex());
5831 // Load the double into the high doubleword.
5832 __ movhpd(reg, stack);
5833
5834 // Store the low double into the destination.
5835 __ movsd(stack, reg);
5836
5837 // Move the high double to the low double.
5838 __ psrldq(reg, Immediate(8));
5839 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5840 // Take advantage of the 16 bytes in the XMM register.
5841 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5842 Address stack(ESP, source.GetStackIndex());
5843 // Load the double into the high doubleword.
5844 __ movhpd(reg, stack);
5845
5846 // Store the low double into the destination.
5847 __ movsd(stack, reg);
5848
5849 // Move the high double to the low double.
5850 __ psrldq(reg, Immediate(8));
5851 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5852 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5853 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005854 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005855 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005856 }
5857}
5858
5859void ParallelMoveResolverX86::SpillScratch(int reg) {
5860 __ pushl(static_cast<Register>(reg));
5861}
5862
5863void ParallelMoveResolverX86::RestoreScratch(int reg) {
5864 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005865}
5866
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005867void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005868 InvokeRuntimeCallingConvention calling_convention;
5869 CodeGenerator::CreateLoadClassLocationSummary(
5870 cls,
5871 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005872 Location::RegisterLocation(EAX),
5873 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005874}
5875
5876void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005877 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005878 if (cls->NeedsAccessCheck()) {
5879 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5880 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5881 cls,
5882 cls->GetDexPc(),
5883 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005884 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005885 return;
5886 }
5887
Roland Levillain0d5a2812015-11-13 10:07:31 +00005888 Location out_loc = locations->Out();
5889 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005890 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005891
Calin Juravle580b6092015-10-06 17:35:58 +01005892 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005893 DCHECK(!cls->CanCallRuntime());
5894 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005895 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5896 GenerateGcRootFieldLoad(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005897 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005898 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005899 // /* GcRoot<mirror::Class>[] */ out =
5900 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5901 __ movl(out, Address(current_method,
5902 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005903 // /* GcRoot<mirror::Class> */ out = out[type_index]
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005904 GenerateGcRootFieldLoad(
5905 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005906
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005907 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5908 DCHECK(cls->CanCallRuntime());
5909 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5910 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5911 codegen_->AddSlowPath(slow_path);
5912
5913 if (!cls->IsInDexCache()) {
5914 __ testl(out, out);
5915 __ j(kEqual, slow_path->GetEntryLabel());
5916 }
5917
5918 if (cls->MustGenerateClinitCheck()) {
5919 GenerateClassInitializationCheck(slow_path, out);
5920 } else {
5921 __ Bind(slow_path->GetExitLabel());
5922 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005923 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005924 }
5925}
5926
5927void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5928 LocationSummary* locations =
5929 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5930 locations->SetInAt(0, Location::RequiresRegister());
5931 if (check->HasUses()) {
5932 locations->SetOut(Location::SameAsFirstInput());
5933 }
5934}
5935
5936void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005937 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005938 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005939 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005940 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005941 GenerateClassInitializationCheck(slow_path,
5942 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005943}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005944
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005945void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005946 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005947 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5948 Immediate(mirror::Class::kStatusInitialized));
5949 __ j(kLess, slow_path->GetEntryLabel());
5950 __ Bind(slow_path->GetExitLabel());
5951 // No need for memory fence, thanks to the X86 memory model.
5952}
5953
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005954HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
5955 HLoadString::LoadKind desired_string_load_kind) {
5956 if (kEmitCompilerReadBarrier) {
5957 switch (desired_string_load_kind) {
5958 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5959 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5960 case HLoadString::LoadKind::kBootImageAddress:
5961 // TODO: Implement for read barrier.
5962 return HLoadString::LoadKind::kDexCacheViaMethod;
5963 default:
5964 break;
5965 }
5966 }
5967 switch (desired_string_load_kind) {
5968 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5969 DCHECK(!GetCompilerOptions().GetCompilePic());
5970 break;
5971 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5972 DCHECK(GetCompilerOptions().GetCompilePic());
5973 FALLTHROUGH_INTENDED;
5974 case HLoadString::LoadKind::kDexCachePcRelative:
5975 DCHECK(!Runtime::Current()->UseJit()); // Note: boot image is also non-JIT.
5976 // We disable pc-relative load when there is an irreducible loop, as the optimization
5977 // is incompatible with it.
5978 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5979 // with irreducible loops.
5980 if (GetGraph()->HasIrreducibleLoops()) {
5981 return HLoadString::LoadKind::kDexCacheViaMethod;
5982 }
5983 break;
5984 case HLoadString::LoadKind::kBootImageAddress:
5985 break;
5986 case HLoadString::LoadKind::kDexCacheAddress:
5987 DCHECK(Runtime::Current()->UseJit());
5988 break;
5989 case HLoadString::LoadKind::kDexCacheViaMethod:
5990 break;
5991 }
5992 return desired_string_load_kind;
5993}
5994
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005995void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005996 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005997 ? LocationSummary::kCallOnSlowPath
5998 : LocationSummary::kNoCall;
5999 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006000 HLoadString::LoadKind load_kind = load->GetLoadKind();
6001 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6002 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6003 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6004 locations->SetInAt(0, Location::RequiresRegister());
6005 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006006 locations->SetOut(Location::RequiresRegister());
6007}
6008
6009void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006010 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006011 Location out_loc = locations->Out();
6012 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006013
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006014 switch (load->GetLoadKind()) {
6015 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6016 DCHECK(!kEmitCompilerReadBarrier);
6017 __ movl(out, Immediate(/* placeholder */ 0));
6018 codegen_->RecordStringPatch(load);
6019 return; // No dex cache slow path.
6020 }
6021 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6022 DCHECK(!kEmitCompilerReadBarrier);
6023 Register method_address = locations->InAt(0).AsRegister<Register>();
6024 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6025 codegen_->RecordStringPatch(load);
6026 return; // No dex cache slow path.
6027 }
6028 case HLoadString::LoadKind::kBootImageAddress: {
6029 DCHECK(!kEmitCompilerReadBarrier);
6030 DCHECK_NE(load->GetAddress(), 0u);
6031 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6032 __ movl(out, Immediate(address));
6033 codegen_->RecordSimplePatch();
6034 return; // No dex cache slow path.
6035 }
6036 case HLoadString::LoadKind::kDexCacheAddress: {
6037 DCHECK_NE(load->GetAddress(), 0u);
6038 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6039 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6040 break;
6041 }
6042 case HLoadString::LoadKind::kDexCachePcRelative: {
6043 Register base_reg = locations->InAt(0).AsRegister<Register>();
6044 uint32_t offset = load->GetDexCacheElementOffset();
6045 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
6046 GenerateGcRootFieldLoad(
6047 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6048 break;
6049 }
6050 case HLoadString::LoadKind::kDexCacheViaMethod: {
6051 Register current_method = locations->InAt(0).AsRegister<Register>();
6052
6053 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6054 GenerateGcRootFieldLoad(
6055 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6056
6057 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6058 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6059 // /* GcRoot<mirror::String> */ out = out[string_index]
6060 GenerateGcRootFieldLoad(
6061 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6062 break;
6063 }
6064 default:
6065 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6066 UNREACHABLE();
6067 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006068
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006069 if (!load->IsInDexCache()) {
6070 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6071 codegen_->AddSlowPath(slow_path);
6072 __ testl(out, out);
6073 __ j(kEqual, slow_path->GetEntryLabel());
6074 __ Bind(slow_path->GetExitLabel());
6075 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006076}
6077
David Brazdilcb1c0552015-08-04 16:22:25 +01006078static Address GetExceptionTlsAddress() {
6079 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6080}
6081
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006082void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6083 LocationSummary* locations =
6084 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6085 locations->SetOut(Location::RequiresRegister());
6086}
6087
6088void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006089 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6090}
6091
6092void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6093 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6094}
6095
6096void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6097 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006098}
6099
6100void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6101 LocationSummary* locations =
6102 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6103 InvokeRuntimeCallingConvention calling_convention;
6104 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6105}
6106
6107void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006108 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6109 instruction,
6110 instruction->GetDexPc(),
6111 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006112 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006113}
6114
Roland Levillain7c1559a2015-12-15 10:55:36 +00006115static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6116 return kEmitCompilerReadBarrier &&
6117 (kUseBakerReadBarrier ||
6118 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6119 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6120 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6121}
6122
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006123void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006124 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006125 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6126 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006127 case TypeCheckKind::kExactCheck:
6128 case TypeCheckKind::kAbstractClassCheck:
6129 case TypeCheckKind::kClassHierarchyCheck:
6130 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006131 call_kind =
6132 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006133 break;
6134 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006135 case TypeCheckKind::kUnresolvedCheck:
6136 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006137 call_kind = LocationSummary::kCallOnSlowPath;
6138 break;
6139 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006140
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006141 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006142 locations->SetInAt(0, Location::RequiresRegister());
6143 locations->SetInAt(1, Location::Any());
6144 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6145 locations->SetOut(Location::RequiresRegister());
6146 // When read barriers are enabled, we need a temporary register for
6147 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006148 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006149 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006150 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006151}
6152
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006153void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006154 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006155 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006156 Location obj_loc = locations->InAt(0);
6157 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006158 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006159 Location out_loc = locations->Out();
6160 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006161 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006162 locations->GetTemp(0) :
6163 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006164 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6166 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6167 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006168 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006169 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006170
6171 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006172 // Avoid null check if we know obj is not null.
6173 if (instruction->MustDoNullCheck()) {
6174 __ testl(obj, obj);
6175 __ j(kEqual, &zero);
6176 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006177
Roland Levillain0d5a2812015-11-13 10:07:31 +00006178 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006179 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006180
Roland Levillain7c1559a2015-12-15 10:55:36 +00006181 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006182 case TypeCheckKind::kExactCheck: {
6183 if (cls.IsRegister()) {
6184 __ cmpl(out, cls.AsRegister<Register>());
6185 } else {
6186 DCHECK(cls.IsStackSlot()) << cls;
6187 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6188 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006189
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006190 // Classes must be equal for the instanceof to succeed.
6191 __ j(kNotEqual, &zero);
6192 __ movl(out, Immediate(1));
6193 __ jmp(&done);
6194 break;
6195 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006196
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006197 case TypeCheckKind::kAbstractClassCheck: {
6198 // If the class is abstract, we eagerly fetch the super class of the
6199 // object to avoid doing a comparison we know will fail.
6200 NearLabel loop;
6201 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006202 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006203 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006204 __ testl(out, out);
6205 // If `out` is null, we use it for the result, and jump to `done`.
6206 __ j(kEqual, &done);
6207 if (cls.IsRegister()) {
6208 __ cmpl(out, cls.AsRegister<Register>());
6209 } else {
6210 DCHECK(cls.IsStackSlot()) << cls;
6211 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6212 }
6213 __ j(kNotEqual, &loop);
6214 __ movl(out, Immediate(1));
6215 if (zero.IsLinked()) {
6216 __ jmp(&done);
6217 }
6218 break;
6219 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006220
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006221 case TypeCheckKind::kClassHierarchyCheck: {
6222 // Walk over the class hierarchy to find a match.
6223 NearLabel loop, success;
6224 __ Bind(&loop);
6225 if (cls.IsRegister()) {
6226 __ cmpl(out, cls.AsRegister<Register>());
6227 } else {
6228 DCHECK(cls.IsStackSlot()) << cls;
6229 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6230 }
6231 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006232 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006233 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006234 __ testl(out, out);
6235 __ j(kNotEqual, &loop);
6236 // If `out` is null, we use it for the result, and jump to `done`.
6237 __ jmp(&done);
6238 __ Bind(&success);
6239 __ movl(out, Immediate(1));
6240 if (zero.IsLinked()) {
6241 __ jmp(&done);
6242 }
6243 break;
6244 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006245
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006246 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006247 // Do an exact check.
6248 NearLabel exact_check;
6249 if (cls.IsRegister()) {
6250 __ cmpl(out, cls.AsRegister<Register>());
6251 } else {
6252 DCHECK(cls.IsStackSlot()) << cls;
6253 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6254 }
6255 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006256 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006257 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006258 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006259 __ testl(out, out);
6260 // If `out` is null, we use it for the result, and jump to `done`.
6261 __ j(kEqual, &done);
6262 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6263 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006264 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006265 __ movl(out, Immediate(1));
6266 __ jmp(&done);
6267 break;
6268 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006269
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006270 case TypeCheckKind::kArrayCheck: {
6271 if (cls.IsRegister()) {
6272 __ cmpl(out, cls.AsRegister<Register>());
6273 } else {
6274 DCHECK(cls.IsStackSlot()) << cls;
6275 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6276 }
6277 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006278 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6279 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006280 codegen_->AddSlowPath(slow_path);
6281 __ j(kNotEqual, slow_path->GetEntryLabel());
6282 __ movl(out, Immediate(1));
6283 if (zero.IsLinked()) {
6284 __ jmp(&done);
6285 }
6286 break;
6287 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006288
Calin Juravle98893e12015-10-02 21:05:03 +01006289 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006290 case TypeCheckKind::kInterfaceCheck: {
6291 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006292 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006293 // cases.
6294 //
6295 // We cannot directly call the InstanceofNonTrivial runtime
6296 // entry point without resorting to a type checking slow path
6297 // here (i.e. by calling InvokeRuntime directly), as it would
6298 // require to assign fixed registers for the inputs of this
6299 // HInstanceOf instruction (following the runtime calling
6300 // convention), which might be cluttered by the potential first
6301 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006302 //
6303 // TODO: Introduce a new runtime entry point taking the object
6304 // to test (instead of its class) as argument, and let it deal
6305 // with the read barrier issues. This will let us refactor this
6306 // case of the `switch` code as it was previously (with a direct
6307 // call to the runtime not using a type checking slow path).
6308 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006309 DCHECK(locations->OnlyCallsOnSlowPath());
6310 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6311 /* is_fatal */ false);
6312 codegen_->AddSlowPath(slow_path);
6313 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006314 if (zero.IsLinked()) {
6315 __ jmp(&done);
6316 }
6317 break;
6318 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006319 }
6320
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006321 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006322 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006323 __ xorl(out, out);
6324 }
6325
6326 if (done.IsLinked()) {
6327 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006328 }
6329
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006330 if (slow_path != nullptr) {
6331 __ Bind(slow_path->GetExitLabel());
6332 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006333}
6334
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006335void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006336 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6337 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006338 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6339 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006340 case TypeCheckKind::kExactCheck:
6341 case TypeCheckKind::kAbstractClassCheck:
6342 case TypeCheckKind::kClassHierarchyCheck:
6343 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006344 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6345 LocationSummary::kCallOnSlowPath :
6346 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006347 break;
6348 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006349 case TypeCheckKind::kUnresolvedCheck:
6350 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006351 call_kind = LocationSummary::kCallOnSlowPath;
6352 break;
6353 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006354 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6355 locations->SetInAt(0, Location::RequiresRegister());
6356 locations->SetInAt(1, Location::Any());
6357 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6358 locations->AddTemp(Location::RequiresRegister());
6359 // When read barriers are enabled, we need an additional temporary
6360 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006361 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006362 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006363 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006364}
6365
6366void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006367 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006368 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006369 Location obj_loc = locations->InAt(0);
6370 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006371 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006372 Location temp_loc = locations->GetTemp(0);
6373 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006374 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006375 locations->GetTemp(1) :
6376 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006377 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6378 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6379 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6380 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006381
Roland Levillain0d5a2812015-11-13 10:07:31 +00006382 bool is_type_check_slow_path_fatal =
6383 (type_check_kind == TypeCheckKind::kExactCheck ||
6384 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6385 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6386 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6387 !instruction->CanThrowIntoCatchBlock();
6388 SlowPathCode* type_check_slow_path =
6389 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6390 is_type_check_slow_path_fatal);
6391 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006392
Roland Levillain0d5a2812015-11-13 10:07:31 +00006393 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006394 // Avoid null check if we know obj is not null.
6395 if (instruction->MustDoNullCheck()) {
6396 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006397 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006398 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006399
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006401 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006402
Roland Levillain0d5a2812015-11-13 10:07:31 +00006403 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006404 case TypeCheckKind::kExactCheck:
6405 case TypeCheckKind::kArrayCheck: {
6406 if (cls.IsRegister()) {
6407 __ cmpl(temp, cls.AsRegister<Register>());
6408 } else {
6409 DCHECK(cls.IsStackSlot()) << cls;
6410 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6411 }
6412 // Jump to slow path for throwing the exception or doing a
6413 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006414 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006415 break;
6416 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006417
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006418 case TypeCheckKind::kAbstractClassCheck: {
6419 // If the class is abstract, we eagerly fetch the super class of the
6420 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006421 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006422 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006423 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006424 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006425
6426 // If the class reference currently in `temp` is not null, jump
6427 // to the `compare_classes` label to compare it with the checked
6428 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006429 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430 __ j(kNotEqual, &compare_classes);
6431 // Otherwise, jump to the slow path to throw the exception.
6432 //
6433 // But before, move back the object's class into `temp` before
6434 // going into the slow path, as it has been overwritten in the
6435 // meantime.
6436 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006437 GenerateReferenceLoadTwoRegisters(
6438 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006439 __ jmp(type_check_slow_path->GetEntryLabel());
6440
6441 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006442 if (cls.IsRegister()) {
6443 __ cmpl(temp, cls.AsRegister<Register>());
6444 } else {
6445 DCHECK(cls.IsStackSlot()) << cls;
6446 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6447 }
6448 __ j(kNotEqual, &loop);
6449 break;
6450 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006451
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006452 case TypeCheckKind::kClassHierarchyCheck: {
6453 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006454 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006455 __ Bind(&loop);
6456 if (cls.IsRegister()) {
6457 __ cmpl(temp, cls.AsRegister<Register>());
6458 } else {
6459 DCHECK(cls.IsStackSlot()) << cls;
6460 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6461 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006462 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006463
Roland Levillain0d5a2812015-11-13 10:07:31 +00006464 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006465 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006466
6467 // If the class reference currently in `temp` is not null, jump
6468 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006469 __ testl(temp, temp);
6470 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006471 // Otherwise, jump to the slow path to throw the exception.
6472 //
6473 // But before, move back the object's class into `temp` before
6474 // going into the slow path, as it has been overwritten in the
6475 // meantime.
6476 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006477 GenerateReferenceLoadTwoRegisters(
6478 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006479 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006480 break;
6481 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006482
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006483 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006484 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006485 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006486 if (cls.IsRegister()) {
6487 __ cmpl(temp, cls.AsRegister<Register>());
6488 } else {
6489 DCHECK(cls.IsStackSlot()) << cls;
6490 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6491 }
6492 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006493
6494 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006495 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006496 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006497
6498 // If the component type is not null (i.e. the object is indeed
6499 // an array), jump to label `check_non_primitive_component_type`
6500 // to further check that this component type is not a primitive
6501 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006502 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006503 __ j(kNotEqual, &check_non_primitive_component_type);
6504 // Otherwise, jump to the slow path to throw the exception.
6505 //
6506 // But before, move back the object's class into `temp` before
6507 // going into the slow path, as it has been overwritten in the
6508 // meantime.
6509 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006510 GenerateReferenceLoadTwoRegisters(
6511 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006512 __ jmp(type_check_slow_path->GetEntryLabel());
6513
6514 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006515 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006516 __ j(kEqual, &done);
6517 // Same comment as above regarding `temp` and the slow path.
6518 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006519 GenerateReferenceLoadTwoRegisters(
6520 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006521 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006522 break;
6523 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006524
Calin Juravle98893e12015-10-02 21:05:03 +01006525 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006526 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006527 // We always go into the type check slow path for the unresolved
6528 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006529 //
6530 // We cannot directly call the CheckCast runtime entry point
6531 // without resorting to a type checking slow path here (i.e. by
6532 // calling InvokeRuntime directly), as it would require to
6533 // assign fixed registers for the inputs of this HInstanceOf
6534 // instruction (following the runtime calling convention), which
6535 // might be cluttered by the potential first read barrier
6536 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006537 //
6538 // TODO: Introduce a new runtime entry point taking the object
6539 // to test (instead of its class) as argument, and let it deal
6540 // with the read barrier issues. This will let us refactor this
6541 // case of the `switch` code as it was previously (with a direct
6542 // call to the runtime not using a type checking slow path).
6543 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006544 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006545 break;
6546 }
6547 __ Bind(&done);
6548
Roland Levillain0d5a2812015-11-13 10:07:31 +00006549 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006550}
6551
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006552void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6553 LocationSummary* locations =
6554 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6555 InvokeRuntimeCallingConvention calling_convention;
6556 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6557}
6558
6559void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006560 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6561 : QUICK_ENTRY_POINT(pUnlockObject),
6562 instruction,
6563 instruction->GetDexPc(),
6564 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006565 if (instruction->IsEnter()) {
6566 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6567 } else {
6568 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6569 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006570}
6571
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006572void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6573void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6574void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6575
6576void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6577 LocationSummary* locations =
6578 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6579 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6580 || instruction->GetResultType() == Primitive::kPrimLong);
6581 locations->SetInAt(0, Location::RequiresRegister());
6582 locations->SetInAt(1, Location::Any());
6583 locations->SetOut(Location::SameAsFirstInput());
6584}
6585
6586void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6587 HandleBitwiseOperation(instruction);
6588}
6589
6590void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6591 HandleBitwiseOperation(instruction);
6592}
6593
6594void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6595 HandleBitwiseOperation(instruction);
6596}
6597
6598void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6599 LocationSummary* locations = instruction->GetLocations();
6600 Location first = locations->InAt(0);
6601 Location second = locations->InAt(1);
6602 DCHECK(first.Equals(locations->Out()));
6603
6604 if (instruction->GetResultType() == Primitive::kPrimInt) {
6605 if (second.IsRegister()) {
6606 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006607 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006608 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006609 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006610 } else {
6611 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006612 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006613 }
6614 } else if (second.IsConstant()) {
6615 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006616 __ andl(first.AsRegister<Register>(),
6617 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006618 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006619 __ orl(first.AsRegister<Register>(),
6620 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006621 } else {
6622 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006623 __ xorl(first.AsRegister<Register>(),
6624 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006625 }
6626 } else {
6627 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006628 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006629 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006630 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006631 } else {
6632 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006633 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006634 }
6635 }
6636 } else {
6637 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6638 if (second.IsRegisterPair()) {
6639 if (instruction->IsAnd()) {
6640 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6641 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6642 } else if (instruction->IsOr()) {
6643 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6644 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6645 } else {
6646 DCHECK(instruction->IsXor());
6647 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6648 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6649 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006650 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006651 if (instruction->IsAnd()) {
6652 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6653 __ andl(first.AsRegisterPairHigh<Register>(),
6654 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6655 } else if (instruction->IsOr()) {
6656 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6657 __ orl(first.AsRegisterPairHigh<Register>(),
6658 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6659 } else {
6660 DCHECK(instruction->IsXor());
6661 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6662 __ xorl(first.AsRegisterPairHigh<Register>(),
6663 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6664 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006665 } else {
6666 DCHECK(second.IsConstant()) << second;
6667 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006668 int32_t low_value = Low32Bits(value);
6669 int32_t high_value = High32Bits(value);
6670 Immediate low(low_value);
6671 Immediate high(high_value);
6672 Register first_low = first.AsRegisterPairLow<Register>();
6673 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006674 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006675 if (low_value == 0) {
6676 __ xorl(first_low, first_low);
6677 } else if (low_value != -1) {
6678 __ andl(first_low, low);
6679 }
6680 if (high_value == 0) {
6681 __ xorl(first_high, first_high);
6682 } else if (high_value != -1) {
6683 __ andl(first_high, high);
6684 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006685 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006686 if (low_value != 0) {
6687 __ orl(first_low, low);
6688 }
6689 if (high_value != 0) {
6690 __ orl(first_high, high);
6691 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006692 } else {
6693 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006694 if (low_value != 0) {
6695 __ xorl(first_low, low);
6696 }
6697 if (high_value != 0) {
6698 __ xorl(first_high, high);
6699 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006700 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006701 }
6702 }
6703}
6704
Roland Levillain7c1559a2015-12-15 10:55:36 +00006705void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6706 Location out,
6707 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006708 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006709 Register out_reg = out.AsRegister<Register>();
6710 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006711 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006712 if (kUseBakerReadBarrier) {
6713 // Load with fast path based Baker's read barrier.
6714 // /* HeapReference<Object> */ out = *(out + offset)
6715 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006716 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006717 } else {
6718 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006719 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006720 // in the following move operation, as we will need it for the
6721 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006722 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006723 // /* HeapReference<Object> */ out = *(out + offset)
6724 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006725 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006726 }
6727 } else {
6728 // Plain load with no read barrier.
6729 // /* HeapReference<Object> */ out = *(out + offset)
6730 __ movl(out_reg, Address(out_reg, offset));
6731 __ MaybeUnpoisonHeapReference(out_reg);
6732 }
6733}
6734
6735void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6736 Location out,
6737 Location obj,
6738 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006739 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006740 Register out_reg = out.AsRegister<Register>();
6741 Register obj_reg = obj.AsRegister<Register>();
6742 if (kEmitCompilerReadBarrier) {
6743 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006744 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006745 // Load with fast path based Baker's read barrier.
6746 // /* HeapReference<Object> */ out = *(obj + offset)
6747 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006748 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006749 } else {
6750 // Load with slow path based read barrier.
6751 // /* HeapReference<Object> */ out = *(obj + offset)
6752 __ movl(out_reg, Address(obj_reg, offset));
6753 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6754 }
6755 } else {
6756 // Plain load with no read barrier.
6757 // /* HeapReference<Object> */ out = *(obj + offset)
6758 __ movl(out_reg, Address(obj_reg, offset));
6759 __ MaybeUnpoisonHeapReference(out_reg);
6760 }
6761}
6762
6763void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6764 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006765 const Address& address,
6766 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006767 Register root_reg = root.AsRegister<Register>();
6768 if (kEmitCompilerReadBarrier) {
6769 if (kUseBakerReadBarrier) {
6770 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6771 // Baker's read barrier are used:
6772 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006773 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006774 // if (Thread::Current()->GetIsGcMarking()) {
6775 // root = ReadBarrier::Mark(root)
6776 // }
6777
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006778 // /* GcRoot<mirror::Object> */ root = *address
6779 __ movl(root_reg, address);
6780 if (fixup_label != nullptr) {
6781 __ Bind(fixup_label);
6782 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006783 static_assert(
6784 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6785 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6786 "have different sizes.");
6787 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6788 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6789 "have different sizes.");
6790
6791 // Slow path used to mark the GC root `root`.
6792 SlowPathCode* slow_path =
6793 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6794 codegen_->AddSlowPath(slow_path);
6795
6796 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6797 Immediate(0));
6798 __ j(kNotEqual, slow_path->GetEntryLabel());
6799 __ Bind(slow_path->GetExitLabel());
6800 } else {
6801 // GC root loaded through a slow path for read barriers other
6802 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006803 // /* GcRoot<mirror::Object>* */ root = address
6804 __ leal(root_reg, address);
6805 if (fixup_label != nullptr) {
6806 __ Bind(fixup_label);
6807 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006808 // /* mirror::Object* */ root = root->Read()
6809 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6810 }
6811 } else {
6812 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006813 // /* GcRoot<mirror::Object> */ root = *address
6814 __ movl(root_reg, address);
6815 if (fixup_label != nullptr) {
6816 __ Bind(fixup_label);
6817 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006818 // Note that GC roots are not affected by heap poisoning, thus we
6819 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006820 }
6821}
6822
6823void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6824 Location ref,
6825 Register obj,
6826 uint32_t offset,
6827 Location temp,
6828 bool needs_null_check) {
6829 DCHECK(kEmitCompilerReadBarrier);
6830 DCHECK(kUseBakerReadBarrier);
6831
6832 // /* HeapReference<Object> */ ref = *(obj + offset)
6833 Address src(obj, offset);
6834 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6835}
6836
6837void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6838 Location ref,
6839 Register obj,
6840 uint32_t data_offset,
6841 Location index,
6842 Location temp,
6843 bool needs_null_check) {
6844 DCHECK(kEmitCompilerReadBarrier);
6845 DCHECK(kUseBakerReadBarrier);
6846
6847 // /* HeapReference<Object> */ ref =
6848 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6849 Address src = index.IsConstant() ?
6850 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6851 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6852 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6853}
6854
6855void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6856 Location ref,
6857 Register obj,
6858 const Address& src,
6859 Location temp,
6860 bool needs_null_check) {
6861 DCHECK(kEmitCompilerReadBarrier);
6862 DCHECK(kUseBakerReadBarrier);
6863
6864 // In slow path based read barriers, the read barrier call is
6865 // inserted after the original load. However, in fast path based
6866 // Baker's read barriers, we need to perform the load of
6867 // mirror::Object::monitor_ *before* the original reference load.
6868 // This load-load ordering is required by the read barrier.
6869 // The fast path/slow path (for Baker's algorithm) should look like:
6870 //
6871 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6872 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6873 // HeapReference<Object> ref = *src; // Original reference load.
6874 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6875 // if (is_gray) {
6876 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6877 // }
6878 //
6879 // Note: the original implementation in ReadBarrier::Barrier is
6880 // slightly more complex as:
6881 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006882 // the high-bits of rb_state, which are expected to be all zeroes
6883 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6884 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006885 // - it performs additional checks that we do not do here for
6886 // performance reasons.
6887
6888 Register ref_reg = ref.AsRegister<Register>();
6889 Register temp_reg = temp.AsRegister<Register>();
6890 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6891
6892 // /* int32_t */ monitor = obj->monitor_
6893 __ movl(temp_reg, Address(obj, monitor_offset));
6894 if (needs_null_check) {
6895 MaybeRecordImplicitNullCheck(instruction);
6896 }
6897 // /* LockWord */ lock_word = LockWord(monitor)
6898 static_assert(sizeof(LockWord) == sizeof(int32_t),
6899 "art::LockWord and int32_t have different sizes.");
6900 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6901 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6902 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6903 static_assert(
6904 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6905 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6906
6907 // Load fence to prevent load-load reordering.
6908 // Note that this is a no-op, thanks to the x86 memory model.
6909 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6910
6911 // The actual reference load.
6912 // /* HeapReference<Object> */ ref = *src
6913 __ movl(ref_reg, src);
6914
6915 // Object* ref = ref_addr->AsMirrorPtr()
6916 __ MaybeUnpoisonHeapReference(ref_reg);
6917
6918 // Slow path used to mark the object `ref` when it is gray.
6919 SlowPathCode* slow_path =
6920 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6921 AddSlowPath(slow_path);
6922
6923 // if (rb_state == ReadBarrier::gray_ptr_)
6924 // ref = ReadBarrier::Mark(ref);
6925 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6926 __ j(kEqual, slow_path->GetEntryLabel());
6927 __ Bind(slow_path->GetExitLabel());
6928}
6929
6930void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6931 Location out,
6932 Location ref,
6933 Location obj,
6934 uint32_t offset,
6935 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006936 DCHECK(kEmitCompilerReadBarrier);
6937
Roland Levillain7c1559a2015-12-15 10:55:36 +00006938 // Insert a slow path based read barrier *after* the reference load.
6939 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006940 // If heap poisoning is enabled, the unpoisoning of the loaded
6941 // reference will be carried out by the runtime within the slow
6942 // path.
6943 //
6944 // Note that `ref` currently does not get unpoisoned (when heap
6945 // poisoning is enabled), which is alright as the `ref` argument is
6946 // not used by the artReadBarrierSlow entry point.
6947 //
6948 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6949 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6950 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6951 AddSlowPath(slow_path);
6952
Roland Levillain0d5a2812015-11-13 10:07:31 +00006953 __ jmp(slow_path->GetEntryLabel());
6954 __ Bind(slow_path->GetExitLabel());
6955}
6956
Roland Levillain7c1559a2015-12-15 10:55:36 +00006957void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6958 Location out,
6959 Location ref,
6960 Location obj,
6961 uint32_t offset,
6962 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006963 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006964 // Baker's read barriers shall be handled by the fast path
6965 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6966 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006967 // If heap poisoning is enabled, unpoisoning will be taken care of
6968 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006969 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006970 } else if (kPoisonHeapReferences) {
6971 __ UnpoisonHeapReference(out.AsRegister<Register>());
6972 }
6973}
6974
Roland Levillain7c1559a2015-12-15 10:55:36 +00006975void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6976 Location out,
6977 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006978 DCHECK(kEmitCompilerReadBarrier);
6979
Roland Levillain7c1559a2015-12-15 10:55:36 +00006980 // Insert a slow path based read barrier *after* the GC root load.
6981 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006982 // Note that GC roots are not affected by heap poisoning, so we do
6983 // not need to do anything special for this here.
6984 SlowPathCode* slow_path =
6985 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6986 AddSlowPath(slow_path);
6987
Roland Levillain0d5a2812015-11-13 10:07:31 +00006988 __ jmp(slow_path->GetEntryLabel());
6989 __ Bind(slow_path->GetExitLabel());
6990}
6991
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006992void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006993 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006994 LOG(FATAL) << "Unreachable";
6995}
6996
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006997void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006998 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006999 LOG(FATAL) << "Unreachable";
7000}
7001
Mark Mendellfe57faa2015-09-18 09:26:15 -04007002// Simple implementation of packed switch - generate cascaded compare/jumps.
7003void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7004 LocationSummary* locations =
7005 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7006 locations->SetInAt(0, Location::RequiresRegister());
7007}
7008
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007009void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7010 int32_t lower_bound,
7011 uint32_t num_entries,
7012 HBasicBlock* switch_block,
7013 HBasicBlock* default_block) {
7014 // Figure out the correct compare values and jump conditions.
7015 // Handle the first compare/branch as a special case because it might
7016 // jump to the default case.
7017 DCHECK_GT(num_entries, 2u);
7018 Condition first_condition;
7019 uint32_t index;
7020 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7021 if (lower_bound != 0) {
7022 first_condition = kLess;
7023 __ cmpl(value_reg, Immediate(lower_bound));
7024 __ j(first_condition, codegen_->GetLabelOf(default_block));
7025 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007026
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007027 index = 1;
7028 } else {
7029 // Handle all the compare/jumps below.
7030 first_condition = kBelow;
7031 index = 0;
7032 }
7033
7034 // Handle the rest of the compare/jumps.
7035 for (; index + 1 < num_entries; index += 2) {
7036 int32_t compare_to_value = lower_bound + index + 1;
7037 __ cmpl(value_reg, Immediate(compare_to_value));
7038 // Jump to successors[index] if value < case_value[index].
7039 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7040 // Jump to successors[index + 1] if value == case_value[index + 1].
7041 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7042 }
7043
7044 if (index != num_entries) {
7045 // There are an odd number of entries. Handle the last one.
7046 DCHECK_EQ(index + 1, num_entries);
7047 __ cmpl(value_reg, Immediate(lower_bound + index));
7048 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007049 }
7050
7051 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007052 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7053 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007054 }
7055}
7056
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007057void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7058 int32_t lower_bound = switch_instr->GetStartValue();
7059 uint32_t num_entries = switch_instr->GetNumEntries();
7060 LocationSummary* locations = switch_instr->GetLocations();
7061 Register value_reg = locations->InAt(0).AsRegister<Register>();
7062
7063 GenPackedSwitchWithCompares(value_reg,
7064 lower_bound,
7065 num_entries,
7066 switch_instr->GetBlock(),
7067 switch_instr->GetDefaultBlock());
7068}
7069
Mark Mendell805b3b52015-09-18 14:10:29 -04007070void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7071 LocationSummary* locations =
7072 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7073 locations->SetInAt(0, Location::RequiresRegister());
7074
7075 // Constant area pointer.
7076 locations->SetInAt(1, Location::RequiresRegister());
7077
7078 // And the temporary we need.
7079 locations->AddTemp(Location::RequiresRegister());
7080}
7081
7082void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7083 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007084 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007085 LocationSummary* locations = switch_instr->GetLocations();
7086 Register value_reg = locations->InAt(0).AsRegister<Register>();
7087 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7088
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007089 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7090 GenPackedSwitchWithCompares(value_reg,
7091 lower_bound,
7092 num_entries,
7093 switch_instr->GetBlock(),
7094 default_block);
7095 return;
7096 }
7097
Mark Mendell805b3b52015-09-18 14:10:29 -04007098 // Optimizing has a jump area.
7099 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7100 Register constant_area = locations->InAt(1).AsRegister<Register>();
7101
7102 // Remove the bias, if needed.
7103 if (lower_bound != 0) {
7104 __ leal(temp_reg, Address(value_reg, -lower_bound));
7105 value_reg = temp_reg;
7106 }
7107
7108 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007109 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007110 __ cmpl(value_reg, Immediate(num_entries - 1));
7111 __ j(kAbove, codegen_->GetLabelOf(default_block));
7112
7113 // We are in the range of the table.
7114 // Load (target-constant_area) from the jump table, indexing by the value.
7115 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7116
7117 // Compute the actual target address by adding in constant_area.
7118 __ addl(temp_reg, constant_area);
7119
7120 // And jump.
7121 __ jmp(temp_reg);
7122}
7123
Mark Mendell0616ae02015-04-17 12:49:27 -04007124void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7125 HX86ComputeBaseMethodAddress* insn) {
7126 LocationSummary* locations =
7127 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7128 locations->SetOut(Location::RequiresRegister());
7129}
7130
7131void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7132 HX86ComputeBaseMethodAddress* insn) {
7133 LocationSummary* locations = insn->GetLocations();
7134 Register reg = locations->Out().AsRegister<Register>();
7135
7136 // Generate call to next instruction.
7137 Label next_instruction;
7138 __ call(&next_instruction);
7139 __ Bind(&next_instruction);
7140
7141 // Remember this offset for later use with constant area.
7142 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7143
7144 // Grab the return address off the stack.
7145 __ popl(reg);
7146}
7147
7148void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7149 HX86LoadFromConstantTable* insn) {
7150 LocationSummary* locations =
7151 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7152
7153 locations->SetInAt(0, Location::RequiresRegister());
7154 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7155
7156 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007157 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007158 return;
7159 }
7160
7161 switch (insn->GetType()) {
7162 case Primitive::kPrimFloat:
7163 case Primitive::kPrimDouble:
7164 locations->SetOut(Location::RequiresFpuRegister());
7165 break;
7166
7167 case Primitive::kPrimInt:
7168 locations->SetOut(Location::RequiresRegister());
7169 break;
7170
7171 default:
7172 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7173 }
7174}
7175
7176void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007177 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007178 return;
7179 }
7180
7181 LocationSummary* locations = insn->GetLocations();
7182 Location out = locations->Out();
7183 Register const_area = locations->InAt(0).AsRegister<Register>();
7184 HConstant *value = insn->GetConstant();
7185
7186 switch (insn->GetType()) {
7187 case Primitive::kPrimFloat:
7188 __ movss(out.AsFpuRegister<XmmRegister>(),
7189 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7190 break;
7191
7192 case Primitive::kPrimDouble:
7193 __ movsd(out.AsFpuRegister<XmmRegister>(),
7194 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7195 break;
7196
7197 case Primitive::kPrimInt:
7198 __ movl(out.AsRegister<Register>(),
7199 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7200 break;
7201
7202 default:
7203 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7204 }
7205}
7206
Mark Mendell0616ae02015-04-17 12:49:27 -04007207/**
7208 * Class to handle late fixup of offsets into constant area.
7209 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007210class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007211 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007212 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7213 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7214
7215 protected:
7216 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7217
7218 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007219
7220 private:
7221 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7222 // Patch the correct offset for the instruction. The place to patch is the
7223 // last 4 bytes of the instruction.
7224 // The value to patch is the distance from the offset in the constant area
7225 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007226 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7227 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007228
7229 // Patch in the right value.
7230 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7231 }
7232
Mark Mendell0616ae02015-04-17 12:49:27 -04007233 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007234 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007235};
7236
Mark Mendell805b3b52015-09-18 14:10:29 -04007237/**
7238 * Class to handle late fixup of offsets to a jump table that will be created in the
7239 * constant area.
7240 */
7241class JumpTableRIPFixup : public RIPFixup {
7242 public:
7243 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7244 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7245
7246 void CreateJumpTable() {
7247 X86Assembler* assembler = codegen_->GetAssembler();
7248
7249 // Ensure that the reference to the jump table has the correct offset.
7250 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7251 SetOffset(offset_in_constant_table);
7252
7253 // The label values in the jump table are computed relative to the
7254 // instruction addressing the constant area.
7255 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7256
7257 // Populate the jump table with the correct values for the jump table.
7258 int32_t num_entries = switch_instr_->GetNumEntries();
7259 HBasicBlock* block = switch_instr_->GetBlock();
7260 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7261 // The value that we want is the target offset - the position of the table.
7262 for (int32_t i = 0; i < num_entries; i++) {
7263 HBasicBlock* b = successors[i];
7264 Label* l = codegen_->GetLabelOf(b);
7265 DCHECK(l->IsBound());
7266 int32_t offset_to_block = l->Position() - relative_offset;
7267 assembler->AppendInt32(offset_to_block);
7268 }
7269 }
7270
7271 private:
7272 const HX86PackedSwitch* switch_instr_;
7273};
7274
7275void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7276 // Generate the constant area if needed.
7277 X86Assembler* assembler = GetAssembler();
7278 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7279 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7280 // byte values.
7281 assembler->Align(4, 0);
7282 constant_area_start_ = assembler->CodeSize();
7283
7284 // Populate any jump tables.
7285 for (auto jump_table : fixups_to_jump_tables_) {
7286 jump_table->CreateJumpTable();
7287 }
7288
7289 // And now add the constant area to the generated code.
7290 assembler->AddConstantArea();
7291 }
7292
7293 // And finish up.
7294 CodeGenerator::Finalize(allocator);
7295}
7296
Mark Mendell0616ae02015-04-17 12:49:27 -04007297Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7298 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7299 return Address(reg, kDummy32BitOffset, fixup);
7300}
7301
7302Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7303 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7304 return Address(reg, kDummy32BitOffset, fixup);
7305}
7306
7307Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7308 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7309 return Address(reg, kDummy32BitOffset, fixup);
7310}
7311
7312Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7313 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7314 return Address(reg, kDummy32BitOffset, fixup);
7315}
7316
Aart Bika19616e2016-02-01 18:57:58 -08007317void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7318 if (value == 0) {
7319 __ xorl(dest, dest);
7320 } else {
7321 __ movl(dest, Immediate(value));
7322 }
7323}
7324
7325void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7326 if (value == 0) {
7327 __ testl(dest, dest);
7328 } else {
7329 __ cmpl(dest, Immediate(value));
7330 }
7331}
7332
Mark Mendell805b3b52015-09-18 14:10:29 -04007333Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7334 Register reg,
7335 Register value) {
7336 // Create a fixup to be used to create and address the jump table.
7337 JumpTableRIPFixup* table_fixup =
7338 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7339
7340 // We have to populate the jump tables.
7341 fixups_to_jump_tables_.push_back(table_fixup);
7342
7343 // We want a scaled address, as we are extracting the correct offset from the table.
7344 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7345}
7346
Andreas Gampe85b62f22015-09-09 13:15:38 -07007347// TODO: target as memory.
7348void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7349 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007350 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007351 return;
7352 }
7353
7354 DCHECK_NE(type, Primitive::kPrimVoid);
7355
7356 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7357 if (target.Equals(return_loc)) {
7358 return;
7359 }
7360
7361 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7362 // with the else branch.
7363 if (type == Primitive::kPrimLong) {
7364 HParallelMove parallel_move(GetGraph()->GetArena());
7365 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7366 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7367 GetMoveResolver()->EmitNativeCode(&parallel_move);
7368 } else {
7369 // Let the parallel move resolver take care of all of this.
7370 HParallelMove parallel_move(GetGraph()->GetArena());
7371 parallel_move.AddMove(return_loc, target, type, nullptr);
7372 GetMoveResolver()->EmitNativeCode(&parallel_move);
7373 }
7374}
7375
Roland Levillain4d027112015-07-01 15:41:14 +01007376#undef __
7377
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007378} // namespace x86
7379} // namespace art