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