blob: ee5a674e65b1d2b0c3eb597256e2304b8200c792 [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
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037namespace x86 {
38
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010039static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010040static constexpr Register kMethodRegisterArgument = EAX;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010041
Mark Mendell5f874182015-03-04 15:42:45 -050042static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010043
Mark Mendell24f2dfa2015-01-14 19:51:45 -050044static constexpr int kC2ConditionMask = 0x400;
45
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000046static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000047
Roland Levillain62a46b22015-06-01 18:24:13 +010048#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Alexandre Rames8158f282015-08-07 10:26:17 +010049#define QUICK_ENTRY_POINT(x) Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x))
Nicolas Geoffraye5038322014-07-04 09:41:32 +010050
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010051class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010053 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054
Alexandre Rames2ed20af2015-03-06 13:55:35 +000055 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010056 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +010058 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
59 instruction_,
60 instruction_->GetDexPc(),
61 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062 }
63
Alexandre Rames8158f282015-08-07 10:26:17 +010064 bool IsFatal() const OVERRIDE { return true; }
65
Alexandre Rames9931f312015-06-19 14:47:01 +010066 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
67
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010069 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
71};
72
Calin Juravled0d48522014-11-04 16:40:20 +000073class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
74 public:
75 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
76
Alexandre Rames2ed20af2015-03-06 13:55:35 +000077 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010078 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000079 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +010080 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
81 instruction_,
82 instruction_->GetDexPc(),
83 this);
Calin Juravled0d48522014-11-04 16:40:20 +000084 }
85
Alexandre Rames8158f282015-08-07 10:26:17 +010086 bool IsFatal() const OVERRIDE { return true; }
87
Alexandre Rames9931f312015-06-19 14:47:01 +010088 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
89
Calin Juravled0d48522014-11-04 16:40:20 +000090 private:
91 HDivZeroCheck* const instruction_;
92 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
93};
94
Calin Juravlebacfec32014-11-14 15:54:36 +000095class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000096 public:
Roland Levillain3887c462015-08-12 18:15:42 +010097 DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000098
Alexandre Rames2ed20af2015-03-06 13:55:35 +000099 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000100 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000101 if (is_div_) {
102 __ negl(reg_);
103 } else {
104 __ movl(reg_, Immediate(0));
105 }
Calin Juravled0d48522014-11-04 16:40:20 +0000106 __ jmp(GetExitLabel());
107 }
108
Alexandre Rames9931f312015-06-19 14:47:01 +0100109 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
110
Calin Juravled0d48522014-11-04 16:40:20 +0000111 private:
112 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 bool is_div_;
114 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000115};
116
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100117class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100118 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100119 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100120
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000121 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100122 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100123 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100124 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000125 // We're moving two locations to locations that could overlap, so we need a parallel
126 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000128 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100129 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000130 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100131 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100132 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100133 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
134 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100135 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
136 instruction_,
137 instruction_->GetDexPc(),
138 this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100139 }
140
Alexandre Rames8158f282015-08-07 10:26:17 +0100141 bool IsFatal() const OVERRIDE { return true; }
142
Alexandre Rames9931f312015-06-19 14:47:01 +0100143 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
144
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100145 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100146 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147
148 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
149};
150
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100151class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000152 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000153 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100154 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100157 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000159 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100160 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
161 instruction_,
162 instruction_->GetDexPc(),
163 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000164 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100165 if (successor_ == nullptr) {
166 __ jmp(GetReturnLabel());
167 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100168 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100169 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170 }
171
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100172 Label* GetReturnLabel() {
173 DCHECK(successor_ == nullptr);
174 return &return_label_;
175 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000176
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100177 HBasicBlock* GetSuccessor() const {
178 return successor_;
179 }
180
Alexandre Rames9931f312015-06-19 14:47:01 +0100181 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
182
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000183 private:
184 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000186 Label return_label_;
187
188 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
189};
190
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000191class LoadStringSlowPathX86 : public SlowPathCodeX86 {
192 public:
193 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
194
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000195 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000196 LocationSummary* locations = instruction_->GetLocations();
197 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
198
199 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
200 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000201 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000202
203 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800204 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100205 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
206 instruction_,
207 instruction_->GetDexPc(),
208 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000209 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000210 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000211
212 __ jmp(GetExitLabel());
213 }
214
Alexandre Rames9931f312015-06-19 14:47:01 +0100215 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
216
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000217 private:
218 HLoadString* const instruction_;
219
220 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
221};
222
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000223class LoadClassSlowPathX86 : public SlowPathCodeX86 {
224 public:
225 LoadClassSlowPathX86(HLoadClass* cls,
226 HInstruction* at,
227 uint32_t dex_pc,
228 bool do_clinit)
229 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
230 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
231 }
232
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000233 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000234 LocationSummary* locations = at_->GetLocations();
235 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
236 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000237 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000238
239 InvokeRuntimeCallingConvention calling_convention;
240 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100241 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
242 : QUICK_ENTRY_POINT(pInitializeType),
243 at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244
245 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000246 Location out = locations->Out();
247 if (out.IsValid()) {
248 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
249 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000251
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000252 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253 __ jmp(GetExitLabel());
254 }
255
Alexandre Rames9931f312015-06-19 14:47:01 +0100256 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
257
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 private:
259 // The class this slow path will load.
260 HLoadClass* const cls_;
261
262 // The instruction where this slow path is happening.
263 // (Might be the load class or an initialization check).
264 HInstruction* const at_;
265
266 // The dex PC of `at_`.
267 const uint32_t dex_pc_;
268
269 // Whether to initialize the class.
270 const bool do_clinit_;
271
272 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
273};
274
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000275class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
276 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100277 explicit TypeCheckSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000279 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000280 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100281 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
282 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000283 DCHECK(instruction_->IsCheckCast()
284 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285
286 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
287 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000288 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289
290 // We're moving two locations to locations that could overlap, so we need a parallel
291 // move resolver.
292 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000293 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100294 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000295 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100296 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100297 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100298 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
299 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000301 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100302 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
303 instruction_,
304 instruction_->GetDexPc(),
305 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000306 } else {
307 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100308 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
309 instruction_,
310 instruction_->GetDexPc(),
311 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000312 }
313
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000314 if (instruction_->IsInstanceOf()) {
315 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
316 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000317 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318
319 __ jmp(GetExitLabel());
320 }
321
Alexandre Rames9931f312015-06-19 14:47:01 +0100322 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
323
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 HInstruction* const instruction_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
327 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
328};
329
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700330class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
331 public:
332 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
333 : instruction_(instruction) {}
334
335 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames98596202015-08-19 11:33:36 +0100336 DCHECK(instruction_->IsDeoptimize());
Alexandre Rames8158f282015-08-07 10:26:17 +0100337 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700338 __ Bind(GetEntryLabel());
339 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100340 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
341 instruction_,
342 instruction_->GetDexPc(),
343 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700344 }
345
Alexandre Rames9931f312015-06-19 14:47:01 +0100346 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
347
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700348 private:
349 HInstruction* const instruction_;
350 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
351};
352
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100353#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100354#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100355
Roland Levillain4fa13f62015-07-06 18:11:54 +0100356inline Condition X86SignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700357 switch (cond) {
358 case kCondEQ: return kEqual;
359 case kCondNE: return kNotEqual;
360 case kCondLT: return kLess;
361 case kCondLE: return kLessEqual;
362 case kCondGT: return kGreater;
363 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700364 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100365 LOG(FATAL) << "Unreachable";
366 UNREACHABLE();
367}
368
369inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
370 switch (cond) {
371 case kCondEQ: return kEqual;
372 case kCondNE: return kNotEqual;
373 case kCondLT: return kBelow;
374 case kCondLE: return kBelowEqual;
375 case kCondGT: return kAbove;
376 case kCondGE: return kAboveEqual;
377 }
378 LOG(FATAL) << "Unreachable";
379 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700380}
381
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100382void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100383 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100384}
385
386void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100387 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100388}
389
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100390size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
391 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
392 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100393}
394
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100395size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
396 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
397 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100398}
399
Mark Mendell7c8d0092015-01-26 11:21:33 -0500400size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
401 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
402 return GetFloatingPointSpillSlotSize();
403}
404
405size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
406 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
407 return GetFloatingPointSpillSlotSize();
408}
409
Alexandre Rames8158f282015-08-07 10:26:17 +0100410void CodeGeneratorX86::InvokeRuntime(Address entry_point,
411 HInstruction* instruction,
412 uint32_t dex_pc,
413 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100414 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100415 __ fs()->call(entry_point);
416 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100417}
418
Mark Mendellfb8d2792015-03-31 22:16:59 -0400419CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
420 const X86InstructionSetFeatures& isa_features,
421 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500422 : CodeGenerator(graph,
423 kNumberOfCpuRegisters,
424 kNumberOfXmmRegisters,
425 kNumberOfRegisterPairs,
426 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
427 arraysize(kCoreCalleeSaves))
428 | (1 << kFakeReturnRegister),
429 0,
430 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100431 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100432 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100433 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400434 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000435 isa_features_(isa_features),
436 method_patches_(graph->GetArena()->Adapter()),
437 relative_call_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000438 // Use a fake return address register to mimic Quick.
439 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100440}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100441
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100442Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100443 switch (type) {
444 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100445 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100446 X86ManagedRegister pair =
447 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100448 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
449 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100450 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
451 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100452 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100453 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100454 }
455
456 case Primitive::kPrimByte:
457 case Primitive::kPrimBoolean:
458 case Primitive::kPrimChar:
459 case Primitive::kPrimShort:
460 case Primitive::kPrimInt:
461 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100462 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100463 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100464 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100465 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
466 X86ManagedRegister current =
467 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
468 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100469 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100470 }
471 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100472 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100473 }
474
475 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100476 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100477 return Location::FpuRegisterLocation(
478 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100479 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100480
481 case Primitive::kPrimVoid:
482 LOG(FATAL) << "Unreachable type " << type;
483 }
484
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100485 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100486}
487
Mark Mendell5f874182015-03-04 15:42:45 -0500488void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100489 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100490 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100491
492 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100493 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100494
Mark Mendell5f874182015-03-04 15:42:45 -0500495 if (is_baseline) {
496 blocked_core_registers_[EBP] = true;
497 blocked_core_registers_[ESI] = true;
498 blocked_core_registers_[EDI] = true;
499 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100500
501 UpdateBlockedPairRegisters();
502}
503
504void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
505 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
506 X86ManagedRegister current =
507 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
508 if (blocked_core_registers_[current.AsRegisterPairLow()]
509 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
510 blocked_register_pairs_[i] = true;
511 }
512 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100513}
514
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100515InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
516 : HGraphVisitor(graph),
517 assembler_(codegen->GetAssembler()),
518 codegen_(codegen) {}
519
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100520static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100521 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100522}
523
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000524void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100525 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000526 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000527 bool skip_overflow_check =
528 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000529 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000530
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000531 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100532 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100533 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100534 }
535
Mark Mendell5f874182015-03-04 15:42:45 -0500536 if (HasEmptyFrame()) {
537 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000538 }
Mark Mendell5f874182015-03-04 15:42:45 -0500539
540 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
541 Register reg = kCoreCalleeSaves[i];
542 if (allocated_registers_.ContainsCoreRegister(reg)) {
543 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100544 __ cfi().AdjustCFAOffset(kX86WordSize);
545 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500546 }
547 }
548
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100549 int adjust = GetFrameSize() - FrameEntrySpillSize();
550 __ subl(ESP, Immediate(adjust));
551 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100552 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000553}
554
555void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100556 __ cfi().RememberState();
557 if (!HasEmptyFrame()) {
558 int adjust = GetFrameSize() - FrameEntrySpillSize();
559 __ addl(ESP, Immediate(adjust));
560 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500561
David Srbeckyc34dc932015-04-12 09:27:43 +0100562 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
563 Register reg = kCoreCalleeSaves[i];
564 if (allocated_registers_.ContainsCoreRegister(reg)) {
565 __ popl(reg);
566 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
567 __ cfi().Restore(DWARFReg(reg));
568 }
Mark Mendell5f874182015-03-04 15:42:45 -0500569 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000570 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100571 __ ret();
572 __ cfi().RestoreState();
573 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000574}
575
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100576void CodeGeneratorX86::Bind(HBasicBlock* block) {
577 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000578}
579
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100580Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
581 switch (load->GetType()) {
582 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100583 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100584 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100585
586 case Primitive::kPrimInt:
587 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100588 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100589 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100590
591 case Primitive::kPrimBoolean:
592 case Primitive::kPrimByte:
593 case Primitive::kPrimChar:
594 case Primitive::kPrimShort:
595 case Primitive::kPrimVoid:
596 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700597 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100598 }
599
600 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700601 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100602}
603
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100604Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
605 switch (type) {
606 case Primitive::kPrimBoolean:
607 case Primitive::kPrimByte:
608 case Primitive::kPrimChar:
609 case Primitive::kPrimShort:
610 case Primitive::kPrimInt:
611 case Primitive::kPrimNot:
612 return Location::RegisterLocation(EAX);
613
614 case Primitive::kPrimLong:
615 return Location::RegisterPairLocation(EAX, EDX);
616
617 case Primitive::kPrimVoid:
618 return Location::NoLocation();
619
620 case Primitive::kPrimDouble:
621 case Primitive::kPrimFloat:
622 return Location::FpuRegisterLocation(XMM0);
623 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100624
625 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100626}
627
628Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
629 return Location::RegisterLocation(kMethodRegisterArgument);
630}
631
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100632Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100633 switch (type) {
634 case Primitive::kPrimBoolean:
635 case Primitive::kPrimByte:
636 case Primitive::kPrimChar:
637 case Primitive::kPrimShort:
638 case Primitive::kPrimInt:
639 case Primitive::kPrimNot: {
640 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000641 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100642 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100643 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100644 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000645 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100646 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100647 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100648
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000649 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100650 uint32_t index = gp_index_;
651 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000652 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100653 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100654 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
655 calling_convention.GetRegisterPairAt(index));
656 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100657 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000658 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
659 }
660 }
661
662 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100663 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000664 stack_index_++;
665 if (index < calling_convention.GetNumberOfFpuRegisters()) {
666 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
667 } else {
668 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
669 }
670 }
671
672 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100673 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000674 stack_index_ += 2;
675 if (index < calling_convention.GetNumberOfFpuRegisters()) {
676 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
677 } else {
678 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100679 }
680 }
681
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100682 case Primitive::kPrimVoid:
683 LOG(FATAL) << "Unexpected parameter type " << type;
684 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100685 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100686 return Location();
687}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100688
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100689void CodeGeneratorX86::Move32(Location destination, Location source) {
690 if (source.Equals(destination)) {
691 return;
692 }
693 if (destination.IsRegister()) {
694 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000695 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100696 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000697 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100698 } else {
699 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000700 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100701 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100702 } else if (destination.IsFpuRegister()) {
703 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000704 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100705 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000706 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100707 } else {
708 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000709 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100710 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100711 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000712 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100713 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000714 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100715 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000716 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500717 } else if (source.IsConstant()) {
718 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000719 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500720 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100721 } else {
722 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100723 __ pushl(Address(ESP, source.GetStackIndex()));
724 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100725 }
726 }
727}
728
729void CodeGeneratorX86::Move64(Location destination, Location source) {
730 if (source.Equals(destination)) {
731 return;
732 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100733 if (destination.IsRegisterPair()) {
734 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000735 EmitParallelMoves(
736 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
737 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100738 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000739 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100740 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
741 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100742 } else if (source.IsFpuRegister()) {
743 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100744 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000745 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100746 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100747 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
748 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100749 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
750 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100751 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500752 if (source.IsFpuRegister()) {
753 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
754 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000755 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100756 } else {
757 LOG(FATAL) << "Unimplemented";
758 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100759 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000760 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100761 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000762 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100763 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100764 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100765 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100766 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000767 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000768 } else if (source.IsConstant()) {
769 HConstant* constant = source.GetConstant();
770 int64_t value;
771 if (constant->IsLongConstant()) {
772 value = constant->AsLongConstant()->GetValue();
773 } else {
774 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000775 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000776 }
777 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
778 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100779 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000780 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000781 EmitParallelMoves(
782 Location::StackSlot(source.GetStackIndex()),
783 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100784 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000785 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100786 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
787 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100788 }
789 }
790}
791
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100792void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000793 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100794 if (instruction->IsCurrentMethod()) {
795 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
796 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000797 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100798 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000799 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000800 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
801 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000802 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000803 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000804 } else if (location.IsStackSlot()) {
805 __ movl(Address(ESP, location.GetStackIndex()), imm);
806 } else {
807 DCHECK(location.IsConstant());
808 DCHECK_EQ(location.GetConstant(), const_to_move);
809 }
810 } else if (const_to_move->IsLongConstant()) {
811 int64_t value = const_to_move->AsLongConstant()->GetValue();
812 if (location.IsRegisterPair()) {
813 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
814 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
815 } else if (location.IsDoubleStackSlot()) {
816 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000817 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
818 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000819 } else {
820 DCHECK(location.IsConstant());
821 DCHECK_EQ(location.GetConstant(), instruction);
822 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100823 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000824 } else if (instruction->IsTemporary()) {
825 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000826 if (temp_location.IsStackSlot()) {
827 Move32(location, temp_location);
828 } else {
829 DCHECK(temp_location.IsDoubleStackSlot());
830 Move64(location, temp_location);
831 }
Roland Levillain476df552014-10-09 17:51:36 +0100832 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100833 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100834 switch (instruction->GetType()) {
835 case Primitive::kPrimBoolean:
836 case Primitive::kPrimByte:
837 case Primitive::kPrimChar:
838 case Primitive::kPrimShort:
839 case Primitive::kPrimInt:
840 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100841 case Primitive::kPrimFloat:
842 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100843 break;
844
845 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100846 case Primitive::kPrimDouble:
847 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100848 break;
849
850 default:
851 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
852 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000853 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100854 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100855 switch (instruction->GetType()) {
856 case Primitive::kPrimBoolean:
857 case Primitive::kPrimByte:
858 case Primitive::kPrimChar:
859 case Primitive::kPrimShort:
860 case Primitive::kPrimInt:
861 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100862 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000863 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100864 break;
865
866 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100867 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000868 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100869 break;
870
871 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100872 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100873 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000874 }
875}
876
David Brazdilfc6a86a2015-06-26 10:33:45 +0000877void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100878 DCHECK(!successor->IsExitBlock());
879
880 HBasicBlock* block = got->GetBlock();
881 HInstruction* previous = got->GetPrevious();
882
883 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000884 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100885 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
886 return;
887 }
888
889 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
890 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
891 }
892 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000893 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000894 }
895}
896
David Brazdilfc6a86a2015-06-26 10:33:45 +0000897void LocationsBuilderX86::VisitGoto(HGoto* got) {
898 got->SetLocations(nullptr);
899}
900
901void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
902 HandleGoto(got, got->GetSuccessor());
903}
904
905void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
906 try_boundary->SetLocations(nullptr);
907}
908
909void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
910 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
911 if (!successor->IsExitBlock()) {
912 HandleGoto(try_boundary, successor);
913 }
914}
915
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000916void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000917 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000918}
919
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000920void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700921 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000922}
923
Mark Mendellc4701932015-04-10 13:18:51 -0400924void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
925 Label* true_label,
926 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100927 if (cond->IsFPConditionTrueIfNaN()) {
928 __ j(kUnordered, true_label);
929 } else if (cond->IsFPConditionFalseIfNaN()) {
930 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400931 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100932 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400933}
934
935void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
936 Label* true_label,
937 Label* false_label) {
938 LocationSummary* locations = cond->GetLocations();
939 Location left = locations->InAt(0);
940 Location right = locations->InAt(1);
941 IfCondition if_cond = cond->GetCondition();
942
Mark Mendellc4701932015-04-10 13:18:51 -0400943 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100944 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -0400945 IfCondition true_high_cond = if_cond;
946 IfCondition false_high_cond = cond->GetOppositeCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100947 Condition final_condition = X86UnsignedOrFPCondition(if_cond);
Mark Mendellc4701932015-04-10 13:18:51 -0400948
949 // Set the conditions for the test, remembering that == needs to be
950 // decided using the low words.
951 switch (if_cond) {
952 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -0400953 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +0100954 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -0400955 break;
956 case kCondLT:
957 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400958 break;
959 case kCondLE:
960 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400961 break;
962 case kCondGT:
963 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400964 break;
965 case kCondGE:
966 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400967 break;
968 }
969
970 if (right.IsConstant()) {
971 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -0400972 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +0100973 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -0400974
975 if (val_high == 0) {
976 __ testl(left_high, left_high);
977 } else {
978 __ cmpl(left_high, Immediate(val_high));
979 }
980 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100981 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400982 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100983 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400984 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100985 __ j(X86SignedCondition(true_high_cond), true_label);
986 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400987 }
988 // Must be equal high, so compare the lows.
989 if (val_low == 0) {
990 __ testl(left_low, left_low);
991 } else {
992 __ cmpl(left_low, Immediate(val_low));
993 }
994 } else {
Mark Mendellc4701932015-04-10 13:18:51 -0400995 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100996 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -0400997
998 __ cmpl(left_high, right_high);
999 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001000 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001001 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001002 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001003 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001004 __ j(X86SignedCondition(true_high_cond), true_label);
1005 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001006 }
1007 // Must be equal high, so compare the lows.
1008 __ cmpl(left_low, right_low);
1009 }
1010 // The last comparison might be unsigned.
1011 __ j(final_condition, true_label);
1012}
1013
1014void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1015 HCondition* condition,
1016 Label* true_target,
1017 Label* false_target,
1018 Label* always_true_target) {
1019 LocationSummary* locations = condition->GetLocations();
1020 Location left = locations->InAt(0);
1021 Location right = locations->InAt(1);
1022
1023 // We don't want true_target as a nullptr.
1024 if (true_target == nullptr) {
1025 true_target = always_true_target;
1026 }
1027 bool falls_through = (false_target == nullptr);
1028
1029 // FP compares don't like null false_targets.
1030 if (false_target == nullptr) {
1031 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1032 }
1033
1034 Primitive::Type type = condition->InputAt(0)->GetType();
1035 switch (type) {
1036 case Primitive::kPrimLong:
1037 GenerateLongComparesAndJumps(condition, true_target, false_target);
1038 break;
1039 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001040 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1041 GenerateFPJumps(condition, true_target, false_target);
1042 break;
1043 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001044 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1045 GenerateFPJumps(condition, true_target, false_target);
1046 break;
1047 default:
1048 LOG(FATAL) << "Unexpected compare type " << type;
1049 }
1050
1051 if (!falls_through) {
1052 __ jmp(false_target);
1053 }
1054}
1055
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001056void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1057 Label* true_target,
1058 Label* false_target,
1059 Label* always_true_target) {
1060 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001061 if (cond->IsIntConstant()) {
1062 // Constant condition, statically compared against 1.
1063 int32_t cond_value = cond->AsIntConstant()->GetValue();
1064 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001065 if (always_true_target != nullptr) {
1066 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001067 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001068 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001069 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001070 DCHECK_EQ(cond_value, 0);
1071 }
1072 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001073 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001074 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1075 // Moves do not affect the eflags register, so if the condition is
1076 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001077 // again. We can't use the eflags on long/FP conditions if they are
1078 // materialized due to the complex branching.
1079 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001080 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001081 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001082 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1083 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001084 if (!eflags_set) {
1085 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001086 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001087 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001088 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001089 } else {
1090 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1091 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001092 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001093 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001094 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001095 }
1096 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001097 // Condition has not been materialized, use its inputs as the
1098 // comparison and its condition as the branch condition.
1099
Mark Mendellc4701932015-04-10 13:18:51 -04001100 // Is this a long or FP comparison that has been folded into the HCondition?
1101 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1102 // Generate the comparison directly.
1103 GenerateCompareTestAndBranch(instruction->AsIf(),
1104 cond->AsCondition(),
1105 true_target,
1106 false_target,
1107 always_true_target);
1108 return;
1109 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001110
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001111 Location lhs = cond->GetLocations()->InAt(0);
1112 Location rhs = cond->GetLocations()->InAt(1);
1113 // LHS is guaranteed to be in a register (see
1114 // LocationsBuilderX86::VisitCondition).
1115 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001116 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001117 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001118 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001119 if (constant == 0) {
1120 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1121 } else {
1122 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1123 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001124 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001125 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001126 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001127 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001128 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001129 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001130 if (false_target != nullptr) {
1131 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001132 }
1133}
1134
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001135void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1136 LocationSummary* locations =
1137 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1138 HInstruction* cond = if_instr->InputAt(0);
1139 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1140 locations->SetInAt(0, Location::Any());
1141 }
1142}
1143
1144void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1145 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1146 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1147 Label* always_true_target = true_target;
1148 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1149 if_instr->IfTrueSuccessor())) {
1150 always_true_target = nullptr;
1151 }
1152 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1153 if_instr->IfFalseSuccessor())) {
1154 false_target = nullptr;
1155 }
1156 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1157}
1158
1159void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1160 LocationSummary* locations = new (GetGraph()->GetArena())
1161 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1162 HInstruction* cond = deoptimize->InputAt(0);
1163 DCHECK(cond->IsCondition());
1164 if (cond->AsCondition()->NeedsMaterialization()) {
1165 locations->SetInAt(0, Location::Any());
1166 }
1167}
1168
1169void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1170 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
1171 DeoptimizationSlowPathX86(deoptimize);
1172 codegen_->AddSlowPath(slow_path);
1173 Label* slow_path_entry = slow_path->GetEntryLabel();
1174 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1175}
1176
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001177void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001178 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001179}
1180
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001181void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1182 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001183}
1184
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001185void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001186 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001187}
1188
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001189void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001190 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001191 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001192}
1193
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001194void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001195 LocationSummary* locations =
1196 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001197 switch (store->InputAt(1)->GetType()) {
1198 case Primitive::kPrimBoolean:
1199 case Primitive::kPrimByte:
1200 case Primitive::kPrimChar:
1201 case Primitive::kPrimShort:
1202 case Primitive::kPrimInt:
1203 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001204 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001205 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1206 break;
1207
1208 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001209 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001210 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1211 break;
1212
1213 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001214 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001215 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001216}
1217
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001218void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001219 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001220}
1221
Roland Levillain0d37cd02015-05-27 16:39:19 +01001222void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001223 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001224 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001225 // Handle the long/FP comparisons made in instruction simplification.
1226 switch (cond->InputAt(0)->GetType()) {
1227 case Primitive::kPrimLong: {
1228 locations->SetInAt(0, Location::RequiresRegister());
1229 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1230 if (cond->NeedsMaterialization()) {
1231 locations->SetOut(Location::RequiresRegister());
1232 }
1233 break;
1234 }
1235 case Primitive::kPrimFloat:
1236 case Primitive::kPrimDouble: {
1237 locations->SetInAt(0, Location::RequiresFpuRegister());
1238 locations->SetInAt(1, Location::RequiresFpuRegister());
1239 if (cond->NeedsMaterialization()) {
1240 locations->SetOut(Location::RequiresRegister());
1241 }
1242 break;
1243 }
1244 default:
1245 locations->SetInAt(0, Location::RequiresRegister());
1246 locations->SetInAt(1, Location::Any());
1247 if (cond->NeedsMaterialization()) {
1248 // We need a byte register.
1249 locations->SetOut(Location::RegisterLocation(ECX));
1250 }
1251 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001252 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001253}
1254
Roland Levillain0d37cd02015-05-27 16:39:19 +01001255void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001256 if (!cond->NeedsMaterialization()) {
1257 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001258 }
Mark Mendellc4701932015-04-10 13:18:51 -04001259
1260 LocationSummary* locations = cond->GetLocations();
1261 Location lhs = locations->InAt(0);
1262 Location rhs = locations->InAt(1);
1263 Register reg = locations->Out().AsRegister<Register>();
1264 Label true_label, false_label;
1265
1266 switch (cond->InputAt(0)->GetType()) {
1267 default: {
1268 // Integer case.
1269
1270 // Clear output register: setcc only sets the low byte.
1271 __ xorl(reg, reg);
1272
1273 if (rhs.IsRegister()) {
1274 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1275 } else if (rhs.IsConstant()) {
1276 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1277 if (constant == 0) {
1278 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1279 } else {
1280 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1281 }
1282 } else {
1283 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1284 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001285 __ setb(X86SignedCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001286 return;
1287 }
1288 case Primitive::kPrimLong:
1289 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1290 break;
1291 case Primitive::kPrimFloat:
1292 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1293 GenerateFPJumps(cond, &true_label, &false_label);
1294 break;
1295 case Primitive::kPrimDouble:
1296 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1297 GenerateFPJumps(cond, &true_label, &false_label);
1298 break;
1299 }
1300
1301 // Convert the jumps into the result.
1302 Label done_label;
1303
Roland Levillain4fa13f62015-07-06 18:11:54 +01001304 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001305 __ Bind(&false_label);
1306 __ xorl(reg, reg);
1307 __ jmp(&done_label);
1308
Roland Levillain4fa13f62015-07-06 18:11:54 +01001309 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001310 __ Bind(&true_label);
1311 __ movl(reg, Immediate(1));
1312 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001313}
1314
1315void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1316 VisitCondition(comp);
1317}
1318
1319void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1320 VisitCondition(comp);
1321}
1322
1323void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1324 VisitCondition(comp);
1325}
1326
1327void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1328 VisitCondition(comp);
1329}
1330
1331void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1332 VisitCondition(comp);
1333}
1334
1335void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1336 VisitCondition(comp);
1337}
1338
1339void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1340 VisitCondition(comp);
1341}
1342
1343void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1344 VisitCondition(comp);
1345}
1346
1347void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1348 VisitCondition(comp);
1349}
1350
1351void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1352 VisitCondition(comp);
1353}
1354
1355void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1356 VisitCondition(comp);
1357}
1358
1359void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1360 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001361}
1362
1363void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001364 LocationSummary* locations =
1365 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001366 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001367}
1368
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001369void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001370 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001371 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001372}
1373
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001374void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1375 LocationSummary* locations =
1376 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1377 locations->SetOut(Location::ConstantLocation(constant));
1378}
1379
1380void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1381 // Will be generated at use site.
1382 UNUSED(constant);
1383}
1384
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001385void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001386 LocationSummary* locations =
1387 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001388 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001389}
1390
1391void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1392 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001393 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001394}
1395
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001396void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1397 LocationSummary* locations =
1398 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1399 locations->SetOut(Location::ConstantLocation(constant));
1400}
1401
1402void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1403 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001404 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001405}
1406
1407void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1408 LocationSummary* locations =
1409 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1410 locations->SetOut(Location::ConstantLocation(constant));
1411}
1412
1413void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1414 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001415 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001416}
1417
Calin Juravle27df7582015-04-17 19:12:31 +01001418void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1419 memory_barrier->SetLocations(nullptr);
1420}
1421
1422void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1423 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1424}
1425
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001426void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001427 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001428}
1429
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001430void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001431 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001432 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001433}
1434
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001435void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001436 LocationSummary* locations =
1437 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001438 switch (ret->InputAt(0)->GetType()) {
1439 case Primitive::kPrimBoolean:
1440 case Primitive::kPrimByte:
1441 case Primitive::kPrimChar:
1442 case Primitive::kPrimShort:
1443 case Primitive::kPrimInt:
1444 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001445 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001446 break;
1447
1448 case Primitive::kPrimLong:
1449 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001450 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001451 break;
1452
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001453 case Primitive::kPrimFloat:
1454 case Primitive::kPrimDouble:
1455 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001456 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001457 break;
1458
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001459 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001460 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001461 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001462}
1463
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001464void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001465 if (kIsDebugBuild) {
1466 switch (ret->InputAt(0)->GetType()) {
1467 case Primitive::kPrimBoolean:
1468 case Primitive::kPrimByte:
1469 case Primitive::kPrimChar:
1470 case Primitive::kPrimShort:
1471 case Primitive::kPrimInt:
1472 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001473 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001474 break;
1475
1476 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001477 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1478 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001479 break;
1480
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001481 case Primitive::kPrimFloat:
1482 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001483 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001484 break;
1485
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001486 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001487 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001488 }
1489 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001490 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001491}
1492
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001493void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001494 // When we do not run baseline, explicit clinit checks triggered by static
1495 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1496 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001497
Mark Mendellfb8d2792015-03-31 22:16:59 -04001498 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001499 if (intrinsic.TryDispatch(invoke)) {
1500 return;
1501 }
1502
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001503 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001504
1505 if (codegen_->IsBaseline()) {
1506 // Baseline does not have enough registers if the current method also
1507 // needs a register. We therefore do not require a register for it, and let
1508 // the code generation of the invoke handle it.
1509 LocationSummary* locations = invoke->GetLocations();
1510 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1511 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1512 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1513 }
1514 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001515}
1516
Mark Mendell09ed1a32015-03-25 08:30:06 -04001517static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1518 if (invoke->GetLocations()->Intrinsified()) {
1519 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1520 intrinsic.Dispatch(invoke);
1521 return true;
1522 }
1523 return false;
1524}
1525
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001526void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001527 // When we do not run baseline, explicit clinit checks triggered by static
1528 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1529 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001530
Mark Mendell09ed1a32015-03-25 08:30:06 -04001531 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1532 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001533 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001534
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001535 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001536 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001537 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001538 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001539}
1540
1541void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1542 HandleInvoke(invoke);
1543}
1544
1545void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001546 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001547 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001548}
1549
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001550void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001551 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1552 return;
1553 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001554
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001555 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001556 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001557 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001558}
1559
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001560void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1561 HandleInvoke(invoke);
1562 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001563 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001564}
1565
1566void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1567 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001568 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001569 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1570 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001571 LocationSummary* locations = invoke->GetLocations();
1572 Location receiver = locations->InAt(0);
1573 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1574
1575 // Set the hidden argument.
1576 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001577 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001578
1579 // temp = object->GetClass();
1580 if (receiver.IsStackSlot()) {
1581 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1582 __ movl(temp, Address(temp, class_offset));
1583 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001584 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001585 }
Roland Levillain4d027112015-07-01 15:41:14 +01001586 codegen_->MaybeRecordImplicitNullCheck(invoke);
1587 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001588 // temp = temp->GetImtEntryAt(method_offset);
1589 __ movl(temp, Address(temp, method_offset));
1590 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001591 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001592 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001593
1594 DCHECK(!codegen_->IsLeafMethod());
1595 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1596}
1597
Roland Levillain88cb1752014-10-20 16:36:47 +01001598void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1599 LocationSummary* locations =
1600 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1601 switch (neg->GetResultType()) {
1602 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001603 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001604 locations->SetInAt(0, Location::RequiresRegister());
1605 locations->SetOut(Location::SameAsFirstInput());
1606 break;
1607
Roland Levillain88cb1752014-10-20 16:36:47 +01001608 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001609 locations->SetInAt(0, Location::RequiresFpuRegister());
1610 locations->SetOut(Location::SameAsFirstInput());
1611 locations->AddTemp(Location::RequiresRegister());
1612 locations->AddTemp(Location::RequiresFpuRegister());
1613 break;
1614
Roland Levillain88cb1752014-10-20 16:36:47 +01001615 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001616 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001617 locations->SetOut(Location::SameAsFirstInput());
1618 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001619 break;
1620
1621 default:
1622 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1623 }
1624}
1625
1626void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1627 LocationSummary* locations = neg->GetLocations();
1628 Location out = locations->Out();
1629 Location in = locations->InAt(0);
1630 switch (neg->GetResultType()) {
1631 case Primitive::kPrimInt:
1632 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001633 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001634 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001635 break;
1636
1637 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001638 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001639 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001640 __ negl(out.AsRegisterPairLow<Register>());
1641 // Negation is similar to subtraction from zero. The least
1642 // significant byte triggers a borrow when it is different from
1643 // zero; to take it into account, add 1 to the most significant
1644 // byte if the carry flag (CF) is set to 1 after the first NEGL
1645 // operation.
1646 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1647 __ negl(out.AsRegisterPairHigh<Register>());
1648 break;
1649
Roland Levillain5368c212014-11-27 15:03:41 +00001650 case Primitive::kPrimFloat: {
1651 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001652 Register constant = locations->GetTemp(0).AsRegister<Register>();
1653 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001654 // Implement float negation with an exclusive or with value
1655 // 0x80000000 (mask for bit 31, representing the sign of a
1656 // single-precision floating-point number).
1657 __ movl(constant, Immediate(INT32_C(0x80000000)));
1658 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001659 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001660 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001661 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001662
Roland Levillain5368c212014-11-27 15:03:41 +00001663 case Primitive::kPrimDouble: {
1664 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001665 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001666 // Implement double negation with an exclusive or with value
1667 // 0x8000000000000000 (mask for bit 63, representing the sign of
1668 // a double-precision floating-point number).
1669 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001670 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001671 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001672 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001673
1674 default:
1675 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1676 }
1677}
1678
Roland Levillaindff1f282014-11-05 14:15:05 +00001679void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001680 Primitive::Type result_type = conversion->GetResultType();
1681 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001682 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001683
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001684 // The float-to-long and double-to-long type conversions rely on a
1685 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001686 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001687 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1688 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001689 ? LocationSummary::kCall
1690 : LocationSummary::kNoCall;
1691 LocationSummary* locations =
1692 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1693
David Brazdilb2bd1c52015-03-25 11:17:37 +00001694 // The Java language does not allow treating boolean as an integral type but
1695 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001696
Roland Levillaindff1f282014-11-05 14:15:05 +00001697 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001698 case Primitive::kPrimByte:
1699 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001700 case Primitive::kPrimBoolean:
1701 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001702 case Primitive::kPrimShort:
1703 case Primitive::kPrimInt:
1704 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001705 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001706 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1707 // Make the output overlap to please the register allocator. This greatly simplifies
1708 // the validation of the linear scan implementation
1709 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001710 break;
1711
1712 default:
1713 LOG(FATAL) << "Unexpected type conversion from " << input_type
1714 << " to " << result_type;
1715 }
1716 break;
1717
Roland Levillain01a8d712014-11-14 16:27:39 +00001718 case Primitive::kPrimShort:
1719 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001720 case Primitive::kPrimBoolean:
1721 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001722 case Primitive::kPrimByte:
1723 case Primitive::kPrimInt:
1724 case Primitive::kPrimChar:
1725 // Processing a Dex `int-to-short' instruction.
1726 locations->SetInAt(0, Location::Any());
1727 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1728 break;
1729
1730 default:
1731 LOG(FATAL) << "Unexpected type conversion from " << input_type
1732 << " to " << result_type;
1733 }
1734 break;
1735
Roland Levillain946e1432014-11-11 17:35:19 +00001736 case Primitive::kPrimInt:
1737 switch (input_type) {
1738 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001739 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001740 locations->SetInAt(0, Location::Any());
1741 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1742 break;
1743
1744 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001745 // Processing a Dex `float-to-int' instruction.
1746 locations->SetInAt(0, Location::RequiresFpuRegister());
1747 locations->SetOut(Location::RequiresRegister());
1748 locations->AddTemp(Location::RequiresFpuRegister());
1749 break;
1750
Roland Levillain946e1432014-11-11 17:35:19 +00001751 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001752 // Processing a Dex `double-to-int' instruction.
1753 locations->SetInAt(0, Location::RequiresFpuRegister());
1754 locations->SetOut(Location::RequiresRegister());
1755 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001756 break;
1757
1758 default:
1759 LOG(FATAL) << "Unexpected type conversion from " << input_type
1760 << " to " << result_type;
1761 }
1762 break;
1763
Roland Levillaindff1f282014-11-05 14:15:05 +00001764 case Primitive::kPrimLong:
1765 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001766 case Primitive::kPrimBoolean:
1767 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001768 case Primitive::kPrimByte:
1769 case Primitive::kPrimShort:
1770 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001771 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001772 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001773 locations->SetInAt(0, Location::RegisterLocation(EAX));
1774 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1775 break;
1776
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001777 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001778 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001779 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001780 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001781 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1782 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1783
Vladimir Marko949c91f2015-01-27 10:48:44 +00001784 // The runtime helper puts the result in EAX, EDX.
1785 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001786 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001787 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001788
1789 default:
1790 LOG(FATAL) << "Unexpected type conversion from " << input_type
1791 << " to " << result_type;
1792 }
1793 break;
1794
Roland Levillain981e4542014-11-14 11:47:14 +00001795 case Primitive::kPrimChar:
1796 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001797 case Primitive::kPrimBoolean:
1798 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001799 case Primitive::kPrimByte:
1800 case Primitive::kPrimShort:
1801 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001802 // Processing a Dex `int-to-char' instruction.
1803 locations->SetInAt(0, Location::Any());
1804 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1805 break;
1806
1807 default:
1808 LOG(FATAL) << "Unexpected type conversion from " << input_type
1809 << " to " << result_type;
1810 }
1811 break;
1812
Roland Levillaindff1f282014-11-05 14:15:05 +00001813 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001814 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001815 case Primitive::kPrimBoolean:
1816 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001817 case Primitive::kPrimByte:
1818 case Primitive::kPrimShort:
1819 case Primitive::kPrimInt:
1820 case Primitive::kPrimChar:
1821 // Processing a Dex `int-to-float' instruction.
1822 locations->SetInAt(0, Location::RequiresRegister());
1823 locations->SetOut(Location::RequiresFpuRegister());
1824 break;
1825
1826 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001827 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001828 locations->SetInAt(0, Location::Any());
1829 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001830 break;
1831
Roland Levillaincff13742014-11-17 14:32:17 +00001832 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001833 // Processing a Dex `double-to-float' instruction.
1834 locations->SetInAt(0, Location::RequiresFpuRegister());
1835 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001836 break;
1837
1838 default:
1839 LOG(FATAL) << "Unexpected type conversion from " << input_type
1840 << " to " << result_type;
1841 };
1842 break;
1843
Roland Levillaindff1f282014-11-05 14:15:05 +00001844 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001845 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001846 case Primitive::kPrimBoolean:
1847 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001848 case Primitive::kPrimByte:
1849 case Primitive::kPrimShort:
1850 case Primitive::kPrimInt:
1851 case Primitive::kPrimChar:
1852 // Processing a Dex `int-to-double' instruction.
1853 locations->SetInAt(0, Location::RequiresRegister());
1854 locations->SetOut(Location::RequiresFpuRegister());
1855 break;
1856
1857 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001858 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001859 locations->SetInAt(0, Location::Any());
1860 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001861 break;
1862
Roland Levillaincff13742014-11-17 14:32:17 +00001863 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001864 // Processing a Dex `float-to-double' instruction.
1865 locations->SetInAt(0, Location::RequiresFpuRegister());
1866 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001867 break;
1868
1869 default:
1870 LOG(FATAL) << "Unexpected type conversion from " << input_type
1871 << " to " << result_type;
1872 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001873 break;
1874
1875 default:
1876 LOG(FATAL) << "Unexpected type conversion from " << input_type
1877 << " to " << result_type;
1878 }
1879}
1880
1881void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1882 LocationSummary* locations = conversion->GetLocations();
1883 Location out = locations->Out();
1884 Location in = locations->InAt(0);
1885 Primitive::Type result_type = conversion->GetResultType();
1886 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001887 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001888 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001889 case Primitive::kPrimByte:
1890 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001891 case Primitive::kPrimBoolean:
1892 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001893 case Primitive::kPrimShort:
1894 case Primitive::kPrimInt:
1895 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001896 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001897 if (in.IsRegister()) {
1898 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001899 } else {
1900 DCHECK(in.GetConstant()->IsIntConstant());
1901 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1902 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1903 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001904 break;
1905
1906 default:
1907 LOG(FATAL) << "Unexpected type conversion from " << input_type
1908 << " to " << result_type;
1909 }
1910 break;
1911
Roland Levillain01a8d712014-11-14 16:27:39 +00001912 case Primitive::kPrimShort:
1913 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001914 case Primitive::kPrimBoolean:
1915 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001916 case Primitive::kPrimByte:
1917 case Primitive::kPrimInt:
1918 case Primitive::kPrimChar:
1919 // Processing a Dex `int-to-short' instruction.
1920 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001921 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001922 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001923 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001924 } else {
1925 DCHECK(in.GetConstant()->IsIntConstant());
1926 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001927 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001928 }
1929 break;
1930
1931 default:
1932 LOG(FATAL) << "Unexpected type conversion from " << input_type
1933 << " to " << result_type;
1934 }
1935 break;
1936
Roland Levillain946e1432014-11-11 17:35:19 +00001937 case Primitive::kPrimInt:
1938 switch (input_type) {
1939 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001940 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001941 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001942 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001943 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001944 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001945 } else {
1946 DCHECK(in.IsConstant());
1947 DCHECK(in.GetConstant()->IsLongConstant());
1948 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001949 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001950 }
1951 break;
1952
Roland Levillain3f8f9362014-12-02 17:45:01 +00001953 case Primitive::kPrimFloat: {
1954 // Processing a Dex `float-to-int' instruction.
1955 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1956 Register output = out.AsRegister<Register>();
1957 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1958 Label done, nan;
1959
1960 __ movl(output, Immediate(kPrimIntMax));
1961 // temp = int-to-float(output)
1962 __ cvtsi2ss(temp, output);
1963 // if input >= temp goto done
1964 __ comiss(input, temp);
1965 __ j(kAboveEqual, &done);
1966 // if input == NaN goto nan
1967 __ j(kUnordered, &nan);
1968 // output = float-to-int-truncate(input)
1969 __ cvttss2si(output, input);
1970 __ jmp(&done);
1971 __ Bind(&nan);
1972 // output = 0
1973 __ xorl(output, output);
1974 __ Bind(&done);
1975 break;
1976 }
1977
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001978 case Primitive::kPrimDouble: {
1979 // Processing a Dex `double-to-int' instruction.
1980 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1981 Register output = out.AsRegister<Register>();
1982 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1983 Label done, nan;
1984
1985 __ movl(output, Immediate(kPrimIntMax));
1986 // temp = int-to-double(output)
1987 __ cvtsi2sd(temp, output);
1988 // if input >= temp goto done
1989 __ comisd(input, temp);
1990 __ j(kAboveEqual, &done);
1991 // if input == NaN goto nan
1992 __ j(kUnordered, &nan);
1993 // output = double-to-int-truncate(input)
1994 __ cvttsd2si(output, input);
1995 __ jmp(&done);
1996 __ Bind(&nan);
1997 // output = 0
1998 __ xorl(output, output);
1999 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002000 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002001 }
Roland Levillain946e1432014-11-11 17:35:19 +00002002
2003 default:
2004 LOG(FATAL) << "Unexpected type conversion from " << input_type
2005 << " to " << result_type;
2006 }
2007 break;
2008
Roland Levillaindff1f282014-11-05 14:15:05 +00002009 case Primitive::kPrimLong:
2010 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002011 case Primitive::kPrimBoolean:
2012 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002013 case Primitive::kPrimByte:
2014 case Primitive::kPrimShort:
2015 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002016 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002017 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002018 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2019 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002020 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002021 __ cdq();
2022 break;
2023
2024 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002025 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002026 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2027 conversion,
2028 conversion->GetDexPc(),
2029 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002030 break;
2031
Roland Levillaindff1f282014-11-05 14:15:05 +00002032 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002033 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002034 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2035 conversion,
2036 conversion->GetDexPc(),
2037 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002038 break;
2039
2040 default:
2041 LOG(FATAL) << "Unexpected type conversion from " << input_type
2042 << " to " << result_type;
2043 }
2044 break;
2045
Roland Levillain981e4542014-11-14 11:47:14 +00002046 case Primitive::kPrimChar:
2047 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002048 case Primitive::kPrimBoolean:
2049 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002050 case Primitive::kPrimByte:
2051 case Primitive::kPrimShort:
2052 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002053 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2054 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002055 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002056 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002057 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002058 } else {
2059 DCHECK(in.GetConstant()->IsIntConstant());
2060 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002061 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002062 }
2063 break;
2064
2065 default:
2066 LOG(FATAL) << "Unexpected type conversion from " << input_type
2067 << " to " << result_type;
2068 }
2069 break;
2070
Roland Levillaindff1f282014-11-05 14:15:05 +00002071 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002072 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002073 case Primitive::kPrimBoolean:
2074 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002075 case Primitive::kPrimByte:
2076 case Primitive::kPrimShort:
2077 case Primitive::kPrimInt:
2078 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002079 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002080 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002081 break;
2082
Roland Levillain6d0e4832014-11-27 18:31:21 +00002083 case Primitive::kPrimLong: {
2084 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002085 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002086
Roland Levillain232ade02015-04-20 15:14:36 +01002087 // Create stack space for the call to
2088 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2089 // TODO: enhance register allocator to ask for stack temporaries.
2090 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2091 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2092 __ subl(ESP, Immediate(adjustment));
2093 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002094
Roland Levillain232ade02015-04-20 15:14:36 +01002095 // Load the value to the FP stack, using temporaries if needed.
2096 PushOntoFPStack(in, 0, adjustment, false, true);
2097
2098 if (out.IsStackSlot()) {
2099 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2100 } else {
2101 __ fstps(Address(ESP, 0));
2102 Location stack_temp = Location::StackSlot(0);
2103 codegen_->Move32(out, stack_temp);
2104 }
2105
2106 // Remove the temporary stack space we allocated.
2107 if (adjustment != 0) {
2108 __ addl(ESP, Immediate(adjustment));
2109 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002110 break;
2111 }
2112
Roland Levillaincff13742014-11-17 14:32:17 +00002113 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002114 // Processing a Dex `double-to-float' instruction.
2115 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002116 break;
2117
2118 default:
2119 LOG(FATAL) << "Unexpected type conversion from " << input_type
2120 << " to " << result_type;
2121 };
2122 break;
2123
Roland Levillaindff1f282014-11-05 14:15:05 +00002124 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002125 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002126 case Primitive::kPrimBoolean:
2127 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002128 case Primitive::kPrimByte:
2129 case Primitive::kPrimShort:
2130 case Primitive::kPrimInt:
2131 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002132 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002133 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002134 break;
2135
Roland Levillain647b9ed2014-11-27 12:06:00 +00002136 case Primitive::kPrimLong: {
2137 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002138 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002139
Roland Levillain232ade02015-04-20 15:14:36 +01002140 // Create stack space for the call to
2141 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2142 // TODO: enhance register allocator to ask for stack temporaries.
2143 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2144 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2145 __ subl(ESP, Immediate(adjustment));
2146 }
2147
2148 // Load the value to the FP stack, using temporaries if needed.
2149 PushOntoFPStack(in, 0, adjustment, false, true);
2150
2151 if (out.IsDoubleStackSlot()) {
2152 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2153 } else {
2154 __ fstpl(Address(ESP, 0));
2155 Location stack_temp = Location::DoubleStackSlot(0);
2156 codegen_->Move64(out, stack_temp);
2157 }
2158
2159 // Remove the temporary stack space we allocated.
2160 if (adjustment != 0) {
2161 __ addl(ESP, Immediate(adjustment));
2162 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002163 break;
2164 }
2165
Roland Levillaincff13742014-11-17 14:32:17 +00002166 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002167 // Processing a Dex `float-to-double' instruction.
2168 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002169 break;
2170
2171 default:
2172 LOG(FATAL) << "Unexpected type conversion from " << input_type
2173 << " to " << result_type;
2174 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002175 break;
2176
2177 default:
2178 LOG(FATAL) << "Unexpected type conversion from " << input_type
2179 << " to " << result_type;
2180 }
2181}
2182
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002183void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002184 LocationSummary* locations =
2185 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002186 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002187 case Primitive::kPrimInt: {
2188 locations->SetInAt(0, Location::RequiresRegister());
2189 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2190 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2191 break;
2192 }
2193
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002194 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002195 locations->SetInAt(0, Location::RequiresRegister());
2196 locations->SetInAt(1, Location::Any());
2197 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002198 break;
2199 }
2200
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002201 case Primitive::kPrimFloat:
2202 case Primitive::kPrimDouble: {
2203 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00002204 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002205 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002206 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002207 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002208
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002209 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002210 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2211 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002212 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002213}
2214
2215void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2216 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002217 Location first = locations->InAt(0);
2218 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002219 Location out = locations->Out();
2220
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002221 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002222 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002223 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002224 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2225 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002226 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2227 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002228 } else {
2229 __ leal(out.AsRegister<Register>(), Address(
2230 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2231 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002232 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002233 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2234 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2235 __ addl(out.AsRegister<Register>(), Immediate(value));
2236 } else {
2237 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2238 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002239 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002240 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002241 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002242 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002243 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002244 }
2245
2246 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002247 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002248 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2249 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002250 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002251 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2252 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002253 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002254 } else {
2255 DCHECK(second.IsConstant()) << second;
2256 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2257 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2258 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002259 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002260 break;
2261 }
2262
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002263 case Primitive::kPrimFloat: {
2264 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002265 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002266 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002267 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002268 }
2269
2270 case Primitive::kPrimDouble: {
2271 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002272 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002273 }
2274 break;
2275 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002276
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002277 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002278 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002279 }
2280}
2281
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002282void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002283 LocationSummary* locations =
2284 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002285 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002286 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002287 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002288 locations->SetInAt(0, Location::RequiresRegister());
2289 locations->SetInAt(1, Location::Any());
2290 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002291 break;
2292 }
Calin Juravle11351682014-10-23 15:38:15 +01002293 case Primitive::kPrimFloat:
2294 case Primitive::kPrimDouble: {
2295 locations->SetInAt(0, Location::RequiresFpuRegister());
2296 locations->SetInAt(1, Location::RequiresFpuRegister());
2297 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002298 break;
Calin Juravle11351682014-10-23 15:38:15 +01002299 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002300
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002301 default:
Calin Juravle11351682014-10-23 15:38:15 +01002302 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002303 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002304}
2305
2306void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2307 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002308 Location first = locations->InAt(0);
2309 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002310 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002311 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002312 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002313 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002314 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002315 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002316 __ subl(first.AsRegister<Register>(),
2317 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002318 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002319 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002320 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002321 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002322 }
2323
2324 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002325 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002326 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2327 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002328 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002329 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002330 __ sbbl(first.AsRegisterPairHigh<Register>(),
2331 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002332 } else {
2333 DCHECK(second.IsConstant()) << second;
2334 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2335 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2336 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002337 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002338 break;
2339 }
2340
Calin Juravle11351682014-10-23 15:38:15 +01002341 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002342 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002343 break;
Calin Juravle11351682014-10-23 15:38:15 +01002344 }
2345
2346 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002347 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002348 break;
2349 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002350
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002351 default:
Calin Juravle11351682014-10-23 15:38:15 +01002352 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002353 }
2354}
2355
Calin Juravle34bacdf2014-10-07 20:23:36 +01002356void LocationsBuilderX86::VisitMul(HMul* mul) {
2357 LocationSummary* locations =
2358 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2359 switch (mul->GetResultType()) {
2360 case Primitive::kPrimInt:
2361 locations->SetInAt(0, Location::RequiresRegister());
2362 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002363 if (mul->InputAt(1)->IsIntConstant()) {
2364 // Can use 3 operand multiply.
2365 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2366 } else {
2367 locations->SetOut(Location::SameAsFirstInput());
2368 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002369 break;
2370 case Primitive::kPrimLong: {
2371 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002372 locations->SetInAt(1, Location::Any());
2373 locations->SetOut(Location::SameAsFirstInput());
2374 // Needed for imul on 32bits with 64bits output.
2375 locations->AddTemp(Location::RegisterLocation(EAX));
2376 locations->AddTemp(Location::RegisterLocation(EDX));
2377 break;
2378 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002379 case Primitive::kPrimFloat:
2380 case Primitive::kPrimDouble: {
2381 locations->SetInAt(0, Location::RequiresFpuRegister());
2382 locations->SetInAt(1, Location::RequiresFpuRegister());
2383 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002384 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002385 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002386
2387 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002388 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002389 }
2390}
2391
2392void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2393 LocationSummary* locations = mul->GetLocations();
2394 Location first = locations->InAt(0);
2395 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002396 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002397
2398 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002399 case Primitive::kPrimInt:
2400 // The constant may have ended up in a register, so test explicitly to avoid
2401 // problems where the output may not be the same as the first operand.
2402 if (mul->InputAt(1)->IsIntConstant()) {
2403 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2404 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2405 } else if (second.IsRegister()) {
2406 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002407 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002408 } else {
2409 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002410 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002411 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002412 }
2413 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002414
2415 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002416 Register in1_hi = first.AsRegisterPairHigh<Register>();
2417 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002418 Register eax = locations->GetTemp(0).AsRegister<Register>();
2419 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002420
2421 DCHECK_EQ(EAX, eax);
2422 DCHECK_EQ(EDX, edx);
2423
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002424 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002425 // output: in1
2426 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2427 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2428 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002429 if (second.IsConstant()) {
2430 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002431
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002432 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2433 int32_t low_value = Low32Bits(value);
2434 int32_t high_value = High32Bits(value);
2435 Immediate low(low_value);
2436 Immediate high(high_value);
2437
2438 __ movl(eax, high);
2439 // eax <- in1.lo * in2.hi
2440 __ imull(eax, in1_lo);
2441 // in1.hi <- in1.hi * in2.lo
2442 __ imull(in1_hi, low);
2443 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2444 __ addl(in1_hi, eax);
2445 // move in2_lo to eax to prepare for double precision
2446 __ movl(eax, low);
2447 // edx:eax <- in1.lo * in2.lo
2448 __ mull(in1_lo);
2449 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2450 __ addl(in1_hi, edx);
2451 // in1.lo <- (in1.lo * in2.lo)[31:0];
2452 __ movl(in1_lo, eax);
2453 } else if (second.IsRegisterPair()) {
2454 Register in2_hi = second.AsRegisterPairHigh<Register>();
2455 Register in2_lo = second.AsRegisterPairLow<Register>();
2456
2457 __ movl(eax, in2_hi);
2458 // eax <- in1.lo * in2.hi
2459 __ imull(eax, in1_lo);
2460 // in1.hi <- in1.hi * in2.lo
2461 __ imull(in1_hi, in2_lo);
2462 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2463 __ addl(in1_hi, eax);
2464 // move in1_lo to eax to prepare for double precision
2465 __ movl(eax, in1_lo);
2466 // edx:eax <- in1.lo * in2.lo
2467 __ mull(in2_lo);
2468 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2469 __ addl(in1_hi, edx);
2470 // in1.lo <- (in1.lo * in2.lo)[31:0];
2471 __ movl(in1_lo, eax);
2472 } else {
2473 DCHECK(second.IsDoubleStackSlot()) << second;
2474 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2475 Address in2_lo(ESP, second.GetStackIndex());
2476
2477 __ movl(eax, in2_hi);
2478 // eax <- in1.lo * in2.hi
2479 __ imull(eax, in1_lo);
2480 // in1.hi <- in1.hi * in2.lo
2481 __ imull(in1_hi, in2_lo);
2482 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2483 __ addl(in1_hi, eax);
2484 // move in1_lo to eax to prepare for double precision
2485 __ movl(eax, in1_lo);
2486 // edx:eax <- in1.lo * in2.lo
2487 __ mull(in2_lo);
2488 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2489 __ addl(in1_hi, edx);
2490 // in1.lo <- (in1.lo * in2.lo)[31:0];
2491 __ movl(in1_lo, eax);
2492 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002493
2494 break;
2495 }
2496
Calin Juravleb5bfa962014-10-21 18:02:24 +01002497 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002498 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002499 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002500 }
2501
2502 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002503 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002504 break;
2505 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002506
2507 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002508 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002509 }
2510}
2511
Roland Levillain232ade02015-04-20 15:14:36 +01002512void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2513 uint32_t temp_offset,
2514 uint32_t stack_adjustment,
2515 bool is_fp,
2516 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002517 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002518 DCHECK(!is_wide);
2519 if (is_fp) {
2520 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2521 } else {
2522 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2523 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002524 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002525 DCHECK(is_wide);
2526 if (is_fp) {
2527 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2528 } else {
2529 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2530 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002531 } else {
2532 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002533 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002534 Location stack_temp = Location::StackSlot(temp_offset);
2535 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002536 if (is_fp) {
2537 __ flds(Address(ESP, temp_offset));
2538 } else {
2539 __ filds(Address(ESP, temp_offset));
2540 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002541 } else {
2542 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2543 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002544 if (is_fp) {
2545 __ fldl(Address(ESP, temp_offset));
2546 } else {
2547 __ fildl(Address(ESP, temp_offset));
2548 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002549 }
2550 }
2551}
2552
2553void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2554 Primitive::Type type = rem->GetResultType();
2555 bool is_float = type == Primitive::kPrimFloat;
2556 size_t elem_size = Primitive::ComponentSize(type);
2557 LocationSummary* locations = rem->GetLocations();
2558 Location first = locations->InAt(0);
2559 Location second = locations->InAt(1);
2560 Location out = locations->Out();
2561
2562 // Create stack space for 2 elements.
2563 // TODO: enhance register allocator to ask for stack temporaries.
2564 __ subl(ESP, Immediate(2 * elem_size));
2565
2566 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002567 const bool is_wide = !is_float;
2568 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2569 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002570
2571 // Loop doing FPREM until we stabilize.
2572 Label retry;
2573 __ Bind(&retry);
2574 __ fprem();
2575
2576 // Move FP status to AX.
2577 __ fstsw();
2578
2579 // And see if the argument reduction is complete. This is signaled by the
2580 // C2 FPU flag bit set to 0.
2581 __ andl(EAX, Immediate(kC2ConditionMask));
2582 __ j(kNotEqual, &retry);
2583
2584 // We have settled on the final value. Retrieve it into an XMM register.
2585 // Store FP top of stack to real stack.
2586 if (is_float) {
2587 __ fsts(Address(ESP, 0));
2588 } else {
2589 __ fstl(Address(ESP, 0));
2590 }
2591
2592 // Pop the 2 items from the FP stack.
2593 __ fucompp();
2594
2595 // Load the value from the stack into an XMM register.
2596 DCHECK(out.IsFpuRegister()) << out;
2597 if (is_float) {
2598 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2599 } else {
2600 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2601 }
2602
2603 // And remove the temporary stack space we allocated.
2604 __ addl(ESP, Immediate(2 * elem_size));
2605}
2606
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002607
2608void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2609 DCHECK(instruction->IsDiv() || instruction->IsRem());
2610
2611 LocationSummary* locations = instruction->GetLocations();
2612 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002613 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002614
2615 Register out_register = locations->Out().AsRegister<Register>();
2616 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002617 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002618
2619 DCHECK(imm == 1 || imm == -1);
2620
2621 if (instruction->IsRem()) {
2622 __ xorl(out_register, out_register);
2623 } else {
2624 __ movl(out_register, input_register);
2625 if (imm == -1) {
2626 __ negl(out_register);
2627 }
2628 }
2629}
2630
2631
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002632void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002633 LocationSummary* locations = instruction->GetLocations();
2634
2635 Register out_register = locations->Out().AsRegister<Register>();
2636 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002637 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002638
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002639 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002640 Register num = locations->GetTemp(0).AsRegister<Register>();
2641
2642 __ leal(num, Address(input_register, std::abs(imm) - 1));
2643 __ testl(input_register, input_register);
2644 __ cmovl(kGreaterEqual, num, input_register);
2645 int shift = CTZ(imm);
2646 __ sarl(num, Immediate(shift));
2647
2648 if (imm < 0) {
2649 __ negl(num);
2650 }
2651
2652 __ movl(out_register, num);
2653}
2654
2655void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2656 DCHECK(instruction->IsDiv() || instruction->IsRem());
2657
2658 LocationSummary* locations = instruction->GetLocations();
2659 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2660
2661 Register eax = locations->InAt(0).AsRegister<Register>();
2662 Register out = locations->Out().AsRegister<Register>();
2663 Register num;
2664 Register edx;
2665
2666 if (instruction->IsDiv()) {
2667 edx = locations->GetTemp(0).AsRegister<Register>();
2668 num = locations->GetTemp(1).AsRegister<Register>();
2669 } else {
2670 edx = locations->Out().AsRegister<Register>();
2671 num = locations->GetTemp(0).AsRegister<Register>();
2672 }
2673
2674 DCHECK_EQ(EAX, eax);
2675 DCHECK_EQ(EDX, edx);
2676 if (instruction->IsDiv()) {
2677 DCHECK_EQ(EAX, out);
2678 } else {
2679 DCHECK_EQ(EDX, out);
2680 }
2681
2682 int64_t magic;
2683 int shift;
2684 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2685
2686 Label ndiv;
2687 Label end;
2688 // If numerator is 0, the result is 0, no computation needed.
2689 __ testl(eax, eax);
2690 __ j(kNotEqual, &ndiv);
2691
2692 __ xorl(out, out);
2693 __ jmp(&end);
2694
2695 __ Bind(&ndiv);
2696
2697 // Save the numerator.
2698 __ movl(num, eax);
2699
2700 // EAX = magic
2701 __ movl(eax, Immediate(magic));
2702
2703 // EDX:EAX = magic * numerator
2704 __ imull(num);
2705
2706 if (imm > 0 && magic < 0) {
2707 // EDX += num
2708 __ addl(edx, num);
2709 } else if (imm < 0 && magic > 0) {
2710 __ subl(edx, num);
2711 }
2712
2713 // Shift if needed.
2714 if (shift != 0) {
2715 __ sarl(edx, Immediate(shift));
2716 }
2717
2718 // EDX += 1 if EDX < 0
2719 __ movl(eax, edx);
2720 __ shrl(edx, Immediate(31));
2721 __ addl(edx, eax);
2722
2723 if (instruction->IsRem()) {
2724 __ movl(eax, num);
2725 __ imull(edx, Immediate(imm));
2726 __ subl(eax, edx);
2727 __ movl(edx, eax);
2728 } else {
2729 __ movl(eax, edx);
2730 }
2731 __ Bind(&end);
2732}
2733
Calin Juravlebacfec32014-11-14 15:54:36 +00002734void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2735 DCHECK(instruction->IsDiv() || instruction->IsRem());
2736
2737 LocationSummary* locations = instruction->GetLocations();
2738 Location out = locations->Out();
2739 Location first = locations->InAt(0);
2740 Location second = locations->InAt(1);
2741 bool is_div = instruction->IsDiv();
2742
2743 switch (instruction->GetResultType()) {
2744 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002745 DCHECK_EQ(EAX, first.AsRegister<Register>());
2746 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002747
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002748 if (instruction->InputAt(1)->IsIntConstant()) {
2749 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002750
2751 if (imm == 0) {
2752 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2753 } else if (imm == 1 || imm == -1) {
2754 DivRemOneOrMinusOne(instruction);
2755 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002756 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002757 } else {
2758 DCHECK(imm <= -2 || imm >= 2);
2759 GenerateDivRemWithAnyConstant(instruction);
2760 }
2761 } else {
2762 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002763 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002764 is_div);
2765 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002766
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002767 Register second_reg = second.AsRegister<Register>();
2768 // 0x80000000/-1 triggers an arithmetic exception!
2769 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2770 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002771
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002772 __ cmpl(second_reg, Immediate(-1));
2773 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002774
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002775 // edx:eax <- sign-extended of eax
2776 __ cdq();
2777 // eax = quotient, edx = remainder
2778 __ idivl(second_reg);
2779 __ Bind(slow_path->GetExitLabel());
2780 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002781 break;
2782 }
2783
2784 case Primitive::kPrimLong: {
2785 InvokeRuntimeCallingConvention calling_convention;
2786 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2787 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2788 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2789 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2790 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2791 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2792
2793 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002794 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2795 instruction,
2796 instruction->GetDexPc(),
2797 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002798 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002799 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2800 instruction,
2801 instruction->GetDexPc(),
2802 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002803 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002804 break;
2805 }
2806
2807 default:
2808 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2809 }
2810}
2811
Calin Juravle7c4954d2014-10-28 16:57:40 +00002812void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002813 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002814 ? LocationSummary::kCall
2815 : LocationSummary::kNoCall;
2816 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2817
Calin Juravle7c4954d2014-10-28 16:57:40 +00002818 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002819 case Primitive::kPrimInt: {
2820 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002821 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002822 locations->SetOut(Location::SameAsFirstInput());
2823 // Intel uses edx:eax as the dividend.
2824 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002825 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2826 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2827 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002828 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002829 locations->AddTemp(Location::RequiresRegister());
2830 }
Calin Juravled0d48522014-11-04 16:40:20 +00002831 break;
2832 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002833 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002834 InvokeRuntimeCallingConvention calling_convention;
2835 locations->SetInAt(0, Location::RegisterPairLocation(
2836 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2837 locations->SetInAt(1, Location::RegisterPairLocation(
2838 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2839 // Runtime helper puts the result in EAX, EDX.
2840 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002841 break;
2842 }
2843 case Primitive::kPrimFloat:
2844 case Primitive::kPrimDouble: {
2845 locations->SetInAt(0, Location::RequiresFpuRegister());
2846 locations->SetInAt(1, Location::RequiresFpuRegister());
2847 locations->SetOut(Location::SameAsFirstInput());
2848 break;
2849 }
2850
2851 default:
2852 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2853 }
2854}
2855
2856void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2857 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002858 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002859 Location first = locations->InAt(0);
2860 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002861
2862 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002863 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002864 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002865 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002866 break;
2867 }
2868
2869 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002870 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002871 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002872 break;
2873 }
2874
2875 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002876 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002877 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002878 break;
2879 }
2880
2881 default:
2882 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2883 }
2884}
2885
Calin Juravlebacfec32014-11-14 15:54:36 +00002886void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002887 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002888
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002889 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2890 ? LocationSummary::kCall
2891 : LocationSummary::kNoCall;
2892 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002893
Calin Juravled2ec87d2014-12-08 14:24:46 +00002894 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002895 case Primitive::kPrimInt: {
2896 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002897 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002898 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002899 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2900 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2901 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002902 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002903 locations->AddTemp(Location::RequiresRegister());
2904 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002905 break;
2906 }
2907 case Primitive::kPrimLong: {
2908 InvokeRuntimeCallingConvention calling_convention;
2909 locations->SetInAt(0, Location::RegisterPairLocation(
2910 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2911 locations->SetInAt(1, Location::RegisterPairLocation(
2912 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2913 // Runtime helper puts the result in EAX, EDX.
2914 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2915 break;
2916 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002917 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002918 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002919 locations->SetInAt(0, Location::Any());
2920 locations->SetInAt(1, Location::Any());
2921 locations->SetOut(Location::RequiresFpuRegister());
2922 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002923 break;
2924 }
2925
2926 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002927 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002928 }
2929}
2930
2931void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2932 Primitive::Type type = rem->GetResultType();
2933 switch (type) {
2934 case Primitive::kPrimInt:
2935 case Primitive::kPrimLong: {
2936 GenerateDivRemIntegral(rem);
2937 break;
2938 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002939 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002940 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002941 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002942 break;
2943 }
2944 default:
2945 LOG(FATAL) << "Unexpected rem type " << type;
2946 }
2947}
2948
Calin Juravled0d48522014-11-04 16:40:20 +00002949void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2950 LocationSummary* locations =
2951 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002952 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002953 case Primitive::kPrimByte:
2954 case Primitive::kPrimChar:
2955 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002956 case Primitive::kPrimInt: {
2957 locations->SetInAt(0, Location::Any());
2958 break;
2959 }
2960 case Primitive::kPrimLong: {
2961 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2962 if (!instruction->IsConstant()) {
2963 locations->AddTemp(Location::RequiresRegister());
2964 }
2965 break;
2966 }
2967 default:
2968 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2969 }
Calin Juravled0d48522014-11-04 16:40:20 +00002970 if (instruction->HasUses()) {
2971 locations->SetOut(Location::SameAsFirstInput());
2972 }
2973}
2974
2975void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2976 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2977 codegen_->AddSlowPath(slow_path);
2978
2979 LocationSummary* locations = instruction->GetLocations();
2980 Location value = locations->InAt(0);
2981
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002982 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002983 case Primitive::kPrimByte:
2984 case Primitive::kPrimChar:
2985 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002986 case Primitive::kPrimInt: {
2987 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002988 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002989 __ j(kEqual, slow_path->GetEntryLabel());
2990 } else if (value.IsStackSlot()) {
2991 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2992 __ j(kEqual, slow_path->GetEntryLabel());
2993 } else {
2994 DCHECK(value.IsConstant()) << value;
2995 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2996 __ jmp(slow_path->GetEntryLabel());
2997 }
2998 }
2999 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003000 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003001 case Primitive::kPrimLong: {
3002 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003003 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003004 __ movl(temp, value.AsRegisterPairLow<Register>());
3005 __ orl(temp, value.AsRegisterPairHigh<Register>());
3006 __ j(kEqual, slow_path->GetEntryLabel());
3007 } else {
3008 DCHECK(value.IsConstant()) << value;
3009 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3010 __ jmp(slow_path->GetEntryLabel());
3011 }
3012 }
3013 break;
3014 }
3015 default:
3016 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003017 }
Calin Juravled0d48522014-11-04 16:40:20 +00003018}
3019
Calin Juravle9aec02f2014-11-18 23:06:35 +00003020void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3021 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3022
3023 LocationSummary* locations =
3024 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3025
3026 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003027 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003028 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003029 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003030 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003031 // The shift count needs to be in CL or a constant.
3032 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003033 locations->SetOut(Location::SameAsFirstInput());
3034 break;
3035 }
3036 default:
3037 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3038 }
3039}
3040
3041void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3042 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3043
3044 LocationSummary* locations = op->GetLocations();
3045 Location first = locations->InAt(0);
3046 Location second = locations->InAt(1);
3047 DCHECK(first.Equals(locations->Out()));
3048
3049 switch (op->GetResultType()) {
3050 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003051 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003052 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003053 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003054 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003055 DCHECK_EQ(ECX, second_reg);
3056 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003057 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003058 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003059 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003060 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003061 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003062 }
3063 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003064 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3065 if (shift == 0) {
3066 return;
3067 }
3068 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003069 if (op->IsShl()) {
3070 __ shll(first_reg, imm);
3071 } else if (op->IsShr()) {
3072 __ sarl(first_reg, imm);
3073 } else {
3074 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003075 }
3076 }
3077 break;
3078 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003079 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003080 if (second.IsRegister()) {
3081 Register second_reg = second.AsRegister<Register>();
3082 DCHECK_EQ(ECX, second_reg);
3083 if (op->IsShl()) {
3084 GenerateShlLong(first, second_reg);
3085 } else if (op->IsShr()) {
3086 GenerateShrLong(first, second_reg);
3087 } else {
3088 GenerateUShrLong(first, second_reg);
3089 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003090 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003091 // Shift by a constant.
3092 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3093 // Nothing to do if the shift is 0, as the input is already the output.
3094 if (shift != 0) {
3095 if (op->IsShl()) {
3096 GenerateShlLong(first, shift);
3097 } else if (op->IsShr()) {
3098 GenerateShrLong(first, shift);
3099 } else {
3100 GenerateUShrLong(first, shift);
3101 }
3102 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003103 }
3104 break;
3105 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003106 default:
3107 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3108 }
3109}
3110
Mark P Mendell73945692015-04-29 14:56:17 +00003111void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3112 Register low = loc.AsRegisterPairLow<Register>();
3113 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003114 if (shift == 1) {
3115 // This is just an addition.
3116 __ addl(low, low);
3117 __ adcl(high, high);
3118 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003119 // Shift by 32 is easy. High gets low, and low gets 0.
3120 codegen_->EmitParallelMoves(
3121 loc.ToLow(),
3122 loc.ToHigh(),
3123 Primitive::kPrimInt,
3124 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3125 loc.ToLow(),
3126 Primitive::kPrimInt);
3127 } else if (shift > 32) {
3128 // Low part becomes 0. High part is low part << (shift-32).
3129 __ movl(high, low);
3130 __ shll(high, Immediate(shift - 32));
3131 __ xorl(low, low);
3132 } else {
3133 // Between 1 and 31.
3134 __ shld(high, low, Immediate(shift));
3135 __ shll(low, Immediate(shift));
3136 }
3137}
3138
Calin Juravle9aec02f2014-11-18 23:06:35 +00003139void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
3140 Label done;
3141 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3142 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3143 __ testl(shifter, Immediate(32));
3144 __ j(kEqual, &done);
3145 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3146 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3147 __ Bind(&done);
3148}
3149
Mark P Mendell73945692015-04-29 14:56:17 +00003150void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3151 Register low = loc.AsRegisterPairLow<Register>();
3152 Register high = loc.AsRegisterPairHigh<Register>();
3153 if (shift == 32) {
3154 // Need to copy the sign.
3155 DCHECK_NE(low, high);
3156 __ movl(low, high);
3157 __ sarl(high, Immediate(31));
3158 } else if (shift > 32) {
3159 DCHECK_NE(low, high);
3160 // High part becomes sign. Low part is shifted by shift - 32.
3161 __ movl(low, high);
3162 __ sarl(high, Immediate(31));
3163 __ sarl(low, Immediate(shift - 32));
3164 } else {
3165 // Between 1 and 31.
3166 __ shrd(low, high, Immediate(shift));
3167 __ sarl(high, Immediate(shift));
3168 }
3169}
3170
Calin Juravle9aec02f2014-11-18 23:06:35 +00003171void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
3172 Label done;
3173 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3174 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3175 __ testl(shifter, Immediate(32));
3176 __ j(kEqual, &done);
3177 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3178 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3179 __ Bind(&done);
3180}
3181
Mark P Mendell73945692015-04-29 14:56:17 +00003182void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3183 Register low = loc.AsRegisterPairLow<Register>();
3184 Register high = loc.AsRegisterPairHigh<Register>();
3185 if (shift == 32) {
3186 // Shift by 32 is easy. Low gets high, and high gets 0.
3187 codegen_->EmitParallelMoves(
3188 loc.ToHigh(),
3189 loc.ToLow(),
3190 Primitive::kPrimInt,
3191 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3192 loc.ToHigh(),
3193 Primitive::kPrimInt);
3194 } else if (shift > 32) {
3195 // Low part is high >> (shift - 32). High part becomes 0.
3196 __ movl(low, high);
3197 __ shrl(low, Immediate(shift - 32));
3198 __ xorl(high, high);
3199 } else {
3200 // Between 1 and 31.
3201 __ shrd(low, high, Immediate(shift));
3202 __ shrl(high, Immediate(shift));
3203 }
3204}
3205
Calin Juravle9aec02f2014-11-18 23:06:35 +00003206void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
3207 Label done;
3208 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3209 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3210 __ testl(shifter, Immediate(32));
3211 __ j(kEqual, &done);
3212 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3213 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3214 __ Bind(&done);
3215}
3216
3217void LocationsBuilderX86::VisitShl(HShl* shl) {
3218 HandleShift(shl);
3219}
3220
3221void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3222 HandleShift(shl);
3223}
3224
3225void LocationsBuilderX86::VisitShr(HShr* shr) {
3226 HandleShift(shr);
3227}
3228
3229void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3230 HandleShift(shr);
3231}
3232
3233void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3234 HandleShift(ushr);
3235}
3236
3237void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3238 HandleShift(ushr);
3239}
3240
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003241void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003242 LocationSummary* locations =
3243 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003244 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003245 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003246 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003247 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003248}
3249
3250void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3251 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003252 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003253 // Note: if heap poisoning is enabled, the entry point takes cares
3254 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003255 codegen_->InvokeRuntime(
3256 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3257 instruction,
3258 instruction->GetDexPc(),
3259 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003260 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003261}
3262
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003263void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3264 LocationSummary* locations =
3265 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3266 locations->SetOut(Location::RegisterLocation(EAX));
3267 InvokeRuntimeCallingConvention calling_convention;
3268 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003269 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003270 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003271}
3272
3273void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3274 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003275 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3276
Roland Levillain4d027112015-07-01 15:41:14 +01003277 // Note: if heap poisoning is enabled, the entry point takes cares
3278 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003279 codegen_->InvokeRuntime(
3280 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3281 instruction,
3282 instruction->GetDexPc(),
3283 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003284 DCHECK(!codegen_->IsLeafMethod());
3285}
3286
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003287void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003288 LocationSummary* locations =
3289 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003290 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3291 if (location.IsStackSlot()) {
3292 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3293 } else if (location.IsDoubleStackSlot()) {
3294 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003295 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003296 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003297}
3298
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003299void InstructionCodeGeneratorX86::VisitParameterValue(
3300 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3301}
3302
3303void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3304 LocationSummary* locations =
3305 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3306 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3307}
3308
3309void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003310}
3311
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003312void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003313 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003314 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003315 locations->SetInAt(0, Location::RequiresRegister());
3316 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003317}
3318
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003319void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3320 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003321 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003322 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003323 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003324 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003325 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003326 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003327 break;
3328
3329 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003330 __ notl(out.AsRegisterPairLow<Register>());
3331 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003332 break;
3333
3334 default:
3335 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3336 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003337}
3338
David Brazdil66d126e2015-04-03 16:02:44 +01003339void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3340 LocationSummary* locations =
3341 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3342 locations->SetInAt(0, Location::RequiresRegister());
3343 locations->SetOut(Location::SameAsFirstInput());
3344}
3345
3346void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003347 LocationSummary* locations = bool_not->GetLocations();
3348 Location in = locations->InAt(0);
3349 Location out = locations->Out();
3350 DCHECK(in.Equals(out));
3351 __ xorl(out.AsRegister<Register>(), Immediate(1));
3352}
3353
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003354void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003355 LocationSummary* locations =
3356 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003357 switch (compare->InputAt(0)->GetType()) {
3358 case Primitive::kPrimLong: {
3359 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003360 locations->SetInAt(1, Location::Any());
3361 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3362 break;
3363 }
3364 case Primitive::kPrimFloat:
3365 case Primitive::kPrimDouble: {
3366 locations->SetInAt(0, Location::RequiresFpuRegister());
3367 locations->SetInAt(1, Location::RequiresFpuRegister());
3368 locations->SetOut(Location::RequiresRegister());
3369 break;
3370 }
3371 default:
3372 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3373 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003374}
3375
3376void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003377 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003378 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003379 Location left = locations->InAt(0);
3380 Location right = locations->InAt(1);
3381
3382 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003383 switch (compare->InputAt(0)->GetType()) {
3384 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003385 Register left_low = left.AsRegisterPairLow<Register>();
3386 Register left_high = left.AsRegisterPairHigh<Register>();
3387 int32_t val_low = 0;
3388 int32_t val_high = 0;
3389 bool right_is_const = false;
3390
3391 if (right.IsConstant()) {
3392 DCHECK(right.GetConstant()->IsLongConstant());
3393 right_is_const = true;
3394 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3395 val_low = Low32Bits(val);
3396 val_high = High32Bits(val);
3397 }
3398
Calin Juravleddb7df22014-11-25 20:56:51 +00003399 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003400 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003401 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003402 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003403 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003404 DCHECK(right_is_const) << right;
3405 if (val_high == 0) {
3406 __ testl(left_high, left_high);
3407 } else {
3408 __ cmpl(left_high, Immediate(val_high));
3409 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003410 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003411 __ j(kLess, &less); // Signed compare.
3412 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003413 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003414 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003415 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003416 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003417 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003418 DCHECK(right_is_const) << right;
3419 if (val_low == 0) {
3420 __ testl(left_low, left_low);
3421 } else {
3422 __ cmpl(left_low, Immediate(val_low));
3423 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003424 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003425 break;
3426 }
3427 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003428 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003429 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3430 break;
3431 }
3432 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003433 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003434 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003435 break;
3436 }
3437 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003438 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003439 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003440 __ movl(out, Immediate(0));
3441 __ j(kEqual, &done);
3442 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3443
3444 __ Bind(&greater);
3445 __ movl(out, Immediate(1));
3446 __ jmp(&done);
3447
3448 __ Bind(&less);
3449 __ movl(out, Immediate(-1));
3450
3451 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003452}
3453
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003454void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003455 LocationSummary* locations =
3456 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003457 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3458 locations->SetInAt(i, Location::Any());
3459 }
3460 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003461}
3462
3463void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003464 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003465 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003466}
3467
Calin Juravle52c48962014-12-16 17:02:57 +00003468void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3469 /*
3470 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3471 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3472 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3473 */
3474 switch (kind) {
3475 case MemBarrierKind::kAnyAny: {
3476 __ mfence();
3477 break;
3478 }
3479 case MemBarrierKind::kAnyStore:
3480 case MemBarrierKind::kLoadAny:
3481 case MemBarrierKind::kStoreStore: {
3482 // nop
3483 break;
3484 }
3485 default:
3486 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003487 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003488}
3489
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003490
Vladimir Marko58155012015-08-19 12:49:41 +00003491void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3492 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3493 switch (invoke->GetMethodLoadKind()) {
3494 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3495 // temp = thread->string_init_entrypoint
3496 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3497 break;
3498 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3499 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3500 break;
3501 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3502 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3503 break;
3504 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3505 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3506 method_patches_.emplace_back(invoke->GetTargetMethod());
3507 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3508 break;
3509 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3510 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3511 FALLTHROUGH_INTENDED;
3512 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3513 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3514 Register method_reg;
3515 Register reg = temp.AsRegister<Register>();
3516 if (current_method.IsRegister()) {
3517 method_reg = current_method.AsRegister<Register>();
3518 } else {
3519 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3520 DCHECK(!current_method.IsValid());
3521 method_reg = reg;
3522 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3523 }
3524 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003525 __ movl(reg, Address(method_reg,
3526 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003527 // temp = temp[index_in_cache]
3528 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3529 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3530 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003531 }
Vladimir Marko58155012015-08-19 12:49:41 +00003532 }
3533
3534 switch (invoke->GetCodePtrLocation()) {
3535 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3536 __ call(GetFrameEntryLabel());
3537 break;
3538 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3539 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3540 Label* label = &relative_call_patches_.back().label;
3541 __ call(label); // Bind to the patch label, override at link time.
3542 __ Bind(label); // Bind the label at the end of the "call" insn.
3543 break;
3544 }
3545 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3546 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3547 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3548 // (Though the direct CALL ptr16:32 is available for consideration).
3549 FALLTHROUGH_INTENDED;
3550 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3551 // (callee_method + offset_of_quick_compiled_code)()
3552 __ call(Address(callee_method.AsRegister<Register>(),
3553 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3554 kX86WordSize).Int32Value()));
3555 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003556 }
3557
3558 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003559}
3560
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003561void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3562 Register temp = temp_in.AsRegister<Register>();
3563 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3564 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
3565 LocationSummary* locations = invoke->GetLocations();
3566 Location receiver = locations->InAt(0);
3567 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3568 // temp = object->GetClass();
3569 DCHECK(receiver.IsRegister());
3570 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
3571 MaybeRecordImplicitNullCheck(invoke);
3572 __ MaybeUnpoisonHeapReference(temp);
3573 // temp = temp->GetMethodAt(method_offset);
3574 __ movl(temp, Address(temp, method_offset));
3575 // call temp->GetEntryPoint();
3576 __ call(Address(
3577 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3578}
3579
Vladimir Marko58155012015-08-19 12:49:41 +00003580void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3581 DCHECK(linker_patches->empty());
3582 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3583 for (const MethodPatchInfo<Label>& info : method_patches_) {
3584 // The label points to the end of the "movl" insn but the literal offset for method
3585 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3586 uint32_t literal_offset = info.label.Position() - 4;
3587 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3588 info.target_method.dex_file,
3589 info.target_method.dex_method_index));
3590 }
3591 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3592 // The label points to the end of the "call" insn but the literal offset for method
3593 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3594 uint32_t literal_offset = info.label.Position() - 4;
3595 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3596 info.target_method.dex_file,
3597 info.target_method.dex_method_index));
3598 }
3599}
3600
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003601void CodeGeneratorX86::MarkGCCard(Register temp,
3602 Register card,
3603 Register object,
3604 Register value,
3605 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003606 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003607 if (value_can_be_null) {
3608 __ testl(value, value);
3609 __ j(kEqual, &is_null);
3610 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003611 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3612 __ movl(temp, object);
3613 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003614 __ movb(Address(temp, card, TIMES_1, 0),
3615 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003616 if (value_can_be_null) {
3617 __ Bind(&is_null);
3618 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003619}
3620
Calin Juravle52c48962014-12-16 17:02:57 +00003621void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3622 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003623 LocationSummary* locations =
3624 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003625 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003626
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003627 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3628 locations->SetOut(Location::RequiresFpuRegister());
3629 } else {
3630 // The output overlaps in case of long: we don't want the low move to overwrite
3631 // the object's location.
3632 locations->SetOut(Location::RequiresRegister(),
3633 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3634 : Location::kNoOutputOverlap);
3635 }
Calin Juravle52c48962014-12-16 17:02:57 +00003636
3637 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3638 // Long values can be loaded atomically into an XMM using movsd.
3639 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3640 // and then copy the XMM into the output 32bits at a time).
3641 locations->AddTemp(Location::RequiresFpuRegister());
3642 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003643}
3644
Calin Juravle52c48962014-12-16 17:02:57 +00003645void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3646 const FieldInfo& field_info) {
3647 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003648
Calin Juravle52c48962014-12-16 17:02:57 +00003649 LocationSummary* locations = instruction->GetLocations();
3650 Register base = locations->InAt(0).AsRegister<Register>();
3651 Location out = locations->Out();
3652 bool is_volatile = field_info.IsVolatile();
3653 Primitive::Type field_type = field_info.GetFieldType();
3654 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3655
3656 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003657 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003658 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003659 break;
3660 }
3661
3662 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003663 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003664 break;
3665 }
3666
3667 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003668 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003669 break;
3670 }
3671
3672 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003673 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003674 break;
3675 }
3676
3677 case Primitive::kPrimInt:
3678 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003679 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003680 break;
3681 }
3682
3683 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003684 if (is_volatile) {
3685 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3686 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003687 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003688 __ movd(out.AsRegisterPairLow<Register>(), temp);
3689 __ psrlq(temp, Immediate(32));
3690 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3691 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003692 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003693 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003694 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003695 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3696 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003697 break;
3698 }
3699
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003700 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003701 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003702 break;
3703 }
3704
3705 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003706 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003707 break;
3708 }
3709
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003710 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003711 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003712 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003713 }
Calin Juravle52c48962014-12-16 17:02:57 +00003714
Calin Juravle77520bc2015-01-12 18:45:46 +00003715 // Longs are handled in the switch.
3716 if (field_type != Primitive::kPrimLong) {
3717 codegen_->MaybeRecordImplicitNullCheck(instruction);
3718 }
3719
Calin Juravle52c48962014-12-16 17:02:57 +00003720 if (is_volatile) {
3721 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3722 }
Roland Levillain4d027112015-07-01 15:41:14 +01003723
3724 if (field_type == Primitive::kPrimNot) {
3725 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3726 }
Calin Juravle52c48962014-12-16 17:02:57 +00003727}
3728
3729void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3730 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3731
3732 LocationSummary* locations =
3733 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3734 locations->SetInAt(0, Location::RequiresRegister());
3735 bool is_volatile = field_info.IsVolatile();
3736 Primitive::Type field_type = field_info.GetFieldType();
3737 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3738 || (field_type == Primitive::kPrimByte);
3739
3740 // The register allocator does not support multiple
3741 // inputs that die at entry with one in a specific register.
3742 if (is_byte_type) {
3743 // Ensure the value is in a byte register.
3744 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003745 } else if (Primitive::IsFloatingPointType(field_type)) {
3746 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003747 } else {
3748 locations->SetInAt(1, Location::RequiresRegister());
3749 }
Calin Juravle52c48962014-12-16 17:02:57 +00003750 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003751 // Temporary registers for the write barrier.
3752 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003753 // Ensure the card is in a byte register.
3754 locations->AddTemp(Location::RegisterLocation(ECX));
3755 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3756 // 64bits value can be atomically written to an address with movsd and an XMM register.
3757 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3758 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3759 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3760 // isolated cases when we need this it isn't worth adding the extra complexity.
3761 locations->AddTemp(Location::RequiresFpuRegister());
3762 locations->AddTemp(Location::RequiresFpuRegister());
3763 }
3764}
3765
3766void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003767 const FieldInfo& field_info,
3768 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003769 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3770
3771 LocationSummary* locations = instruction->GetLocations();
3772 Register base = locations->InAt(0).AsRegister<Register>();
3773 Location value = locations->InAt(1);
3774 bool is_volatile = field_info.IsVolatile();
3775 Primitive::Type field_type = field_info.GetFieldType();
3776 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003777 bool needs_write_barrier =
3778 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003779
3780 if (is_volatile) {
3781 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3782 }
3783
3784 switch (field_type) {
3785 case Primitive::kPrimBoolean:
3786 case Primitive::kPrimByte: {
3787 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3788 break;
3789 }
3790
3791 case Primitive::kPrimShort:
3792 case Primitive::kPrimChar: {
3793 __ movw(Address(base, offset), value.AsRegister<Register>());
3794 break;
3795 }
3796
3797 case Primitive::kPrimInt:
3798 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003799 if (kPoisonHeapReferences && needs_write_barrier) {
3800 // Note that in the case where `value` is a null reference,
3801 // we do not enter this block, as the reference does not
3802 // need poisoning.
3803 DCHECK_EQ(field_type, Primitive::kPrimNot);
3804 Register temp = locations->GetTemp(0).AsRegister<Register>();
3805 __ movl(temp, value.AsRegister<Register>());
3806 __ PoisonHeapReference(temp);
3807 __ movl(Address(base, offset), temp);
3808 } else {
3809 __ movl(Address(base, offset), value.AsRegister<Register>());
3810 }
Calin Juravle52c48962014-12-16 17:02:57 +00003811 break;
3812 }
3813
3814 case Primitive::kPrimLong: {
3815 if (is_volatile) {
3816 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3817 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3818 __ movd(temp1, value.AsRegisterPairLow<Register>());
3819 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3820 __ punpckldq(temp1, temp2);
3821 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003822 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003823 } else {
3824 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003825 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003826 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3827 }
3828 break;
3829 }
3830
3831 case Primitive::kPrimFloat: {
3832 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3833 break;
3834 }
3835
3836 case Primitive::kPrimDouble: {
3837 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3838 break;
3839 }
3840
3841 case Primitive::kPrimVoid:
3842 LOG(FATAL) << "Unreachable type " << field_type;
3843 UNREACHABLE();
3844 }
3845
Calin Juravle77520bc2015-01-12 18:45:46 +00003846 // Longs are handled in the switch.
3847 if (field_type != Primitive::kPrimLong) {
3848 codegen_->MaybeRecordImplicitNullCheck(instruction);
3849 }
3850
Roland Levillain4d027112015-07-01 15:41:14 +01003851 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003852 Register temp = locations->GetTemp(0).AsRegister<Register>();
3853 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003854 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003855 }
3856
Calin Juravle52c48962014-12-16 17:02:57 +00003857 if (is_volatile) {
3858 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3859 }
3860}
3861
3862void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3863 HandleFieldGet(instruction, instruction->GetFieldInfo());
3864}
3865
3866void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3867 HandleFieldGet(instruction, instruction->GetFieldInfo());
3868}
3869
3870void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3871 HandleFieldSet(instruction, instruction->GetFieldInfo());
3872}
3873
3874void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003875 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003876}
3877
3878void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3879 HandleFieldSet(instruction, instruction->GetFieldInfo());
3880}
3881
3882void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003883 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003884}
3885
3886void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3887 HandleFieldGet(instruction, instruction->GetFieldInfo());
3888}
3889
3890void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3891 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003892}
3893
3894void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003895 LocationSummary* locations =
3896 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003897 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3898 ? Location::RequiresRegister()
3899 : Location::Any();
3900 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003901 if (instruction->HasUses()) {
3902 locations->SetOut(Location::SameAsFirstInput());
3903 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003904}
3905
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003906void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003907 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3908 return;
3909 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003910 LocationSummary* locations = instruction->GetLocations();
3911 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003912
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003913 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3914 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3915}
3916
3917void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003918 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003919 codegen_->AddSlowPath(slow_path);
3920
3921 LocationSummary* locations = instruction->GetLocations();
3922 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003923
3924 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003925 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003926 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003927 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003928 } else {
3929 DCHECK(obj.IsConstant()) << obj;
3930 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3931 __ jmp(slow_path->GetEntryLabel());
3932 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003933 }
3934 __ j(kEqual, slow_path->GetEntryLabel());
3935}
3936
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003937void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3938 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3939 GenerateImplicitNullCheck(instruction);
3940 } else {
3941 GenerateExplicitNullCheck(instruction);
3942 }
3943}
3944
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003945void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003946 LocationSummary* locations =
3947 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003948 locations->SetInAt(0, Location::RequiresRegister());
3949 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003950 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3951 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3952 } else {
3953 // The output overlaps in case of long: we don't want the low move to overwrite
3954 // the array's location.
3955 locations->SetOut(Location::RequiresRegister(),
3956 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3957 : Location::kNoOutputOverlap);
3958 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003959}
3960
3961void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3962 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003963 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003964 Location index = locations->InAt(1);
3965
Calin Juravle77520bc2015-01-12 18:45:46 +00003966 Primitive::Type type = instruction->GetType();
3967 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003968 case Primitive::kPrimBoolean: {
3969 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003970 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003971 if (index.IsConstant()) {
3972 __ movzxb(out, Address(obj,
3973 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3974 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003975 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003976 }
3977 break;
3978 }
3979
3980 case Primitive::kPrimByte: {
3981 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003982 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003983 if (index.IsConstant()) {
3984 __ movsxb(out, Address(obj,
3985 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3986 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003987 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003988 }
3989 break;
3990 }
3991
3992 case Primitive::kPrimShort: {
3993 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003994 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003995 if (index.IsConstant()) {
3996 __ movsxw(out, Address(obj,
3997 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3998 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003999 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004000 }
4001 break;
4002 }
4003
4004 case Primitive::kPrimChar: {
4005 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004006 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004007 if (index.IsConstant()) {
4008 __ movzxw(out, Address(obj,
4009 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4010 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004011 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004012 }
4013 break;
4014 }
4015
4016 case Primitive::kPrimInt:
4017 case Primitive::kPrimNot: {
4018 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004019 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004020 if (index.IsConstant()) {
4021 __ movl(out, Address(obj,
4022 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4023 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004024 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004025 }
4026 break;
4027 }
4028
4029 case Primitive::kPrimLong: {
4030 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004031 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004032 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004033 if (index.IsConstant()) {
4034 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004035 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004036 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004037 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004038 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004039 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004040 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004041 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004042 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004043 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004044 }
4045 break;
4046 }
4047
Mark Mendell7c8d0092015-01-26 11:21:33 -05004048 case Primitive::kPrimFloat: {
4049 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4050 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4051 if (index.IsConstant()) {
4052 __ movss(out, Address(obj,
4053 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4054 } else {
4055 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4056 }
4057 break;
4058 }
4059
4060 case Primitive::kPrimDouble: {
4061 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4062 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4063 if (index.IsConstant()) {
4064 __ movsd(out, Address(obj,
4065 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4066 } else {
4067 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4068 }
4069 break;
4070 }
4071
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004072 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004073 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004074 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004075 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004076
4077 if (type != Primitive::kPrimLong) {
4078 codegen_->MaybeRecordImplicitNullCheck(instruction);
4079 }
Roland Levillain4d027112015-07-01 15:41:14 +01004080
4081 if (type == Primitive::kPrimNot) {
4082 Register out = locations->Out().AsRegister<Register>();
4083 __ MaybeUnpoisonHeapReference(out);
4084 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004085}
4086
4087void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004088 // This location builder might end up asking to up to four registers, which is
4089 // not currently possible for baseline. The situation in which we need four
4090 // registers cannot be met by baseline though, because it has not run any
4091 // optimization.
4092
Nicolas Geoffray39468442014-09-02 15:17:15 +01004093 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004094 bool needs_write_barrier =
4095 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4096
Mark Mendell5f874182015-03-04 15:42:45 -05004097 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004098
Nicolas Geoffray39468442014-09-02 15:17:15 +01004099 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4100 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004101 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004102
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004103 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004104 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004105 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4106 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4107 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004108 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004109 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4110 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004111 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004112 // In case of a byte operation, the register allocator does not support multiple
4113 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004114 locations->SetInAt(0, Location::RequiresRegister());
4115 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004116 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004117 // Ensure the value is in a byte register.
4118 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004119 } else if (Primitive::IsFloatingPointType(value_type)) {
4120 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004121 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004122 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004123 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004124 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004125 // Temporary registers for the write barrier.
4126 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004127 // Ensure the card is in a byte register.
4128 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004129 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004130 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004131}
4132
4133void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4134 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004135 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004136 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004137 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004138 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004139 bool needs_runtime_call = locations->WillCall();
4140 bool needs_write_barrier =
4141 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004142
4143 switch (value_type) {
4144 case Primitive::kPrimBoolean:
4145 case Primitive::kPrimByte: {
4146 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004147 if (index.IsConstant()) {
4148 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004149 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004150 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004151 } else {
4152 __ movb(Address(obj, offset),
4153 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4154 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004155 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004156 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004157 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004158 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004159 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004160 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004161 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4162 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004163 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004164 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004165 break;
4166 }
4167
4168 case Primitive::kPrimShort:
4169 case Primitive::kPrimChar: {
4170 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004171 if (index.IsConstant()) {
4172 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004173 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004174 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004175 } else {
4176 __ movw(Address(obj, offset),
4177 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4178 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004179 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004180 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004181 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4182 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004183 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004184 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004185 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4186 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004187 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004188 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004189 break;
4190 }
4191
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004192 case Primitive::kPrimInt:
4193 case Primitive::kPrimNot: {
4194 if (!needs_runtime_call) {
4195 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4196 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004197 size_t offset =
4198 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004199 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004200 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4201 Register temp = locations->GetTemp(0).AsRegister<Register>();
4202 __ movl(temp, value.AsRegister<Register>());
4203 __ PoisonHeapReference(temp);
4204 __ movl(Address(obj, offset), temp);
4205 } else {
4206 __ movl(Address(obj, offset), value.AsRegister<Register>());
4207 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004208 } else {
4209 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004210 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4211 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4212 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4213 // Note: if heap poisoning is enabled, no need to poison
4214 // (negate) `v` if it is a reference, as it would be null.
4215 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004216 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004217 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004218 DCHECK(index.IsRegister()) << index;
4219 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004220 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4221 Register temp = locations->GetTemp(0).AsRegister<Register>();
4222 __ movl(temp, value.AsRegister<Register>());
4223 __ PoisonHeapReference(temp);
4224 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4225 } else {
4226 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4227 value.AsRegister<Register>());
4228 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004229 } else {
4230 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004231 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4232 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4233 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4234 // Note: if heap poisoning is enabled, no need to poison
4235 // (negate) `v` if it is a reference, as it would be null.
4236 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004237 }
4238 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004239 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004240
4241 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004242 Register temp = locations->GetTemp(0).AsRegister<Register>();
4243 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004244 codegen_->MarkGCCard(
4245 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004246 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004247 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004248 DCHECK_EQ(value_type, Primitive::kPrimNot);
4249 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004250 // Note: if heap poisoning is enabled, pAputObject takes cares
4251 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004252 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4253 instruction,
4254 instruction->GetDexPc(),
4255 nullptr);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004256 }
4257 break;
4258 }
4259
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004260 case Primitive::kPrimLong: {
4261 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004262 if (index.IsConstant()) {
4263 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004264 if (value.IsRegisterPair()) {
4265 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004266 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004267 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004268 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004269 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004270 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4271 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004272 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004273 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4274 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004275 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004276 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004277 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004278 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004279 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004280 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004281 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004282 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004283 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004284 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004285 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004286 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004287 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004288 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004289 Immediate(High32Bits(val)));
4290 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004291 }
4292 break;
4293 }
4294
Mark Mendell7c8d0092015-01-26 11:21:33 -05004295 case Primitive::kPrimFloat: {
4296 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4297 DCHECK(value.IsFpuRegister());
4298 if (index.IsConstant()) {
4299 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4300 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4301 } else {
4302 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4303 value.AsFpuRegister<XmmRegister>());
4304 }
4305 break;
4306 }
4307
4308 case Primitive::kPrimDouble: {
4309 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4310 DCHECK(value.IsFpuRegister());
4311 if (index.IsConstant()) {
4312 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4313 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4314 } else {
4315 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4316 value.AsFpuRegister<XmmRegister>());
4317 }
4318 break;
4319 }
4320
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004321 case Primitive::kPrimVoid:
4322 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004323 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004324 }
4325}
4326
4327void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4328 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004329 locations->SetInAt(0, Location::RequiresRegister());
4330 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004331}
4332
4333void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4334 LocationSummary* locations = instruction->GetLocations();
4335 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004336 Register obj = locations->InAt(0).AsRegister<Register>();
4337 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004338 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004339 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004340}
4341
4342void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004343 LocationSummary* locations =
4344 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004345 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004346 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004347 if (instruction->HasUses()) {
4348 locations->SetOut(Location::SameAsFirstInput());
4349 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004350}
4351
4352void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4353 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004354 Location index_loc = locations->InAt(0);
4355 Location length_loc = locations->InAt(1);
4356 SlowPathCodeX86* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004357 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004358
Mark Mendell99dbd682015-04-22 16:18:52 -04004359 if (length_loc.IsConstant()) {
4360 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4361 if (index_loc.IsConstant()) {
4362 // BCE will remove the bounds check if we are guarenteed to pass.
4363 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4364 if (index < 0 || index >= length) {
4365 codegen_->AddSlowPath(slow_path);
4366 __ jmp(slow_path->GetEntryLabel());
4367 } else {
4368 // Some optimization after BCE may have generated this, and we should not
4369 // generate a bounds check if it is a valid range.
4370 }
4371 return;
4372 }
4373
4374 // We have to reverse the jump condition because the length is the constant.
4375 Register index_reg = index_loc.AsRegister<Register>();
4376 __ cmpl(index_reg, Immediate(length));
4377 codegen_->AddSlowPath(slow_path);
4378 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004379 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004380 Register length = length_loc.AsRegister<Register>();
4381 if (index_loc.IsConstant()) {
4382 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4383 __ cmpl(length, Immediate(value));
4384 } else {
4385 __ cmpl(length, index_loc.AsRegister<Register>());
4386 }
4387 codegen_->AddSlowPath(slow_path);
4388 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004389 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004390}
4391
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004392void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4393 temp->SetLocations(nullptr);
4394}
4395
4396void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4397 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004398 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004399}
4400
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004401void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004402 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004403 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004404}
4405
4406void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004407 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4408}
4409
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004410void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4411 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4412}
4413
4414void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004415 HBasicBlock* block = instruction->GetBlock();
4416 if (block->GetLoopInformation() != nullptr) {
4417 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4418 // The back edge will generate the suspend check.
4419 return;
4420 }
4421 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4422 // The goto will generate the suspend check.
4423 return;
4424 }
4425 GenerateSuspendCheck(instruction, nullptr);
4426}
4427
4428void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4429 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004430 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004431 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4432 if (slow_path == nullptr) {
4433 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4434 instruction->SetSlowPath(slow_path);
4435 codegen_->AddSlowPath(slow_path);
4436 if (successor != nullptr) {
4437 DCHECK(successor->IsLoopHeader());
4438 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4439 }
4440 } else {
4441 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4442 }
4443
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004444 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004445 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004446 if (successor == nullptr) {
4447 __ j(kNotEqual, slow_path->GetEntryLabel());
4448 __ Bind(slow_path->GetReturnLabel());
4449 } else {
4450 __ j(kEqual, codegen_->GetLabelOf(successor));
4451 __ jmp(slow_path->GetEntryLabel());
4452 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004453}
4454
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004455X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4456 return codegen_->GetAssembler();
4457}
4458
Mark Mendell7c8d0092015-01-26 11:21:33 -05004459void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004460 ScratchRegisterScope ensure_scratch(
4461 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4462 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4463 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4464 __ movl(temp_reg, Address(ESP, src + stack_offset));
4465 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004466}
4467
4468void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004469 ScratchRegisterScope ensure_scratch(
4470 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4471 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4472 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4473 __ movl(temp_reg, Address(ESP, src + stack_offset));
4474 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4475 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4476 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004477}
4478
4479void ParallelMoveResolverX86::EmitMove(size_t index) {
4480 MoveOperands* move = moves_.Get(index);
4481 Location source = move->GetSource();
4482 Location destination = move->GetDestination();
4483
4484 if (source.IsRegister()) {
4485 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004486 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004487 } else {
4488 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004489 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004490 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004491 } else if (source.IsFpuRegister()) {
4492 if (destination.IsFpuRegister()) {
4493 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4494 } else if (destination.IsStackSlot()) {
4495 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4496 } else {
4497 DCHECK(destination.IsDoubleStackSlot());
4498 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4499 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004500 } else if (source.IsStackSlot()) {
4501 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004502 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004503 } else if (destination.IsFpuRegister()) {
4504 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004505 } else {
4506 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004507 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4508 }
4509 } else if (source.IsDoubleStackSlot()) {
4510 if (destination.IsFpuRegister()) {
4511 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4512 } else {
4513 DCHECK(destination.IsDoubleStackSlot()) << destination;
4514 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004515 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004516 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004517 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004518 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004519 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004520 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004521 if (value == 0) {
4522 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4523 } else {
4524 __ movl(destination.AsRegister<Register>(), Immediate(value));
4525 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004526 } else {
4527 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004528 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004529 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004530 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004531 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004532 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004533 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004534 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004535 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4536 if (value == 0) {
4537 // Easy handling of 0.0.
4538 __ xorps(dest, dest);
4539 } else {
4540 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004541 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4542 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4543 __ movl(temp, Immediate(value));
4544 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004545 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004546 } else {
4547 DCHECK(destination.IsStackSlot()) << destination;
4548 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4549 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004550 } else if (constant->IsLongConstant()) {
4551 int64_t value = constant->AsLongConstant()->GetValue();
4552 int32_t low_value = Low32Bits(value);
4553 int32_t high_value = High32Bits(value);
4554 Immediate low(low_value);
4555 Immediate high(high_value);
4556 if (destination.IsDoubleStackSlot()) {
4557 __ movl(Address(ESP, destination.GetStackIndex()), low);
4558 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4559 } else {
4560 __ movl(destination.AsRegisterPairLow<Register>(), low);
4561 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4562 }
4563 } else {
4564 DCHECK(constant->IsDoubleConstant());
4565 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004566 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004567 int32_t low_value = Low32Bits(value);
4568 int32_t high_value = High32Bits(value);
4569 Immediate low(low_value);
4570 Immediate high(high_value);
4571 if (destination.IsFpuRegister()) {
4572 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4573 if (value == 0) {
4574 // Easy handling of 0.0.
4575 __ xorpd(dest, dest);
4576 } else {
4577 __ pushl(high);
4578 __ pushl(low);
4579 __ movsd(dest, Address(ESP, 0));
4580 __ addl(ESP, Immediate(8));
4581 }
4582 } else {
4583 DCHECK(destination.IsDoubleStackSlot()) << destination;
4584 __ movl(Address(ESP, destination.GetStackIndex()), low);
4585 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4586 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004587 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004588 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004589 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004590 }
4591}
4592
Mark Mendella5c19ce2015-04-01 12:51:05 -04004593void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004594 Register suggested_scratch = reg == EAX ? EBX : EAX;
4595 ScratchRegisterScope ensure_scratch(
4596 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4597
4598 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4599 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4600 __ movl(Address(ESP, mem + stack_offset), reg);
4601 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004602}
4603
Mark Mendell7c8d0092015-01-26 11:21:33 -05004604void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004605 ScratchRegisterScope ensure_scratch(
4606 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4607
4608 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4609 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4610 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4611 __ movss(Address(ESP, mem + stack_offset), reg);
4612 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004613}
4614
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004615void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004616 ScratchRegisterScope ensure_scratch1(
4617 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004618
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004619 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4620 ScratchRegisterScope ensure_scratch2(
4621 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004622
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004623 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4624 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4625 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4626 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4627 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4628 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004629}
4630
4631void ParallelMoveResolverX86::EmitSwap(size_t index) {
4632 MoveOperands* move = moves_.Get(index);
4633 Location source = move->GetSource();
4634 Location destination = move->GetDestination();
4635
4636 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004637 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4638 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4639 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4640 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4641 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004642 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004643 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004644 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004645 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004646 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4647 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004648 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4649 // Use XOR Swap algorithm to avoid a temporary.
4650 DCHECK_NE(source.reg(), destination.reg());
4651 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4652 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4653 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4654 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4655 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4656 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4657 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004658 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4659 // Take advantage of the 16 bytes in the XMM register.
4660 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4661 Address stack(ESP, destination.GetStackIndex());
4662 // Load the double into the high doubleword.
4663 __ movhpd(reg, stack);
4664
4665 // Store the low double into the destination.
4666 __ movsd(stack, reg);
4667
4668 // Move the high double to the low double.
4669 __ psrldq(reg, Immediate(8));
4670 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4671 // Take advantage of the 16 bytes in the XMM register.
4672 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4673 Address stack(ESP, source.GetStackIndex());
4674 // Load the double into the high doubleword.
4675 __ movhpd(reg, stack);
4676
4677 // Store the low double into the destination.
4678 __ movsd(stack, reg);
4679
4680 // Move the high double to the low double.
4681 __ psrldq(reg, Immediate(8));
4682 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4683 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4684 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004685 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004686 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004687 }
4688}
4689
4690void ParallelMoveResolverX86::SpillScratch(int reg) {
4691 __ pushl(static_cast<Register>(reg));
4692}
4693
4694void ParallelMoveResolverX86::RestoreScratch(int reg) {
4695 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004696}
4697
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004698void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004699 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4700 ? LocationSummary::kCallOnSlowPath
4701 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004702 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004703 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004704 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004705 locations->SetOut(Location::RequiresRegister());
4706}
4707
4708void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004709 LocationSummary* locations = cls->GetLocations();
4710 Register out = locations->Out().AsRegister<Register>();
4711 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004712 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004713 DCHECK(!cls->CanCallRuntime());
4714 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004715 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004716 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004717 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004718 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01004719 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004720 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004721 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004722
4723 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4724 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4725 codegen_->AddSlowPath(slow_path);
4726 __ testl(out, out);
4727 __ j(kEqual, slow_path->GetEntryLabel());
4728 if (cls->MustGenerateClinitCheck()) {
4729 GenerateClassInitializationCheck(slow_path, out);
4730 } else {
4731 __ Bind(slow_path->GetExitLabel());
4732 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004733 }
4734}
4735
4736void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4737 LocationSummary* locations =
4738 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4739 locations->SetInAt(0, Location::RequiresRegister());
4740 if (check->HasUses()) {
4741 locations->SetOut(Location::SameAsFirstInput());
4742 }
4743}
4744
4745void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004746 // We assume the class to not be null.
4747 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4748 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004749 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004750 GenerateClassInitializationCheck(slow_path,
4751 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004752}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004753
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004754void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4755 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004756 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4757 Immediate(mirror::Class::kStatusInitialized));
4758 __ j(kLess, slow_path->GetEntryLabel());
4759 __ Bind(slow_path->GetExitLabel());
4760 // No need for memory fence, thanks to the X86 memory model.
4761}
4762
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004763void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4764 LocationSummary* locations =
4765 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004766 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004767 locations->SetOut(Location::RequiresRegister());
4768}
4769
4770void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4771 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4772 codegen_->AddSlowPath(slow_path);
4773
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004774 LocationSummary* locations = load->GetLocations();
4775 Register out = locations->Out().AsRegister<Register>();
4776 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004777 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004778 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004779 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004780 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004781 __ testl(out, out);
4782 __ j(kEqual, slow_path->GetEntryLabel());
4783 __ Bind(slow_path->GetExitLabel());
4784}
4785
David Brazdilcb1c0552015-08-04 16:22:25 +01004786static Address GetExceptionTlsAddress() {
4787 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
4788}
4789
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004790void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4791 LocationSummary* locations =
4792 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4793 locations->SetOut(Location::RequiresRegister());
4794}
4795
4796void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004797 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
4798}
4799
4800void LocationsBuilderX86::VisitClearException(HClearException* clear) {
4801 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4802}
4803
4804void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4805 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004806}
4807
4808void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4809 LocationSummary* locations =
4810 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4811 InvokeRuntimeCallingConvention calling_convention;
4812 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4813}
4814
4815void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004816 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4817 instruction,
4818 instruction->GetDexPc(),
4819 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004820}
4821
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004822void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004823 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4824 ? LocationSummary::kNoCall
4825 : LocationSummary::kCallOnSlowPath;
4826 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4827 locations->SetInAt(0, Location::RequiresRegister());
4828 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004829 // Note that TypeCheckSlowPathX86 uses this register too.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004830 locations->SetOut(Location::RequiresRegister());
4831}
4832
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004833void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004834 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004835 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004836 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004837 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004838 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4839 Label done, zero;
4840 SlowPathCodeX86* slow_path = nullptr;
4841
4842 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004843 // Avoid null check if we know obj is not null.
4844 if (instruction->MustDoNullCheck()) {
4845 __ testl(obj, obj);
4846 __ j(kEqual, &zero);
4847 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004848 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004849 __ movl(out, Address(obj, class_offset));
4850 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004851 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004852 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004853 } else {
4854 DCHECK(cls.IsStackSlot()) << cls;
4855 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4856 }
4857
4858 if (instruction->IsClassFinal()) {
4859 // Classes must be equal for the instanceof to succeed.
4860 __ j(kNotEqual, &zero);
4861 __ movl(out, Immediate(1));
4862 __ jmp(&done);
4863 } else {
4864 // If the classes are not equal, we go into a slow path.
4865 DCHECK(locations->OnlyCallsOnSlowPath());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004866 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004867 codegen_->AddSlowPath(slow_path);
4868 __ j(kNotEqual, slow_path->GetEntryLabel());
4869 __ movl(out, Immediate(1));
4870 __ jmp(&done);
4871 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004872
4873 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4874 __ Bind(&zero);
4875 __ movl(out, Immediate(0));
4876 }
4877
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004878 if (slow_path != nullptr) {
4879 __ Bind(slow_path->GetExitLabel());
4880 }
4881 __ Bind(&done);
4882}
4883
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004884void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4885 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4886 instruction, LocationSummary::kCallOnSlowPath);
4887 locations->SetInAt(0, Location::RequiresRegister());
4888 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004889 // Note that TypeCheckSlowPathX86 uses this register too.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004890 locations->AddTemp(Location::RequiresRegister());
4891}
4892
4893void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4894 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004895 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004896 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004897 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004898 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004899 SlowPathCodeX86* slow_path =
4900 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004901 codegen_->AddSlowPath(slow_path);
4902
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004903 // Avoid null check if we know obj is not null.
4904 if (instruction->MustDoNullCheck()) {
4905 __ testl(obj, obj);
4906 __ j(kEqual, slow_path->GetExitLabel());
4907 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004908 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004909 __ movl(temp, Address(obj, class_offset));
4910 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004911 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004912 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004913 } else {
4914 DCHECK(cls.IsStackSlot()) << cls;
4915 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4916 }
Roland Levillain4d027112015-07-01 15:41:14 +01004917 // The checkcast succeeds if the classes are equal (fast path).
4918 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004919 __ j(kNotEqual, slow_path->GetEntryLabel());
4920 __ Bind(slow_path->GetExitLabel());
4921}
4922
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004923void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4924 LocationSummary* locations =
4925 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4926 InvokeRuntimeCallingConvention calling_convention;
4927 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4928}
4929
4930void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004931 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4932 : QUICK_ENTRY_POINT(pUnlockObject),
4933 instruction,
4934 instruction->GetDexPc(),
4935 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004936}
4937
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004938void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4939void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4940void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4941
4942void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4943 LocationSummary* locations =
4944 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4945 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4946 || instruction->GetResultType() == Primitive::kPrimLong);
4947 locations->SetInAt(0, Location::RequiresRegister());
4948 locations->SetInAt(1, Location::Any());
4949 locations->SetOut(Location::SameAsFirstInput());
4950}
4951
4952void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4953 HandleBitwiseOperation(instruction);
4954}
4955
4956void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4957 HandleBitwiseOperation(instruction);
4958}
4959
4960void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4961 HandleBitwiseOperation(instruction);
4962}
4963
4964void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4965 LocationSummary* locations = instruction->GetLocations();
4966 Location first = locations->InAt(0);
4967 Location second = locations->InAt(1);
4968 DCHECK(first.Equals(locations->Out()));
4969
4970 if (instruction->GetResultType() == Primitive::kPrimInt) {
4971 if (second.IsRegister()) {
4972 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004973 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004974 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004975 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004976 } else {
4977 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004978 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004979 }
4980 } else if (second.IsConstant()) {
4981 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004982 __ andl(first.AsRegister<Register>(),
4983 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004984 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004985 __ orl(first.AsRegister<Register>(),
4986 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004987 } else {
4988 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004989 __ xorl(first.AsRegister<Register>(),
4990 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004991 }
4992 } else {
4993 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004994 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004995 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004996 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004997 } else {
4998 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004999 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005000 }
5001 }
5002 } else {
5003 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5004 if (second.IsRegisterPair()) {
5005 if (instruction->IsAnd()) {
5006 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5007 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5008 } else if (instruction->IsOr()) {
5009 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5010 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5011 } else {
5012 DCHECK(instruction->IsXor());
5013 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5014 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5015 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005016 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005017 if (instruction->IsAnd()) {
5018 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5019 __ andl(first.AsRegisterPairHigh<Register>(),
5020 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5021 } else if (instruction->IsOr()) {
5022 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5023 __ orl(first.AsRegisterPairHigh<Register>(),
5024 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5025 } else {
5026 DCHECK(instruction->IsXor());
5027 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5028 __ xorl(first.AsRegisterPairHigh<Register>(),
5029 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5030 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005031 } else {
5032 DCHECK(second.IsConstant()) << second;
5033 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005034 int32_t low_value = Low32Bits(value);
5035 int32_t high_value = High32Bits(value);
5036 Immediate low(low_value);
5037 Immediate high(high_value);
5038 Register first_low = first.AsRegisterPairLow<Register>();
5039 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005040 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005041 if (low_value == 0) {
5042 __ xorl(first_low, first_low);
5043 } else if (low_value != -1) {
5044 __ andl(first_low, low);
5045 }
5046 if (high_value == 0) {
5047 __ xorl(first_high, first_high);
5048 } else if (high_value != -1) {
5049 __ andl(first_high, high);
5050 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005051 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005052 if (low_value != 0) {
5053 __ orl(first_low, low);
5054 }
5055 if (high_value != 0) {
5056 __ orl(first_high, high);
5057 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005058 } else {
5059 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005060 if (low_value != 0) {
5061 __ xorl(first_low, low);
5062 }
5063 if (high_value != 0) {
5064 __ xorl(first_high, high);
5065 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005066 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005067 }
5068 }
5069}
5070
Calin Juravleb1498f62015-02-16 13:13:29 +00005071void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5072 // Nothing to do, this should be removed during prepare for register allocator.
5073 UNUSED(instruction);
5074 LOG(FATAL) << "Unreachable";
5075}
5076
5077void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5078 // Nothing to do, this should be removed during prepare for register allocator.
5079 UNUSED(instruction);
5080 LOG(FATAL) << "Unreachable";
5081}
5082
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005083void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5084 DCHECK(codegen_->IsBaseline());
5085 LocationSummary* locations =
5086 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5087 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5088}
5089
5090void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5091 DCHECK(codegen_->IsBaseline());
5092 // Will be generated at use site.
5093}
5094
Roland Levillain4d027112015-07-01 15:41:14 +01005095#undef __
5096
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005097} // namespace x86
5098} // namespace art