blob: f48395b1e1b61290c1d830b54d754dc125c30357 [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) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001551 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001552 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1553 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001554 LocationSummary* locations = invoke->GetLocations();
1555 Location receiver = locations->InAt(0);
1556 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001557 // temp = object->GetClass();
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001558 DCHECK(receiver.IsRegister());
1559 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001560 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001561 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001562 // temp = temp->GetMethodAt(method_offset);
1563 __ movl(temp, Address(temp, method_offset));
1564 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001565 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001566 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001567
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001568 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001569 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001570}
1571
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001572void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1573 HandleInvoke(invoke);
1574 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001575 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001576}
1577
1578void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1579 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001580 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001581 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1582 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001583 LocationSummary* locations = invoke->GetLocations();
1584 Location receiver = locations->InAt(0);
1585 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1586
1587 // Set the hidden argument.
1588 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001589 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001590
1591 // temp = object->GetClass();
1592 if (receiver.IsStackSlot()) {
1593 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1594 __ movl(temp, Address(temp, class_offset));
1595 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001596 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001597 }
Roland Levillain4d027112015-07-01 15:41:14 +01001598 codegen_->MaybeRecordImplicitNullCheck(invoke);
1599 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001600 // temp = temp->GetImtEntryAt(method_offset);
1601 __ movl(temp, Address(temp, method_offset));
1602 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001603 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001604 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001605
1606 DCHECK(!codegen_->IsLeafMethod());
1607 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1608}
1609
Roland Levillain88cb1752014-10-20 16:36:47 +01001610void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1611 LocationSummary* locations =
1612 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1613 switch (neg->GetResultType()) {
1614 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001615 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001616 locations->SetInAt(0, Location::RequiresRegister());
1617 locations->SetOut(Location::SameAsFirstInput());
1618 break;
1619
Roland Levillain88cb1752014-10-20 16:36:47 +01001620 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001621 locations->SetInAt(0, Location::RequiresFpuRegister());
1622 locations->SetOut(Location::SameAsFirstInput());
1623 locations->AddTemp(Location::RequiresRegister());
1624 locations->AddTemp(Location::RequiresFpuRegister());
1625 break;
1626
Roland Levillain88cb1752014-10-20 16:36:47 +01001627 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001628 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001629 locations->SetOut(Location::SameAsFirstInput());
1630 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001631 break;
1632
1633 default:
1634 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1635 }
1636}
1637
1638void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1639 LocationSummary* locations = neg->GetLocations();
1640 Location out = locations->Out();
1641 Location in = locations->InAt(0);
1642 switch (neg->GetResultType()) {
1643 case Primitive::kPrimInt:
1644 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001645 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001646 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001647 break;
1648
1649 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001650 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001651 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001652 __ negl(out.AsRegisterPairLow<Register>());
1653 // Negation is similar to subtraction from zero. The least
1654 // significant byte triggers a borrow when it is different from
1655 // zero; to take it into account, add 1 to the most significant
1656 // byte if the carry flag (CF) is set to 1 after the first NEGL
1657 // operation.
1658 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1659 __ negl(out.AsRegisterPairHigh<Register>());
1660 break;
1661
Roland Levillain5368c212014-11-27 15:03:41 +00001662 case Primitive::kPrimFloat: {
1663 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001664 Register constant = locations->GetTemp(0).AsRegister<Register>();
1665 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001666 // Implement float negation with an exclusive or with value
1667 // 0x80000000 (mask for bit 31, representing the sign of a
1668 // single-precision floating-point number).
1669 __ movl(constant, Immediate(INT32_C(0x80000000)));
1670 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001671 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001672 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001673 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001674
Roland Levillain5368c212014-11-27 15:03:41 +00001675 case Primitive::kPrimDouble: {
1676 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001677 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001678 // Implement double negation with an exclusive or with value
1679 // 0x8000000000000000 (mask for bit 63, representing the sign of
1680 // a double-precision floating-point number).
1681 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001682 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001683 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001684 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001685
1686 default:
1687 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1688 }
1689}
1690
Roland Levillaindff1f282014-11-05 14:15:05 +00001691void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001692 Primitive::Type result_type = conversion->GetResultType();
1693 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001694 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001695
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001696 // The float-to-long and double-to-long type conversions rely on a
1697 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001698 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001699 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1700 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001701 ? LocationSummary::kCall
1702 : LocationSummary::kNoCall;
1703 LocationSummary* locations =
1704 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1705
David Brazdilb2bd1c52015-03-25 11:17:37 +00001706 // The Java language does not allow treating boolean as an integral type but
1707 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001708
Roland Levillaindff1f282014-11-05 14:15:05 +00001709 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001710 case Primitive::kPrimByte:
1711 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001712 case Primitive::kPrimBoolean:
1713 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001714 case Primitive::kPrimShort:
1715 case Primitive::kPrimInt:
1716 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001717 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001718 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1719 // Make the output overlap to please the register allocator. This greatly simplifies
1720 // the validation of the linear scan implementation
1721 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001722 break;
1723
1724 default:
1725 LOG(FATAL) << "Unexpected type conversion from " << input_type
1726 << " to " << result_type;
1727 }
1728 break;
1729
Roland Levillain01a8d712014-11-14 16:27:39 +00001730 case Primitive::kPrimShort:
1731 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001732 case Primitive::kPrimBoolean:
1733 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001734 case Primitive::kPrimByte:
1735 case Primitive::kPrimInt:
1736 case Primitive::kPrimChar:
1737 // Processing a Dex `int-to-short' instruction.
1738 locations->SetInAt(0, Location::Any());
1739 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1740 break;
1741
1742 default:
1743 LOG(FATAL) << "Unexpected type conversion from " << input_type
1744 << " to " << result_type;
1745 }
1746 break;
1747
Roland Levillain946e1432014-11-11 17:35:19 +00001748 case Primitive::kPrimInt:
1749 switch (input_type) {
1750 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001751 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001752 locations->SetInAt(0, Location::Any());
1753 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1754 break;
1755
1756 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001757 // Processing a Dex `float-to-int' instruction.
1758 locations->SetInAt(0, Location::RequiresFpuRegister());
1759 locations->SetOut(Location::RequiresRegister());
1760 locations->AddTemp(Location::RequiresFpuRegister());
1761 break;
1762
Roland Levillain946e1432014-11-11 17:35:19 +00001763 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001764 // Processing a Dex `double-to-int' instruction.
1765 locations->SetInAt(0, Location::RequiresFpuRegister());
1766 locations->SetOut(Location::RequiresRegister());
1767 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001768 break;
1769
1770 default:
1771 LOG(FATAL) << "Unexpected type conversion from " << input_type
1772 << " to " << result_type;
1773 }
1774 break;
1775
Roland Levillaindff1f282014-11-05 14:15:05 +00001776 case Primitive::kPrimLong:
1777 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001778 case Primitive::kPrimBoolean:
1779 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001780 case Primitive::kPrimByte:
1781 case Primitive::kPrimShort:
1782 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001783 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001784 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001785 locations->SetInAt(0, Location::RegisterLocation(EAX));
1786 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1787 break;
1788
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001789 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001790 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001791 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001792 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001793 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1794 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1795
Vladimir Marko949c91f2015-01-27 10:48:44 +00001796 // The runtime helper puts the result in EAX, EDX.
1797 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001798 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001799 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001800
1801 default:
1802 LOG(FATAL) << "Unexpected type conversion from " << input_type
1803 << " to " << result_type;
1804 }
1805 break;
1806
Roland Levillain981e4542014-11-14 11:47:14 +00001807 case Primitive::kPrimChar:
1808 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001809 case Primitive::kPrimBoolean:
1810 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001811 case Primitive::kPrimByte:
1812 case Primitive::kPrimShort:
1813 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001814 // Processing a Dex `int-to-char' instruction.
1815 locations->SetInAt(0, Location::Any());
1816 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1817 break;
1818
1819 default:
1820 LOG(FATAL) << "Unexpected type conversion from " << input_type
1821 << " to " << result_type;
1822 }
1823 break;
1824
Roland Levillaindff1f282014-11-05 14:15:05 +00001825 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001826 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001827 case Primitive::kPrimBoolean:
1828 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001829 case Primitive::kPrimByte:
1830 case Primitive::kPrimShort:
1831 case Primitive::kPrimInt:
1832 case Primitive::kPrimChar:
1833 // Processing a Dex `int-to-float' instruction.
1834 locations->SetInAt(0, Location::RequiresRegister());
1835 locations->SetOut(Location::RequiresFpuRegister());
1836 break;
1837
1838 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001839 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001840 locations->SetInAt(0, Location::Any());
1841 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001842 break;
1843
Roland Levillaincff13742014-11-17 14:32:17 +00001844 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001845 // Processing a Dex `double-to-float' instruction.
1846 locations->SetInAt(0, Location::RequiresFpuRegister());
1847 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001848 break;
1849
1850 default:
1851 LOG(FATAL) << "Unexpected type conversion from " << input_type
1852 << " to " << result_type;
1853 };
1854 break;
1855
Roland Levillaindff1f282014-11-05 14:15:05 +00001856 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001857 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001858 case Primitive::kPrimBoolean:
1859 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001860 case Primitive::kPrimByte:
1861 case Primitive::kPrimShort:
1862 case Primitive::kPrimInt:
1863 case Primitive::kPrimChar:
1864 // Processing a Dex `int-to-double' instruction.
1865 locations->SetInAt(0, Location::RequiresRegister());
1866 locations->SetOut(Location::RequiresFpuRegister());
1867 break;
1868
1869 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001870 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001871 locations->SetInAt(0, Location::Any());
1872 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001873 break;
1874
Roland Levillaincff13742014-11-17 14:32:17 +00001875 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001876 // Processing a Dex `float-to-double' instruction.
1877 locations->SetInAt(0, Location::RequiresFpuRegister());
1878 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001879 break;
1880
1881 default:
1882 LOG(FATAL) << "Unexpected type conversion from " << input_type
1883 << " to " << result_type;
1884 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001885 break;
1886
1887 default:
1888 LOG(FATAL) << "Unexpected type conversion from " << input_type
1889 << " to " << result_type;
1890 }
1891}
1892
1893void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1894 LocationSummary* locations = conversion->GetLocations();
1895 Location out = locations->Out();
1896 Location in = locations->InAt(0);
1897 Primitive::Type result_type = conversion->GetResultType();
1898 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001899 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001900 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001901 case Primitive::kPrimByte:
1902 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001903 case Primitive::kPrimBoolean:
1904 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001905 case Primitive::kPrimShort:
1906 case Primitive::kPrimInt:
1907 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001908 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001909 if (in.IsRegister()) {
1910 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001911 } else {
1912 DCHECK(in.GetConstant()->IsIntConstant());
1913 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1914 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1915 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001916 break;
1917
1918 default:
1919 LOG(FATAL) << "Unexpected type conversion from " << input_type
1920 << " to " << result_type;
1921 }
1922 break;
1923
Roland Levillain01a8d712014-11-14 16:27:39 +00001924 case Primitive::kPrimShort:
1925 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001926 case Primitive::kPrimBoolean:
1927 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001928 case Primitive::kPrimByte:
1929 case Primitive::kPrimInt:
1930 case Primitive::kPrimChar:
1931 // Processing a Dex `int-to-short' instruction.
1932 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001933 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001934 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001935 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001936 } else {
1937 DCHECK(in.GetConstant()->IsIntConstant());
1938 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001939 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001940 }
1941 break;
1942
1943 default:
1944 LOG(FATAL) << "Unexpected type conversion from " << input_type
1945 << " to " << result_type;
1946 }
1947 break;
1948
Roland Levillain946e1432014-11-11 17:35:19 +00001949 case Primitive::kPrimInt:
1950 switch (input_type) {
1951 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001952 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001953 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001954 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001955 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001956 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001957 } else {
1958 DCHECK(in.IsConstant());
1959 DCHECK(in.GetConstant()->IsLongConstant());
1960 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001961 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001962 }
1963 break;
1964
Roland Levillain3f8f9362014-12-02 17:45:01 +00001965 case Primitive::kPrimFloat: {
1966 // Processing a Dex `float-to-int' instruction.
1967 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1968 Register output = out.AsRegister<Register>();
1969 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1970 Label done, nan;
1971
1972 __ movl(output, Immediate(kPrimIntMax));
1973 // temp = int-to-float(output)
1974 __ cvtsi2ss(temp, output);
1975 // if input >= temp goto done
1976 __ comiss(input, temp);
1977 __ j(kAboveEqual, &done);
1978 // if input == NaN goto nan
1979 __ j(kUnordered, &nan);
1980 // output = float-to-int-truncate(input)
1981 __ cvttss2si(output, input);
1982 __ jmp(&done);
1983 __ Bind(&nan);
1984 // output = 0
1985 __ xorl(output, output);
1986 __ Bind(&done);
1987 break;
1988 }
1989
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001990 case Primitive::kPrimDouble: {
1991 // Processing a Dex `double-to-int' instruction.
1992 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1993 Register output = out.AsRegister<Register>();
1994 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1995 Label done, nan;
1996
1997 __ movl(output, Immediate(kPrimIntMax));
1998 // temp = int-to-double(output)
1999 __ cvtsi2sd(temp, output);
2000 // if input >= temp goto done
2001 __ comisd(input, temp);
2002 __ j(kAboveEqual, &done);
2003 // if input == NaN goto nan
2004 __ j(kUnordered, &nan);
2005 // output = double-to-int-truncate(input)
2006 __ cvttsd2si(output, input);
2007 __ jmp(&done);
2008 __ Bind(&nan);
2009 // output = 0
2010 __ xorl(output, output);
2011 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002012 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002013 }
Roland Levillain946e1432014-11-11 17:35:19 +00002014
2015 default:
2016 LOG(FATAL) << "Unexpected type conversion from " << input_type
2017 << " to " << result_type;
2018 }
2019 break;
2020
Roland Levillaindff1f282014-11-05 14:15:05 +00002021 case Primitive::kPrimLong:
2022 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002023 case Primitive::kPrimBoolean:
2024 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002025 case Primitive::kPrimByte:
2026 case Primitive::kPrimShort:
2027 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002028 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002029 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002030 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2031 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002032 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002033 __ cdq();
2034 break;
2035
2036 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002037 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002038 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2039 conversion,
2040 conversion->GetDexPc(),
2041 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002042 break;
2043
Roland Levillaindff1f282014-11-05 14:15:05 +00002044 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002045 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002046 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2047 conversion,
2048 conversion->GetDexPc(),
2049 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002050 break;
2051
2052 default:
2053 LOG(FATAL) << "Unexpected type conversion from " << input_type
2054 << " to " << result_type;
2055 }
2056 break;
2057
Roland Levillain981e4542014-11-14 11:47:14 +00002058 case Primitive::kPrimChar:
2059 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002060 case Primitive::kPrimBoolean:
2061 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002062 case Primitive::kPrimByte:
2063 case Primitive::kPrimShort:
2064 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002065 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2066 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002067 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002068 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002069 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002070 } else {
2071 DCHECK(in.GetConstant()->IsIntConstant());
2072 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002073 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002074 }
2075 break;
2076
2077 default:
2078 LOG(FATAL) << "Unexpected type conversion from " << input_type
2079 << " to " << result_type;
2080 }
2081 break;
2082
Roland Levillaindff1f282014-11-05 14:15:05 +00002083 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002084 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002085 case Primitive::kPrimBoolean:
2086 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002087 case Primitive::kPrimByte:
2088 case Primitive::kPrimShort:
2089 case Primitive::kPrimInt:
2090 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002091 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002092 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002093 break;
2094
Roland Levillain6d0e4832014-11-27 18:31:21 +00002095 case Primitive::kPrimLong: {
2096 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002097 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002098
Roland Levillain232ade02015-04-20 15:14:36 +01002099 // Create stack space for the call to
2100 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2101 // TODO: enhance register allocator to ask for stack temporaries.
2102 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2103 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2104 __ subl(ESP, Immediate(adjustment));
2105 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002106
Roland Levillain232ade02015-04-20 15:14:36 +01002107 // Load the value to the FP stack, using temporaries if needed.
2108 PushOntoFPStack(in, 0, adjustment, false, true);
2109
2110 if (out.IsStackSlot()) {
2111 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2112 } else {
2113 __ fstps(Address(ESP, 0));
2114 Location stack_temp = Location::StackSlot(0);
2115 codegen_->Move32(out, stack_temp);
2116 }
2117
2118 // Remove the temporary stack space we allocated.
2119 if (adjustment != 0) {
2120 __ addl(ESP, Immediate(adjustment));
2121 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002122 break;
2123 }
2124
Roland Levillaincff13742014-11-17 14:32:17 +00002125 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002126 // Processing a Dex `double-to-float' instruction.
2127 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002128 break;
2129
2130 default:
2131 LOG(FATAL) << "Unexpected type conversion from " << input_type
2132 << " to " << result_type;
2133 };
2134 break;
2135
Roland Levillaindff1f282014-11-05 14:15:05 +00002136 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002137 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002138 case Primitive::kPrimBoolean:
2139 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002140 case Primitive::kPrimByte:
2141 case Primitive::kPrimShort:
2142 case Primitive::kPrimInt:
2143 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002144 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002145 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002146 break;
2147
Roland Levillain647b9ed2014-11-27 12:06:00 +00002148 case Primitive::kPrimLong: {
2149 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002150 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002151
Roland Levillain232ade02015-04-20 15:14:36 +01002152 // Create stack space for the call to
2153 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2154 // TODO: enhance register allocator to ask for stack temporaries.
2155 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2156 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2157 __ subl(ESP, Immediate(adjustment));
2158 }
2159
2160 // Load the value to the FP stack, using temporaries if needed.
2161 PushOntoFPStack(in, 0, adjustment, false, true);
2162
2163 if (out.IsDoubleStackSlot()) {
2164 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2165 } else {
2166 __ fstpl(Address(ESP, 0));
2167 Location stack_temp = Location::DoubleStackSlot(0);
2168 codegen_->Move64(out, stack_temp);
2169 }
2170
2171 // Remove the temporary stack space we allocated.
2172 if (adjustment != 0) {
2173 __ addl(ESP, Immediate(adjustment));
2174 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002175 break;
2176 }
2177
Roland Levillaincff13742014-11-17 14:32:17 +00002178 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002179 // Processing a Dex `float-to-double' instruction.
2180 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002181 break;
2182
2183 default:
2184 LOG(FATAL) << "Unexpected type conversion from " << input_type
2185 << " to " << result_type;
2186 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002187 break;
2188
2189 default:
2190 LOG(FATAL) << "Unexpected type conversion from " << input_type
2191 << " to " << result_type;
2192 }
2193}
2194
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002195void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002196 LocationSummary* locations =
2197 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002198 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002199 case Primitive::kPrimInt: {
2200 locations->SetInAt(0, Location::RequiresRegister());
2201 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2202 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2203 break;
2204 }
2205
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002206 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002207 locations->SetInAt(0, Location::RequiresRegister());
2208 locations->SetInAt(1, Location::Any());
2209 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002210 break;
2211 }
2212
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002213 case Primitive::kPrimFloat:
2214 case Primitive::kPrimDouble: {
2215 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00002216 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002217 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002218 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002219 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002220
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002221 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002222 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2223 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002224 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002225}
2226
2227void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2228 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002229 Location first = locations->InAt(0);
2230 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002231 Location out = locations->Out();
2232
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002233 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002234 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002235 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002236 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2237 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002238 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2239 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002240 } else {
2241 __ leal(out.AsRegister<Register>(), Address(
2242 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2243 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002244 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002245 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2246 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2247 __ addl(out.AsRegister<Register>(), Immediate(value));
2248 } else {
2249 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2250 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002251 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002252 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002253 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002254 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002255 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002256 }
2257
2258 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002259 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002260 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2261 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002262 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002263 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2264 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002265 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002266 } else {
2267 DCHECK(second.IsConstant()) << second;
2268 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2269 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2270 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002271 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002272 break;
2273 }
2274
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002275 case Primitive::kPrimFloat: {
2276 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002277 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002278 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002279 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002280 }
2281
2282 case Primitive::kPrimDouble: {
2283 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002284 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002285 }
2286 break;
2287 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002288
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002289 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002290 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002291 }
2292}
2293
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002294void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002295 LocationSummary* locations =
2296 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002297 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002298 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002299 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002300 locations->SetInAt(0, Location::RequiresRegister());
2301 locations->SetInAt(1, Location::Any());
2302 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002303 break;
2304 }
Calin Juravle11351682014-10-23 15:38:15 +01002305 case Primitive::kPrimFloat:
2306 case Primitive::kPrimDouble: {
2307 locations->SetInAt(0, Location::RequiresFpuRegister());
2308 locations->SetInAt(1, Location::RequiresFpuRegister());
2309 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002310 break;
Calin Juravle11351682014-10-23 15:38:15 +01002311 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002312
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002313 default:
Calin Juravle11351682014-10-23 15:38:15 +01002314 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002315 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002316}
2317
2318void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2319 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002320 Location first = locations->InAt(0);
2321 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002322 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002323 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002324 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002325 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002326 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002327 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002328 __ subl(first.AsRegister<Register>(),
2329 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002330 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002331 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002332 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002333 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002334 }
2335
2336 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002337 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002338 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2339 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002340 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002341 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002342 __ sbbl(first.AsRegisterPairHigh<Register>(),
2343 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002344 } else {
2345 DCHECK(second.IsConstant()) << second;
2346 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2347 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2348 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002349 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002350 break;
2351 }
2352
Calin Juravle11351682014-10-23 15:38:15 +01002353 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002354 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002355 break;
Calin Juravle11351682014-10-23 15:38:15 +01002356 }
2357
2358 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002359 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002360 break;
2361 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002362
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002363 default:
Calin Juravle11351682014-10-23 15:38:15 +01002364 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002365 }
2366}
2367
Calin Juravle34bacdf2014-10-07 20:23:36 +01002368void LocationsBuilderX86::VisitMul(HMul* mul) {
2369 LocationSummary* locations =
2370 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2371 switch (mul->GetResultType()) {
2372 case Primitive::kPrimInt:
2373 locations->SetInAt(0, Location::RequiresRegister());
2374 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002375 if (mul->InputAt(1)->IsIntConstant()) {
2376 // Can use 3 operand multiply.
2377 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2378 } else {
2379 locations->SetOut(Location::SameAsFirstInput());
2380 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002381 break;
2382 case Primitive::kPrimLong: {
2383 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002384 locations->SetInAt(1, Location::Any());
2385 locations->SetOut(Location::SameAsFirstInput());
2386 // Needed for imul on 32bits with 64bits output.
2387 locations->AddTemp(Location::RegisterLocation(EAX));
2388 locations->AddTemp(Location::RegisterLocation(EDX));
2389 break;
2390 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002391 case Primitive::kPrimFloat:
2392 case Primitive::kPrimDouble: {
2393 locations->SetInAt(0, Location::RequiresFpuRegister());
2394 locations->SetInAt(1, Location::RequiresFpuRegister());
2395 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002396 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002397 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002398
2399 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002400 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002401 }
2402}
2403
2404void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2405 LocationSummary* locations = mul->GetLocations();
2406 Location first = locations->InAt(0);
2407 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002408 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002409
2410 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002411 case Primitive::kPrimInt:
2412 // The constant may have ended up in a register, so test explicitly to avoid
2413 // problems where the output may not be the same as the first operand.
2414 if (mul->InputAt(1)->IsIntConstant()) {
2415 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2416 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2417 } else if (second.IsRegister()) {
2418 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002419 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002420 } else {
2421 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002422 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002423 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002424 }
2425 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002426
2427 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002428 Register in1_hi = first.AsRegisterPairHigh<Register>();
2429 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002430 Register eax = locations->GetTemp(0).AsRegister<Register>();
2431 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002432
2433 DCHECK_EQ(EAX, eax);
2434 DCHECK_EQ(EDX, edx);
2435
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002436 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002437 // output: in1
2438 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2439 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2440 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002441 if (second.IsConstant()) {
2442 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002443
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002444 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2445 int32_t low_value = Low32Bits(value);
2446 int32_t high_value = High32Bits(value);
2447 Immediate low(low_value);
2448 Immediate high(high_value);
2449
2450 __ movl(eax, high);
2451 // eax <- in1.lo * in2.hi
2452 __ imull(eax, in1_lo);
2453 // in1.hi <- in1.hi * in2.lo
2454 __ imull(in1_hi, low);
2455 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2456 __ addl(in1_hi, eax);
2457 // move in2_lo to eax to prepare for double precision
2458 __ movl(eax, low);
2459 // edx:eax <- in1.lo * in2.lo
2460 __ mull(in1_lo);
2461 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2462 __ addl(in1_hi, edx);
2463 // in1.lo <- (in1.lo * in2.lo)[31:0];
2464 __ movl(in1_lo, eax);
2465 } else if (second.IsRegisterPair()) {
2466 Register in2_hi = second.AsRegisterPairHigh<Register>();
2467 Register in2_lo = second.AsRegisterPairLow<Register>();
2468
2469 __ movl(eax, in2_hi);
2470 // eax <- in1.lo * in2.hi
2471 __ imull(eax, in1_lo);
2472 // in1.hi <- in1.hi * in2.lo
2473 __ imull(in1_hi, in2_lo);
2474 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2475 __ addl(in1_hi, eax);
2476 // move in1_lo to eax to prepare for double precision
2477 __ movl(eax, in1_lo);
2478 // edx:eax <- in1.lo * in2.lo
2479 __ mull(in2_lo);
2480 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2481 __ addl(in1_hi, edx);
2482 // in1.lo <- (in1.lo * in2.lo)[31:0];
2483 __ movl(in1_lo, eax);
2484 } else {
2485 DCHECK(second.IsDoubleStackSlot()) << second;
2486 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2487 Address in2_lo(ESP, second.GetStackIndex());
2488
2489 __ movl(eax, in2_hi);
2490 // eax <- in1.lo * in2.hi
2491 __ imull(eax, in1_lo);
2492 // in1.hi <- in1.hi * in2.lo
2493 __ imull(in1_hi, in2_lo);
2494 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2495 __ addl(in1_hi, eax);
2496 // move in1_lo to eax to prepare for double precision
2497 __ movl(eax, in1_lo);
2498 // edx:eax <- in1.lo * in2.lo
2499 __ mull(in2_lo);
2500 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2501 __ addl(in1_hi, edx);
2502 // in1.lo <- (in1.lo * in2.lo)[31:0];
2503 __ movl(in1_lo, eax);
2504 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002505
2506 break;
2507 }
2508
Calin Juravleb5bfa962014-10-21 18:02:24 +01002509 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002510 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002511 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002512 }
2513
2514 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002515 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002516 break;
2517 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002518
2519 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002520 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002521 }
2522}
2523
Roland Levillain232ade02015-04-20 15:14:36 +01002524void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2525 uint32_t temp_offset,
2526 uint32_t stack_adjustment,
2527 bool is_fp,
2528 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002529 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002530 DCHECK(!is_wide);
2531 if (is_fp) {
2532 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2533 } else {
2534 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2535 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002536 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002537 DCHECK(is_wide);
2538 if (is_fp) {
2539 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2540 } else {
2541 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2542 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002543 } else {
2544 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002545 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002546 Location stack_temp = Location::StackSlot(temp_offset);
2547 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002548 if (is_fp) {
2549 __ flds(Address(ESP, temp_offset));
2550 } else {
2551 __ filds(Address(ESP, temp_offset));
2552 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002553 } else {
2554 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2555 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002556 if (is_fp) {
2557 __ fldl(Address(ESP, temp_offset));
2558 } else {
2559 __ fildl(Address(ESP, temp_offset));
2560 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002561 }
2562 }
2563}
2564
2565void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2566 Primitive::Type type = rem->GetResultType();
2567 bool is_float = type == Primitive::kPrimFloat;
2568 size_t elem_size = Primitive::ComponentSize(type);
2569 LocationSummary* locations = rem->GetLocations();
2570 Location first = locations->InAt(0);
2571 Location second = locations->InAt(1);
2572 Location out = locations->Out();
2573
2574 // Create stack space for 2 elements.
2575 // TODO: enhance register allocator to ask for stack temporaries.
2576 __ subl(ESP, Immediate(2 * elem_size));
2577
2578 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002579 const bool is_wide = !is_float;
2580 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2581 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002582
2583 // Loop doing FPREM until we stabilize.
2584 Label retry;
2585 __ Bind(&retry);
2586 __ fprem();
2587
2588 // Move FP status to AX.
2589 __ fstsw();
2590
2591 // And see if the argument reduction is complete. This is signaled by the
2592 // C2 FPU flag bit set to 0.
2593 __ andl(EAX, Immediate(kC2ConditionMask));
2594 __ j(kNotEqual, &retry);
2595
2596 // We have settled on the final value. Retrieve it into an XMM register.
2597 // Store FP top of stack to real stack.
2598 if (is_float) {
2599 __ fsts(Address(ESP, 0));
2600 } else {
2601 __ fstl(Address(ESP, 0));
2602 }
2603
2604 // Pop the 2 items from the FP stack.
2605 __ fucompp();
2606
2607 // Load the value from the stack into an XMM register.
2608 DCHECK(out.IsFpuRegister()) << out;
2609 if (is_float) {
2610 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2611 } else {
2612 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2613 }
2614
2615 // And remove the temporary stack space we allocated.
2616 __ addl(ESP, Immediate(2 * elem_size));
2617}
2618
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002619
2620void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2621 DCHECK(instruction->IsDiv() || instruction->IsRem());
2622
2623 LocationSummary* locations = instruction->GetLocations();
2624 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002625 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002626
2627 Register out_register = locations->Out().AsRegister<Register>();
2628 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002629 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002630
2631 DCHECK(imm == 1 || imm == -1);
2632
2633 if (instruction->IsRem()) {
2634 __ xorl(out_register, out_register);
2635 } else {
2636 __ movl(out_register, input_register);
2637 if (imm == -1) {
2638 __ negl(out_register);
2639 }
2640 }
2641}
2642
2643
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002644void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002645 LocationSummary* locations = instruction->GetLocations();
2646
2647 Register out_register = locations->Out().AsRegister<Register>();
2648 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002649 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002650
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002651 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002652 Register num = locations->GetTemp(0).AsRegister<Register>();
2653
2654 __ leal(num, Address(input_register, std::abs(imm) - 1));
2655 __ testl(input_register, input_register);
2656 __ cmovl(kGreaterEqual, num, input_register);
2657 int shift = CTZ(imm);
2658 __ sarl(num, Immediate(shift));
2659
2660 if (imm < 0) {
2661 __ negl(num);
2662 }
2663
2664 __ movl(out_register, num);
2665}
2666
2667void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2668 DCHECK(instruction->IsDiv() || instruction->IsRem());
2669
2670 LocationSummary* locations = instruction->GetLocations();
2671 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2672
2673 Register eax = locations->InAt(0).AsRegister<Register>();
2674 Register out = locations->Out().AsRegister<Register>();
2675 Register num;
2676 Register edx;
2677
2678 if (instruction->IsDiv()) {
2679 edx = locations->GetTemp(0).AsRegister<Register>();
2680 num = locations->GetTemp(1).AsRegister<Register>();
2681 } else {
2682 edx = locations->Out().AsRegister<Register>();
2683 num = locations->GetTemp(0).AsRegister<Register>();
2684 }
2685
2686 DCHECK_EQ(EAX, eax);
2687 DCHECK_EQ(EDX, edx);
2688 if (instruction->IsDiv()) {
2689 DCHECK_EQ(EAX, out);
2690 } else {
2691 DCHECK_EQ(EDX, out);
2692 }
2693
2694 int64_t magic;
2695 int shift;
2696 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2697
2698 Label ndiv;
2699 Label end;
2700 // If numerator is 0, the result is 0, no computation needed.
2701 __ testl(eax, eax);
2702 __ j(kNotEqual, &ndiv);
2703
2704 __ xorl(out, out);
2705 __ jmp(&end);
2706
2707 __ Bind(&ndiv);
2708
2709 // Save the numerator.
2710 __ movl(num, eax);
2711
2712 // EAX = magic
2713 __ movl(eax, Immediate(magic));
2714
2715 // EDX:EAX = magic * numerator
2716 __ imull(num);
2717
2718 if (imm > 0 && magic < 0) {
2719 // EDX += num
2720 __ addl(edx, num);
2721 } else if (imm < 0 && magic > 0) {
2722 __ subl(edx, num);
2723 }
2724
2725 // Shift if needed.
2726 if (shift != 0) {
2727 __ sarl(edx, Immediate(shift));
2728 }
2729
2730 // EDX += 1 if EDX < 0
2731 __ movl(eax, edx);
2732 __ shrl(edx, Immediate(31));
2733 __ addl(edx, eax);
2734
2735 if (instruction->IsRem()) {
2736 __ movl(eax, num);
2737 __ imull(edx, Immediate(imm));
2738 __ subl(eax, edx);
2739 __ movl(edx, eax);
2740 } else {
2741 __ movl(eax, edx);
2742 }
2743 __ Bind(&end);
2744}
2745
Calin Juravlebacfec32014-11-14 15:54:36 +00002746void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2747 DCHECK(instruction->IsDiv() || instruction->IsRem());
2748
2749 LocationSummary* locations = instruction->GetLocations();
2750 Location out = locations->Out();
2751 Location first = locations->InAt(0);
2752 Location second = locations->InAt(1);
2753 bool is_div = instruction->IsDiv();
2754
2755 switch (instruction->GetResultType()) {
2756 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002757 DCHECK_EQ(EAX, first.AsRegister<Register>());
2758 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002759
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002760 if (instruction->InputAt(1)->IsIntConstant()) {
2761 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002762
2763 if (imm == 0) {
2764 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2765 } else if (imm == 1 || imm == -1) {
2766 DivRemOneOrMinusOne(instruction);
2767 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002768 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002769 } else {
2770 DCHECK(imm <= -2 || imm >= 2);
2771 GenerateDivRemWithAnyConstant(instruction);
2772 }
2773 } else {
2774 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002775 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002776 is_div);
2777 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002778
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002779 Register second_reg = second.AsRegister<Register>();
2780 // 0x80000000/-1 triggers an arithmetic exception!
2781 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2782 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002783
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002784 __ cmpl(second_reg, Immediate(-1));
2785 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002786
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002787 // edx:eax <- sign-extended of eax
2788 __ cdq();
2789 // eax = quotient, edx = remainder
2790 __ idivl(second_reg);
2791 __ Bind(slow_path->GetExitLabel());
2792 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002793 break;
2794 }
2795
2796 case Primitive::kPrimLong: {
2797 InvokeRuntimeCallingConvention calling_convention;
2798 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2799 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2800 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2801 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2802 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2803 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2804
2805 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002806 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2807 instruction,
2808 instruction->GetDexPc(),
2809 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002810 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002811 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2812 instruction,
2813 instruction->GetDexPc(),
2814 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002815 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002816 break;
2817 }
2818
2819 default:
2820 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2821 }
2822}
2823
Calin Juravle7c4954d2014-10-28 16:57:40 +00002824void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002825 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002826 ? LocationSummary::kCall
2827 : LocationSummary::kNoCall;
2828 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2829
Calin Juravle7c4954d2014-10-28 16:57:40 +00002830 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002831 case Primitive::kPrimInt: {
2832 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002833 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002834 locations->SetOut(Location::SameAsFirstInput());
2835 // Intel uses edx:eax as the dividend.
2836 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002837 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2838 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2839 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002840 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002841 locations->AddTemp(Location::RequiresRegister());
2842 }
Calin Juravled0d48522014-11-04 16:40:20 +00002843 break;
2844 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002845 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002846 InvokeRuntimeCallingConvention calling_convention;
2847 locations->SetInAt(0, Location::RegisterPairLocation(
2848 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2849 locations->SetInAt(1, Location::RegisterPairLocation(
2850 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2851 // Runtime helper puts the result in EAX, EDX.
2852 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002853 break;
2854 }
2855 case Primitive::kPrimFloat:
2856 case Primitive::kPrimDouble: {
2857 locations->SetInAt(0, Location::RequiresFpuRegister());
2858 locations->SetInAt(1, Location::RequiresFpuRegister());
2859 locations->SetOut(Location::SameAsFirstInput());
2860 break;
2861 }
2862
2863 default:
2864 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2865 }
2866}
2867
2868void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2869 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002870 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002871 Location first = locations->InAt(0);
2872 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002873
2874 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002875 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002876 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002877 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002878 break;
2879 }
2880
2881 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002882 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002883 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002884 break;
2885 }
2886
2887 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002888 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002889 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002890 break;
2891 }
2892
2893 default:
2894 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2895 }
2896}
2897
Calin Juravlebacfec32014-11-14 15:54:36 +00002898void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002899 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002900
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002901 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2902 ? LocationSummary::kCall
2903 : LocationSummary::kNoCall;
2904 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002905
Calin Juravled2ec87d2014-12-08 14:24:46 +00002906 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002907 case Primitive::kPrimInt: {
2908 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002909 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002910 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002911 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2912 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2913 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002914 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002915 locations->AddTemp(Location::RequiresRegister());
2916 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002917 break;
2918 }
2919 case Primitive::kPrimLong: {
2920 InvokeRuntimeCallingConvention calling_convention;
2921 locations->SetInAt(0, Location::RegisterPairLocation(
2922 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2923 locations->SetInAt(1, Location::RegisterPairLocation(
2924 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2925 // Runtime helper puts the result in EAX, EDX.
2926 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2927 break;
2928 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002929 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002930 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002931 locations->SetInAt(0, Location::Any());
2932 locations->SetInAt(1, Location::Any());
2933 locations->SetOut(Location::RequiresFpuRegister());
2934 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002935 break;
2936 }
2937
2938 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002939 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002940 }
2941}
2942
2943void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2944 Primitive::Type type = rem->GetResultType();
2945 switch (type) {
2946 case Primitive::kPrimInt:
2947 case Primitive::kPrimLong: {
2948 GenerateDivRemIntegral(rem);
2949 break;
2950 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002951 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002952 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002953 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002954 break;
2955 }
2956 default:
2957 LOG(FATAL) << "Unexpected rem type " << type;
2958 }
2959}
2960
Calin Juravled0d48522014-11-04 16:40:20 +00002961void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2962 LocationSummary* locations =
2963 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002964 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002965 case Primitive::kPrimByte:
2966 case Primitive::kPrimChar:
2967 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002968 case Primitive::kPrimInt: {
2969 locations->SetInAt(0, Location::Any());
2970 break;
2971 }
2972 case Primitive::kPrimLong: {
2973 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2974 if (!instruction->IsConstant()) {
2975 locations->AddTemp(Location::RequiresRegister());
2976 }
2977 break;
2978 }
2979 default:
2980 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2981 }
Calin Juravled0d48522014-11-04 16:40:20 +00002982 if (instruction->HasUses()) {
2983 locations->SetOut(Location::SameAsFirstInput());
2984 }
2985}
2986
2987void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2988 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2989 codegen_->AddSlowPath(slow_path);
2990
2991 LocationSummary* locations = instruction->GetLocations();
2992 Location value = locations->InAt(0);
2993
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002994 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002995 case Primitive::kPrimByte:
2996 case Primitive::kPrimChar:
2997 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002998 case Primitive::kPrimInt: {
2999 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003000 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003001 __ j(kEqual, slow_path->GetEntryLabel());
3002 } else if (value.IsStackSlot()) {
3003 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3004 __ j(kEqual, slow_path->GetEntryLabel());
3005 } else {
3006 DCHECK(value.IsConstant()) << value;
3007 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3008 __ jmp(slow_path->GetEntryLabel());
3009 }
3010 }
3011 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003012 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003013 case Primitive::kPrimLong: {
3014 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003015 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003016 __ movl(temp, value.AsRegisterPairLow<Register>());
3017 __ orl(temp, value.AsRegisterPairHigh<Register>());
3018 __ j(kEqual, slow_path->GetEntryLabel());
3019 } else {
3020 DCHECK(value.IsConstant()) << value;
3021 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3022 __ jmp(slow_path->GetEntryLabel());
3023 }
3024 }
3025 break;
3026 }
3027 default:
3028 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003029 }
Calin Juravled0d48522014-11-04 16:40:20 +00003030}
3031
Calin Juravle9aec02f2014-11-18 23:06:35 +00003032void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3033 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3034
3035 LocationSummary* locations =
3036 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3037
3038 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003039 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003040 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003041 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003042 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003043 // The shift count needs to be in CL or a constant.
3044 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003045 locations->SetOut(Location::SameAsFirstInput());
3046 break;
3047 }
3048 default:
3049 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3050 }
3051}
3052
3053void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3054 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3055
3056 LocationSummary* locations = op->GetLocations();
3057 Location first = locations->InAt(0);
3058 Location second = locations->InAt(1);
3059 DCHECK(first.Equals(locations->Out()));
3060
3061 switch (op->GetResultType()) {
3062 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003063 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003064 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003065 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003066 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003067 DCHECK_EQ(ECX, second_reg);
3068 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003069 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003070 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003071 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003072 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003073 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003074 }
3075 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003076 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3077 if (shift == 0) {
3078 return;
3079 }
3080 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003081 if (op->IsShl()) {
3082 __ shll(first_reg, imm);
3083 } else if (op->IsShr()) {
3084 __ sarl(first_reg, imm);
3085 } else {
3086 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003087 }
3088 }
3089 break;
3090 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003091 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003092 if (second.IsRegister()) {
3093 Register second_reg = second.AsRegister<Register>();
3094 DCHECK_EQ(ECX, second_reg);
3095 if (op->IsShl()) {
3096 GenerateShlLong(first, second_reg);
3097 } else if (op->IsShr()) {
3098 GenerateShrLong(first, second_reg);
3099 } else {
3100 GenerateUShrLong(first, second_reg);
3101 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003102 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003103 // Shift by a constant.
3104 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3105 // Nothing to do if the shift is 0, as the input is already the output.
3106 if (shift != 0) {
3107 if (op->IsShl()) {
3108 GenerateShlLong(first, shift);
3109 } else if (op->IsShr()) {
3110 GenerateShrLong(first, shift);
3111 } else {
3112 GenerateUShrLong(first, shift);
3113 }
3114 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003115 }
3116 break;
3117 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003118 default:
3119 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3120 }
3121}
3122
Mark P Mendell73945692015-04-29 14:56:17 +00003123void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3124 Register low = loc.AsRegisterPairLow<Register>();
3125 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003126 if (shift == 1) {
3127 // This is just an addition.
3128 __ addl(low, low);
3129 __ adcl(high, high);
3130 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003131 // Shift by 32 is easy. High gets low, and low gets 0.
3132 codegen_->EmitParallelMoves(
3133 loc.ToLow(),
3134 loc.ToHigh(),
3135 Primitive::kPrimInt,
3136 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3137 loc.ToLow(),
3138 Primitive::kPrimInt);
3139 } else if (shift > 32) {
3140 // Low part becomes 0. High part is low part << (shift-32).
3141 __ movl(high, low);
3142 __ shll(high, Immediate(shift - 32));
3143 __ xorl(low, low);
3144 } else {
3145 // Between 1 and 31.
3146 __ shld(high, low, Immediate(shift));
3147 __ shll(low, Immediate(shift));
3148 }
3149}
3150
Calin Juravle9aec02f2014-11-18 23:06:35 +00003151void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
3152 Label done;
3153 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3154 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3155 __ testl(shifter, Immediate(32));
3156 __ j(kEqual, &done);
3157 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3158 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3159 __ Bind(&done);
3160}
3161
Mark P Mendell73945692015-04-29 14:56:17 +00003162void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3163 Register low = loc.AsRegisterPairLow<Register>();
3164 Register high = loc.AsRegisterPairHigh<Register>();
3165 if (shift == 32) {
3166 // Need to copy the sign.
3167 DCHECK_NE(low, high);
3168 __ movl(low, high);
3169 __ sarl(high, Immediate(31));
3170 } else if (shift > 32) {
3171 DCHECK_NE(low, high);
3172 // High part becomes sign. Low part is shifted by shift - 32.
3173 __ movl(low, high);
3174 __ sarl(high, Immediate(31));
3175 __ sarl(low, Immediate(shift - 32));
3176 } else {
3177 // Between 1 and 31.
3178 __ shrd(low, high, Immediate(shift));
3179 __ sarl(high, Immediate(shift));
3180 }
3181}
3182
Calin Juravle9aec02f2014-11-18 23:06:35 +00003183void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
3184 Label done;
3185 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3186 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3187 __ testl(shifter, Immediate(32));
3188 __ j(kEqual, &done);
3189 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3190 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3191 __ Bind(&done);
3192}
3193
Mark P Mendell73945692015-04-29 14:56:17 +00003194void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3195 Register low = loc.AsRegisterPairLow<Register>();
3196 Register high = loc.AsRegisterPairHigh<Register>();
3197 if (shift == 32) {
3198 // Shift by 32 is easy. Low gets high, and high gets 0.
3199 codegen_->EmitParallelMoves(
3200 loc.ToHigh(),
3201 loc.ToLow(),
3202 Primitive::kPrimInt,
3203 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3204 loc.ToHigh(),
3205 Primitive::kPrimInt);
3206 } else if (shift > 32) {
3207 // Low part is high >> (shift - 32). High part becomes 0.
3208 __ movl(low, high);
3209 __ shrl(low, Immediate(shift - 32));
3210 __ xorl(high, high);
3211 } else {
3212 // Between 1 and 31.
3213 __ shrd(low, high, Immediate(shift));
3214 __ shrl(high, Immediate(shift));
3215 }
3216}
3217
Calin Juravle9aec02f2014-11-18 23:06:35 +00003218void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
3219 Label done;
3220 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3221 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3222 __ testl(shifter, Immediate(32));
3223 __ j(kEqual, &done);
3224 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3225 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3226 __ Bind(&done);
3227}
3228
3229void LocationsBuilderX86::VisitShl(HShl* shl) {
3230 HandleShift(shl);
3231}
3232
3233void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3234 HandleShift(shl);
3235}
3236
3237void LocationsBuilderX86::VisitShr(HShr* shr) {
3238 HandleShift(shr);
3239}
3240
3241void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3242 HandleShift(shr);
3243}
3244
3245void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3246 HandleShift(ushr);
3247}
3248
3249void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3250 HandleShift(ushr);
3251}
3252
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003253void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003254 LocationSummary* locations =
3255 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003256 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003257 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003258 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003259 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003260}
3261
3262void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3263 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003264 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003265 // Note: if heap poisoning is enabled, the entry point takes cares
3266 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003267 codegen_->InvokeRuntime(
3268 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3269 instruction,
3270 instruction->GetDexPc(),
3271 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003272 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003273}
3274
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003275void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3276 LocationSummary* locations =
3277 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3278 locations->SetOut(Location::RegisterLocation(EAX));
3279 InvokeRuntimeCallingConvention calling_convention;
3280 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003281 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003282 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003283}
3284
3285void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3286 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003287 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3288
Roland Levillain4d027112015-07-01 15:41:14 +01003289 // Note: if heap poisoning is enabled, the entry point takes cares
3290 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003291 codegen_->InvokeRuntime(
3292 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3293 instruction,
3294 instruction->GetDexPc(),
3295 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003296 DCHECK(!codegen_->IsLeafMethod());
3297}
3298
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003299void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003300 LocationSummary* locations =
3301 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003302 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3303 if (location.IsStackSlot()) {
3304 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3305 } else if (location.IsDoubleStackSlot()) {
3306 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003307 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003308 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003309}
3310
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003311void InstructionCodeGeneratorX86::VisitParameterValue(
3312 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3313}
3314
3315void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3316 LocationSummary* locations =
3317 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3318 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3319}
3320
3321void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003322}
3323
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003324void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003325 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003326 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003327 locations->SetInAt(0, Location::RequiresRegister());
3328 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003329}
3330
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003331void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3332 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003333 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003334 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003335 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003336 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003337 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003338 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003339 break;
3340
3341 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003342 __ notl(out.AsRegisterPairLow<Register>());
3343 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003344 break;
3345
3346 default:
3347 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3348 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003349}
3350
David Brazdil66d126e2015-04-03 16:02:44 +01003351void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3352 LocationSummary* locations =
3353 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3354 locations->SetInAt(0, Location::RequiresRegister());
3355 locations->SetOut(Location::SameAsFirstInput());
3356}
3357
3358void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003359 LocationSummary* locations = bool_not->GetLocations();
3360 Location in = locations->InAt(0);
3361 Location out = locations->Out();
3362 DCHECK(in.Equals(out));
3363 __ xorl(out.AsRegister<Register>(), Immediate(1));
3364}
3365
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003366void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003367 LocationSummary* locations =
3368 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003369 switch (compare->InputAt(0)->GetType()) {
3370 case Primitive::kPrimLong: {
3371 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003372 locations->SetInAt(1, Location::Any());
3373 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3374 break;
3375 }
3376 case Primitive::kPrimFloat:
3377 case Primitive::kPrimDouble: {
3378 locations->SetInAt(0, Location::RequiresFpuRegister());
3379 locations->SetInAt(1, Location::RequiresFpuRegister());
3380 locations->SetOut(Location::RequiresRegister());
3381 break;
3382 }
3383 default:
3384 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3385 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003386}
3387
3388void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003389 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003390 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003391 Location left = locations->InAt(0);
3392 Location right = locations->InAt(1);
3393
3394 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003395 switch (compare->InputAt(0)->GetType()) {
3396 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003397 Register left_low = left.AsRegisterPairLow<Register>();
3398 Register left_high = left.AsRegisterPairHigh<Register>();
3399 int32_t val_low = 0;
3400 int32_t val_high = 0;
3401 bool right_is_const = false;
3402
3403 if (right.IsConstant()) {
3404 DCHECK(right.GetConstant()->IsLongConstant());
3405 right_is_const = true;
3406 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3407 val_low = Low32Bits(val);
3408 val_high = High32Bits(val);
3409 }
3410
Calin Juravleddb7df22014-11-25 20:56:51 +00003411 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003412 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003413 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003414 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003415 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003416 DCHECK(right_is_const) << right;
3417 if (val_high == 0) {
3418 __ testl(left_high, left_high);
3419 } else {
3420 __ cmpl(left_high, Immediate(val_high));
3421 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003422 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003423 __ j(kLess, &less); // Signed compare.
3424 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003425 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003426 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003427 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003428 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003429 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003430 DCHECK(right_is_const) << right;
3431 if (val_low == 0) {
3432 __ testl(left_low, left_low);
3433 } else {
3434 __ cmpl(left_low, Immediate(val_low));
3435 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003436 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003437 break;
3438 }
3439 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003440 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003441 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3442 break;
3443 }
3444 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003445 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003446 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003447 break;
3448 }
3449 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003450 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003451 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003452 __ movl(out, Immediate(0));
3453 __ j(kEqual, &done);
3454 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3455
3456 __ Bind(&greater);
3457 __ movl(out, Immediate(1));
3458 __ jmp(&done);
3459
3460 __ Bind(&less);
3461 __ movl(out, Immediate(-1));
3462
3463 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003464}
3465
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003466void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003467 LocationSummary* locations =
3468 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003469 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3470 locations->SetInAt(i, Location::Any());
3471 }
3472 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003473}
3474
3475void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003476 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003477 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003478}
3479
Calin Juravle52c48962014-12-16 17:02:57 +00003480void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3481 /*
3482 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3483 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3484 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3485 */
3486 switch (kind) {
3487 case MemBarrierKind::kAnyAny: {
3488 __ mfence();
3489 break;
3490 }
3491 case MemBarrierKind::kAnyStore:
3492 case MemBarrierKind::kLoadAny:
3493 case MemBarrierKind::kStoreStore: {
3494 // nop
3495 break;
3496 }
3497 default:
3498 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003499 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003500}
3501
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003502
Vladimir Marko58155012015-08-19 12:49:41 +00003503void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3504 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3505 switch (invoke->GetMethodLoadKind()) {
3506 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3507 // temp = thread->string_init_entrypoint
3508 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3509 break;
3510 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3511 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3512 break;
3513 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3514 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3515 break;
3516 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3517 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3518 method_patches_.emplace_back(invoke->GetTargetMethod());
3519 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3520 break;
3521 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3522 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3523 FALLTHROUGH_INTENDED;
3524 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3525 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3526 Register method_reg;
3527 Register reg = temp.AsRegister<Register>();
3528 if (current_method.IsRegister()) {
3529 method_reg = current_method.AsRegister<Register>();
3530 } else {
3531 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3532 DCHECK(!current_method.IsValid());
3533 method_reg = reg;
3534 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3535 }
3536 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003537 __ movl(reg, Address(method_reg,
3538 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003539 // temp = temp[index_in_cache]
3540 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3541 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3542 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003543 }
Vladimir Marko58155012015-08-19 12:49:41 +00003544 }
3545
3546 switch (invoke->GetCodePtrLocation()) {
3547 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3548 __ call(GetFrameEntryLabel());
3549 break;
3550 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3551 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3552 Label* label = &relative_call_patches_.back().label;
3553 __ call(label); // Bind to the patch label, override at link time.
3554 __ Bind(label); // Bind the label at the end of the "call" insn.
3555 break;
3556 }
3557 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3558 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3559 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3560 // (Though the direct CALL ptr16:32 is available for consideration).
3561 FALLTHROUGH_INTENDED;
3562 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3563 // (callee_method + offset_of_quick_compiled_code)()
3564 __ call(Address(callee_method.AsRegister<Register>(),
3565 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3566 kX86WordSize).Int32Value()));
3567 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003568 }
3569
3570 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003571}
3572
Vladimir Marko58155012015-08-19 12:49:41 +00003573void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3574 DCHECK(linker_patches->empty());
3575 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3576 for (const MethodPatchInfo<Label>& info : method_patches_) {
3577 // The label points to the end of the "movl" insn but the literal offset for method
3578 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3579 uint32_t literal_offset = info.label.Position() - 4;
3580 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3581 info.target_method.dex_file,
3582 info.target_method.dex_method_index));
3583 }
3584 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3585 // The label points to the end of the "call" insn but the literal offset for method
3586 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3587 uint32_t literal_offset = info.label.Position() - 4;
3588 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3589 info.target_method.dex_file,
3590 info.target_method.dex_method_index));
3591 }
3592}
3593
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003594void CodeGeneratorX86::MarkGCCard(Register temp,
3595 Register card,
3596 Register object,
3597 Register value,
3598 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003599 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003600 if (value_can_be_null) {
3601 __ testl(value, value);
3602 __ j(kEqual, &is_null);
3603 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003604 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3605 __ movl(temp, object);
3606 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003607 __ movb(Address(temp, card, TIMES_1, 0),
3608 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003609 if (value_can_be_null) {
3610 __ Bind(&is_null);
3611 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003612}
3613
Calin Juravle52c48962014-12-16 17:02:57 +00003614void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3615 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003616 LocationSummary* locations =
3617 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003618 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003619
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003620 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3621 locations->SetOut(Location::RequiresFpuRegister());
3622 } else {
3623 // The output overlaps in case of long: we don't want the low move to overwrite
3624 // the object's location.
3625 locations->SetOut(Location::RequiresRegister(),
3626 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3627 : Location::kNoOutputOverlap);
3628 }
Calin Juravle52c48962014-12-16 17:02:57 +00003629
3630 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3631 // Long values can be loaded atomically into an XMM using movsd.
3632 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3633 // and then copy the XMM into the output 32bits at a time).
3634 locations->AddTemp(Location::RequiresFpuRegister());
3635 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003636}
3637
Calin Juravle52c48962014-12-16 17:02:57 +00003638void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3639 const FieldInfo& field_info) {
3640 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003641
Calin Juravle52c48962014-12-16 17:02:57 +00003642 LocationSummary* locations = instruction->GetLocations();
3643 Register base = locations->InAt(0).AsRegister<Register>();
3644 Location out = locations->Out();
3645 bool is_volatile = field_info.IsVolatile();
3646 Primitive::Type field_type = field_info.GetFieldType();
3647 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3648
3649 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003650 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003651 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003652 break;
3653 }
3654
3655 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003656 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003657 break;
3658 }
3659
3660 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003661 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003662 break;
3663 }
3664
3665 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003666 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003667 break;
3668 }
3669
3670 case Primitive::kPrimInt:
3671 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003672 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003673 break;
3674 }
3675
3676 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003677 if (is_volatile) {
3678 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3679 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003680 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003681 __ movd(out.AsRegisterPairLow<Register>(), temp);
3682 __ psrlq(temp, Immediate(32));
3683 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3684 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003685 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003686 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003687 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003688 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3689 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003690 break;
3691 }
3692
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003693 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003694 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003695 break;
3696 }
3697
3698 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003699 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003700 break;
3701 }
3702
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003703 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003704 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003705 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003706 }
Calin Juravle52c48962014-12-16 17:02:57 +00003707
Calin Juravle77520bc2015-01-12 18:45:46 +00003708 // Longs are handled in the switch.
3709 if (field_type != Primitive::kPrimLong) {
3710 codegen_->MaybeRecordImplicitNullCheck(instruction);
3711 }
3712
Calin Juravle52c48962014-12-16 17:02:57 +00003713 if (is_volatile) {
3714 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3715 }
Roland Levillain4d027112015-07-01 15:41:14 +01003716
3717 if (field_type == Primitive::kPrimNot) {
3718 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3719 }
Calin Juravle52c48962014-12-16 17:02:57 +00003720}
3721
3722void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3723 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3724
3725 LocationSummary* locations =
3726 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3727 locations->SetInAt(0, Location::RequiresRegister());
3728 bool is_volatile = field_info.IsVolatile();
3729 Primitive::Type field_type = field_info.GetFieldType();
3730 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3731 || (field_type == Primitive::kPrimByte);
3732
3733 // The register allocator does not support multiple
3734 // inputs that die at entry with one in a specific register.
3735 if (is_byte_type) {
3736 // Ensure the value is in a byte register.
3737 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003738 } else if (Primitive::IsFloatingPointType(field_type)) {
3739 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003740 } else {
3741 locations->SetInAt(1, Location::RequiresRegister());
3742 }
Calin Juravle52c48962014-12-16 17:02:57 +00003743 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003744 // Temporary registers for the write barrier.
3745 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003746 // Ensure the card is in a byte register.
3747 locations->AddTemp(Location::RegisterLocation(ECX));
3748 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3749 // 64bits value can be atomically written to an address with movsd and an XMM register.
3750 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3751 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3752 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3753 // isolated cases when we need this it isn't worth adding the extra complexity.
3754 locations->AddTemp(Location::RequiresFpuRegister());
3755 locations->AddTemp(Location::RequiresFpuRegister());
3756 }
3757}
3758
3759void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003760 const FieldInfo& field_info,
3761 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003762 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3763
3764 LocationSummary* locations = instruction->GetLocations();
3765 Register base = locations->InAt(0).AsRegister<Register>();
3766 Location value = locations->InAt(1);
3767 bool is_volatile = field_info.IsVolatile();
3768 Primitive::Type field_type = field_info.GetFieldType();
3769 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003770 bool needs_write_barrier =
3771 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003772
3773 if (is_volatile) {
3774 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3775 }
3776
3777 switch (field_type) {
3778 case Primitive::kPrimBoolean:
3779 case Primitive::kPrimByte: {
3780 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3781 break;
3782 }
3783
3784 case Primitive::kPrimShort:
3785 case Primitive::kPrimChar: {
3786 __ movw(Address(base, offset), value.AsRegister<Register>());
3787 break;
3788 }
3789
3790 case Primitive::kPrimInt:
3791 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003792 if (kPoisonHeapReferences && needs_write_barrier) {
3793 // Note that in the case where `value` is a null reference,
3794 // we do not enter this block, as the reference does not
3795 // need poisoning.
3796 DCHECK_EQ(field_type, Primitive::kPrimNot);
3797 Register temp = locations->GetTemp(0).AsRegister<Register>();
3798 __ movl(temp, value.AsRegister<Register>());
3799 __ PoisonHeapReference(temp);
3800 __ movl(Address(base, offset), temp);
3801 } else {
3802 __ movl(Address(base, offset), value.AsRegister<Register>());
3803 }
Calin Juravle52c48962014-12-16 17:02:57 +00003804 break;
3805 }
3806
3807 case Primitive::kPrimLong: {
3808 if (is_volatile) {
3809 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3810 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3811 __ movd(temp1, value.AsRegisterPairLow<Register>());
3812 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3813 __ punpckldq(temp1, temp2);
3814 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003815 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003816 } else {
3817 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003818 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003819 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3820 }
3821 break;
3822 }
3823
3824 case Primitive::kPrimFloat: {
3825 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3826 break;
3827 }
3828
3829 case Primitive::kPrimDouble: {
3830 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3831 break;
3832 }
3833
3834 case Primitive::kPrimVoid:
3835 LOG(FATAL) << "Unreachable type " << field_type;
3836 UNREACHABLE();
3837 }
3838
Calin Juravle77520bc2015-01-12 18:45:46 +00003839 // Longs are handled in the switch.
3840 if (field_type != Primitive::kPrimLong) {
3841 codegen_->MaybeRecordImplicitNullCheck(instruction);
3842 }
3843
Roland Levillain4d027112015-07-01 15:41:14 +01003844 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003845 Register temp = locations->GetTemp(0).AsRegister<Register>();
3846 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003847 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003848 }
3849
Calin Juravle52c48962014-12-16 17:02:57 +00003850 if (is_volatile) {
3851 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3852 }
3853}
3854
3855void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3856 HandleFieldGet(instruction, instruction->GetFieldInfo());
3857}
3858
3859void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3860 HandleFieldGet(instruction, instruction->GetFieldInfo());
3861}
3862
3863void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3864 HandleFieldSet(instruction, instruction->GetFieldInfo());
3865}
3866
3867void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003868 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003869}
3870
3871void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3872 HandleFieldSet(instruction, instruction->GetFieldInfo());
3873}
3874
3875void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003876 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003877}
3878
3879void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3880 HandleFieldGet(instruction, instruction->GetFieldInfo());
3881}
3882
3883void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3884 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003885}
3886
3887void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003888 LocationSummary* locations =
3889 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003890 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3891 ? Location::RequiresRegister()
3892 : Location::Any();
3893 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003894 if (instruction->HasUses()) {
3895 locations->SetOut(Location::SameAsFirstInput());
3896 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003897}
3898
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003899void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003900 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3901 return;
3902 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003903 LocationSummary* locations = instruction->GetLocations();
3904 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003905
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003906 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3907 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3908}
3909
3910void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003911 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003912 codegen_->AddSlowPath(slow_path);
3913
3914 LocationSummary* locations = instruction->GetLocations();
3915 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003916
3917 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003918 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003919 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003920 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003921 } else {
3922 DCHECK(obj.IsConstant()) << obj;
3923 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3924 __ jmp(slow_path->GetEntryLabel());
3925 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003926 }
3927 __ j(kEqual, slow_path->GetEntryLabel());
3928}
3929
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003930void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3931 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3932 GenerateImplicitNullCheck(instruction);
3933 } else {
3934 GenerateExplicitNullCheck(instruction);
3935 }
3936}
3937
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003938void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003939 LocationSummary* locations =
3940 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003941 locations->SetInAt(0, Location::RequiresRegister());
3942 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003943 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3944 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3945 } else {
3946 // The output overlaps in case of long: we don't want the low move to overwrite
3947 // the array's location.
3948 locations->SetOut(Location::RequiresRegister(),
3949 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3950 : Location::kNoOutputOverlap);
3951 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003952}
3953
3954void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3955 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003956 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003957 Location index = locations->InAt(1);
3958
Calin Juravle77520bc2015-01-12 18:45:46 +00003959 Primitive::Type type = instruction->GetType();
3960 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003961 case Primitive::kPrimBoolean: {
3962 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003963 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003964 if (index.IsConstant()) {
3965 __ movzxb(out, Address(obj,
3966 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3967 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003968 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003969 }
3970 break;
3971 }
3972
3973 case Primitive::kPrimByte: {
3974 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003975 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003976 if (index.IsConstant()) {
3977 __ movsxb(out, Address(obj,
3978 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3979 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003980 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003981 }
3982 break;
3983 }
3984
3985 case Primitive::kPrimShort: {
3986 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003987 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003988 if (index.IsConstant()) {
3989 __ movsxw(out, Address(obj,
3990 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3991 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003992 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003993 }
3994 break;
3995 }
3996
3997 case Primitive::kPrimChar: {
3998 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003999 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004000 if (index.IsConstant()) {
4001 __ movzxw(out, Address(obj,
4002 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4003 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004004 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004005 }
4006 break;
4007 }
4008
4009 case Primitive::kPrimInt:
4010 case Primitive::kPrimNot: {
4011 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004012 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004013 if (index.IsConstant()) {
4014 __ movl(out, Address(obj,
4015 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4016 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004017 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004018 }
4019 break;
4020 }
4021
4022 case Primitive::kPrimLong: {
4023 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004024 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004025 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004026 if (index.IsConstant()) {
4027 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004028 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004029 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004030 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004031 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004032 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004033 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004034 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004035 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004036 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004037 }
4038 break;
4039 }
4040
Mark Mendell7c8d0092015-01-26 11:21:33 -05004041 case Primitive::kPrimFloat: {
4042 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4043 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4044 if (index.IsConstant()) {
4045 __ movss(out, Address(obj,
4046 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4047 } else {
4048 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4049 }
4050 break;
4051 }
4052
4053 case Primitive::kPrimDouble: {
4054 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4055 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4056 if (index.IsConstant()) {
4057 __ movsd(out, Address(obj,
4058 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4059 } else {
4060 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4061 }
4062 break;
4063 }
4064
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004065 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004066 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004067 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004068 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004069
4070 if (type != Primitive::kPrimLong) {
4071 codegen_->MaybeRecordImplicitNullCheck(instruction);
4072 }
Roland Levillain4d027112015-07-01 15:41:14 +01004073
4074 if (type == Primitive::kPrimNot) {
4075 Register out = locations->Out().AsRegister<Register>();
4076 __ MaybeUnpoisonHeapReference(out);
4077 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004078}
4079
4080void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004081 // This location builder might end up asking to up to four registers, which is
4082 // not currently possible for baseline. The situation in which we need four
4083 // registers cannot be met by baseline though, because it has not run any
4084 // optimization.
4085
Nicolas Geoffray39468442014-09-02 15:17:15 +01004086 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004087 bool needs_write_barrier =
4088 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4089
Mark Mendell5f874182015-03-04 15:42:45 -05004090 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004091
Nicolas Geoffray39468442014-09-02 15:17:15 +01004092 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4093 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004094 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004095
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004096 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004097 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004098 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4099 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4100 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004101 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004102 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4103 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004104 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004105 // In case of a byte operation, the register allocator does not support multiple
4106 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004107 locations->SetInAt(0, Location::RequiresRegister());
4108 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004109 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004110 // Ensure the value is in a byte register.
4111 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004112 } else if (Primitive::IsFloatingPointType(value_type)) {
4113 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004114 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004115 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004116 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004117 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004118 // Temporary registers for the write barrier.
4119 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004120 // Ensure the card is in a byte register.
4121 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004122 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004123 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004124}
4125
4126void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4127 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004128 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004129 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004130 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004131 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004132 bool needs_runtime_call = locations->WillCall();
4133 bool needs_write_barrier =
4134 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004135
4136 switch (value_type) {
4137 case Primitive::kPrimBoolean:
4138 case Primitive::kPrimByte: {
4139 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004140 if (index.IsConstant()) {
4141 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004142 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004143 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004144 } else {
4145 __ movb(Address(obj, offset),
4146 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4147 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004148 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004149 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004150 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004151 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004152 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004153 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004154 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4155 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004156 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004157 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004158 break;
4159 }
4160
4161 case Primitive::kPrimShort:
4162 case Primitive::kPrimChar: {
4163 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004164 if (index.IsConstant()) {
4165 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004166 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004167 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004168 } else {
4169 __ movw(Address(obj, offset),
4170 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4171 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004172 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004173 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004174 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4175 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004176 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004177 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004178 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4179 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004180 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004181 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004182 break;
4183 }
4184
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004185 case Primitive::kPrimInt:
4186 case Primitive::kPrimNot: {
4187 if (!needs_runtime_call) {
4188 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4189 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004190 size_t offset =
4191 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004192 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004193 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4194 Register temp = locations->GetTemp(0).AsRegister<Register>();
4195 __ movl(temp, value.AsRegister<Register>());
4196 __ PoisonHeapReference(temp);
4197 __ movl(Address(obj, offset), temp);
4198 } else {
4199 __ movl(Address(obj, offset), value.AsRegister<Register>());
4200 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004201 } else {
4202 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004203 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4204 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4205 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4206 // Note: if heap poisoning is enabled, no need to poison
4207 // (negate) `v` if it is a reference, as it would be null.
4208 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004209 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004210 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004211 DCHECK(index.IsRegister()) << index;
4212 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004213 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4214 Register temp = locations->GetTemp(0).AsRegister<Register>();
4215 __ movl(temp, value.AsRegister<Register>());
4216 __ PoisonHeapReference(temp);
4217 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4218 } else {
4219 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4220 value.AsRegister<Register>());
4221 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004222 } else {
4223 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004224 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4225 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4226 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4227 // Note: if heap poisoning is enabled, no need to poison
4228 // (negate) `v` if it is a reference, as it would be null.
4229 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004230 }
4231 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004232 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004233
4234 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004235 Register temp = locations->GetTemp(0).AsRegister<Register>();
4236 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004237 codegen_->MarkGCCard(
4238 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004239 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004240 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004241 DCHECK_EQ(value_type, Primitive::kPrimNot);
4242 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004243 // Note: if heap poisoning is enabled, pAputObject takes cares
4244 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004245 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4246 instruction,
4247 instruction->GetDexPc(),
4248 nullptr);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004249 }
4250 break;
4251 }
4252
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004253 case Primitive::kPrimLong: {
4254 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004255 if (index.IsConstant()) {
4256 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004257 if (value.IsRegisterPair()) {
4258 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004259 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004260 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004261 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004262 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004263 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4264 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004265 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004266 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4267 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004268 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004269 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004270 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004271 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004272 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004273 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004274 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004275 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004276 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004277 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004278 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004279 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004280 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004281 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004282 Immediate(High32Bits(val)));
4283 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004284 }
4285 break;
4286 }
4287
Mark Mendell7c8d0092015-01-26 11:21:33 -05004288 case Primitive::kPrimFloat: {
4289 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4290 DCHECK(value.IsFpuRegister());
4291 if (index.IsConstant()) {
4292 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4293 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4294 } else {
4295 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4296 value.AsFpuRegister<XmmRegister>());
4297 }
4298 break;
4299 }
4300
4301 case Primitive::kPrimDouble: {
4302 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4303 DCHECK(value.IsFpuRegister());
4304 if (index.IsConstant()) {
4305 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4306 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4307 } else {
4308 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4309 value.AsFpuRegister<XmmRegister>());
4310 }
4311 break;
4312 }
4313
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004314 case Primitive::kPrimVoid:
4315 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004316 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004317 }
4318}
4319
4320void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4321 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004322 locations->SetInAt(0, Location::RequiresRegister());
4323 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004324}
4325
4326void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4327 LocationSummary* locations = instruction->GetLocations();
4328 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004329 Register obj = locations->InAt(0).AsRegister<Register>();
4330 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004331 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004332 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004333}
4334
4335void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004336 LocationSummary* locations =
4337 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004338 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004339 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004340 if (instruction->HasUses()) {
4341 locations->SetOut(Location::SameAsFirstInput());
4342 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004343}
4344
4345void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4346 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004347 Location index_loc = locations->InAt(0);
4348 Location length_loc = locations->InAt(1);
4349 SlowPathCodeX86* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004350 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004351
Mark Mendell99dbd682015-04-22 16:18:52 -04004352 if (length_loc.IsConstant()) {
4353 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4354 if (index_loc.IsConstant()) {
4355 // BCE will remove the bounds check if we are guarenteed to pass.
4356 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4357 if (index < 0 || index >= length) {
4358 codegen_->AddSlowPath(slow_path);
4359 __ jmp(slow_path->GetEntryLabel());
4360 } else {
4361 // Some optimization after BCE may have generated this, and we should not
4362 // generate a bounds check if it is a valid range.
4363 }
4364 return;
4365 }
4366
4367 // We have to reverse the jump condition because the length is the constant.
4368 Register index_reg = index_loc.AsRegister<Register>();
4369 __ cmpl(index_reg, Immediate(length));
4370 codegen_->AddSlowPath(slow_path);
4371 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004372 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004373 Register length = length_loc.AsRegister<Register>();
4374 if (index_loc.IsConstant()) {
4375 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4376 __ cmpl(length, Immediate(value));
4377 } else {
4378 __ cmpl(length, index_loc.AsRegister<Register>());
4379 }
4380 codegen_->AddSlowPath(slow_path);
4381 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004382 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004383}
4384
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004385void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4386 temp->SetLocations(nullptr);
4387}
4388
4389void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4390 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004391 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004392}
4393
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004394void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004395 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004396 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004397}
4398
4399void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004400 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4401}
4402
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004403void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4404 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4405}
4406
4407void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004408 HBasicBlock* block = instruction->GetBlock();
4409 if (block->GetLoopInformation() != nullptr) {
4410 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4411 // The back edge will generate the suspend check.
4412 return;
4413 }
4414 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4415 // The goto will generate the suspend check.
4416 return;
4417 }
4418 GenerateSuspendCheck(instruction, nullptr);
4419}
4420
4421void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4422 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004423 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004424 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4425 if (slow_path == nullptr) {
4426 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4427 instruction->SetSlowPath(slow_path);
4428 codegen_->AddSlowPath(slow_path);
4429 if (successor != nullptr) {
4430 DCHECK(successor->IsLoopHeader());
4431 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4432 }
4433 } else {
4434 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4435 }
4436
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004437 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004438 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004439 if (successor == nullptr) {
4440 __ j(kNotEqual, slow_path->GetEntryLabel());
4441 __ Bind(slow_path->GetReturnLabel());
4442 } else {
4443 __ j(kEqual, codegen_->GetLabelOf(successor));
4444 __ jmp(slow_path->GetEntryLabel());
4445 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004446}
4447
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004448X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4449 return codegen_->GetAssembler();
4450}
4451
Mark Mendell7c8d0092015-01-26 11:21:33 -05004452void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004453 ScratchRegisterScope ensure_scratch(
4454 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4455 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4456 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4457 __ movl(temp_reg, Address(ESP, src + stack_offset));
4458 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004459}
4460
4461void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004462 ScratchRegisterScope ensure_scratch(
4463 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4464 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4465 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4466 __ movl(temp_reg, Address(ESP, src + stack_offset));
4467 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4468 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4469 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004470}
4471
4472void ParallelMoveResolverX86::EmitMove(size_t index) {
4473 MoveOperands* move = moves_.Get(index);
4474 Location source = move->GetSource();
4475 Location destination = move->GetDestination();
4476
4477 if (source.IsRegister()) {
4478 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004479 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004480 } else {
4481 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004482 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004483 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004484 } else if (source.IsFpuRegister()) {
4485 if (destination.IsFpuRegister()) {
4486 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4487 } else if (destination.IsStackSlot()) {
4488 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4489 } else {
4490 DCHECK(destination.IsDoubleStackSlot());
4491 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4492 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004493 } else if (source.IsStackSlot()) {
4494 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004495 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004496 } else if (destination.IsFpuRegister()) {
4497 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004498 } else {
4499 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004500 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4501 }
4502 } else if (source.IsDoubleStackSlot()) {
4503 if (destination.IsFpuRegister()) {
4504 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4505 } else {
4506 DCHECK(destination.IsDoubleStackSlot()) << destination;
4507 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004508 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004509 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004510 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004511 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004512 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004513 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004514 if (value == 0) {
4515 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4516 } else {
4517 __ movl(destination.AsRegister<Register>(), Immediate(value));
4518 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004519 } else {
4520 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004521 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004522 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004523 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004524 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004525 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004526 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004527 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004528 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4529 if (value == 0) {
4530 // Easy handling of 0.0.
4531 __ xorps(dest, dest);
4532 } else {
4533 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004534 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4535 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4536 __ movl(temp, Immediate(value));
4537 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004538 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004539 } else {
4540 DCHECK(destination.IsStackSlot()) << destination;
4541 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4542 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004543 } else if (constant->IsLongConstant()) {
4544 int64_t value = constant->AsLongConstant()->GetValue();
4545 int32_t low_value = Low32Bits(value);
4546 int32_t high_value = High32Bits(value);
4547 Immediate low(low_value);
4548 Immediate high(high_value);
4549 if (destination.IsDoubleStackSlot()) {
4550 __ movl(Address(ESP, destination.GetStackIndex()), low);
4551 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4552 } else {
4553 __ movl(destination.AsRegisterPairLow<Register>(), low);
4554 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4555 }
4556 } else {
4557 DCHECK(constant->IsDoubleConstant());
4558 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004559 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004560 int32_t low_value = Low32Bits(value);
4561 int32_t high_value = High32Bits(value);
4562 Immediate low(low_value);
4563 Immediate high(high_value);
4564 if (destination.IsFpuRegister()) {
4565 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4566 if (value == 0) {
4567 // Easy handling of 0.0.
4568 __ xorpd(dest, dest);
4569 } else {
4570 __ pushl(high);
4571 __ pushl(low);
4572 __ movsd(dest, Address(ESP, 0));
4573 __ addl(ESP, Immediate(8));
4574 }
4575 } else {
4576 DCHECK(destination.IsDoubleStackSlot()) << destination;
4577 __ movl(Address(ESP, destination.GetStackIndex()), low);
4578 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4579 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004580 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004581 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004582 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004583 }
4584}
4585
Mark Mendella5c19ce2015-04-01 12:51:05 -04004586void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004587 Register suggested_scratch = reg == EAX ? EBX : EAX;
4588 ScratchRegisterScope ensure_scratch(
4589 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4590
4591 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4592 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4593 __ movl(Address(ESP, mem + stack_offset), reg);
4594 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004595}
4596
Mark Mendell7c8d0092015-01-26 11:21:33 -05004597void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004598 ScratchRegisterScope ensure_scratch(
4599 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4600
4601 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4602 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4603 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4604 __ movss(Address(ESP, mem + stack_offset), reg);
4605 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004606}
4607
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004608void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004609 ScratchRegisterScope ensure_scratch1(
4610 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004611
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004612 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4613 ScratchRegisterScope ensure_scratch2(
4614 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004615
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004616 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4617 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4618 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4619 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4620 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4621 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004622}
4623
4624void ParallelMoveResolverX86::EmitSwap(size_t index) {
4625 MoveOperands* move = moves_.Get(index);
4626 Location source = move->GetSource();
4627 Location destination = move->GetDestination();
4628
4629 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004630 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4631 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4632 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4633 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4634 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004635 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004636 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004637 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004638 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004639 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4640 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004641 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4642 // Use XOR Swap algorithm to avoid a temporary.
4643 DCHECK_NE(source.reg(), destination.reg());
4644 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4645 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4646 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4647 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4648 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4649 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4650 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004651 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4652 // Take advantage of the 16 bytes in the XMM register.
4653 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4654 Address stack(ESP, destination.GetStackIndex());
4655 // Load the double into the high doubleword.
4656 __ movhpd(reg, stack);
4657
4658 // Store the low double into the destination.
4659 __ movsd(stack, reg);
4660
4661 // Move the high double to the low double.
4662 __ psrldq(reg, Immediate(8));
4663 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4664 // Take advantage of the 16 bytes in the XMM register.
4665 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4666 Address stack(ESP, source.GetStackIndex());
4667 // Load the double into the high doubleword.
4668 __ movhpd(reg, stack);
4669
4670 // Store the low double into the destination.
4671 __ movsd(stack, reg);
4672
4673 // Move the high double to the low double.
4674 __ psrldq(reg, Immediate(8));
4675 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4676 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4677 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004678 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004679 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004680 }
4681}
4682
4683void ParallelMoveResolverX86::SpillScratch(int reg) {
4684 __ pushl(static_cast<Register>(reg));
4685}
4686
4687void ParallelMoveResolverX86::RestoreScratch(int reg) {
4688 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004689}
4690
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004691void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004692 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4693 ? LocationSummary::kCallOnSlowPath
4694 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004695 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004696 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004697 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004698 locations->SetOut(Location::RequiresRegister());
4699}
4700
4701void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004702 LocationSummary* locations = cls->GetLocations();
4703 Register out = locations->Out().AsRegister<Register>();
4704 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004705 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004706 DCHECK(!cls->CanCallRuntime());
4707 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004708 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004709 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004710 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004711 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01004712 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004713 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004714 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004715
4716 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4717 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4718 codegen_->AddSlowPath(slow_path);
4719 __ testl(out, out);
4720 __ j(kEqual, slow_path->GetEntryLabel());
4721 if (cls->MustGenerateClinitCheck()) {
4722 GenerateClassInitializationCheck(slow_path, out);
4723 } else {
4724 __ Bind(slow_path->GetExitLabel());
4725 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004726 }
4727}
4728
4729void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4730 LocationSummary* locations =
4731 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4732 locations->SetInAt(0, Location::RequiresRegister());
4733 if (check->HasUses()) {
4734 locations->SetOut(Location::SameAsFirstInput());
4735 }
4736}
4737
4738void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004739 // We assume the class to not be null.
4740 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4741 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004742 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004743 GenerateClassInitializationCheck(slow_path,
4744 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004745}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004746
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004747void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4748 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004749 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4750 Immediate(mirror::Class::kStatusInitialized));
4751 __ j(kLess, slow_path->GetEntryLabel());
4752 __ Bind(slow_path->GetExitLabel());
4753 // No need for memory fence, thanks to the X86 memory model.
4754}
4755
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004756void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4757 LocationSummary* locations =
4758 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004759 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004760 locations->SetOut(Location::RequiresRegister());
4761}
4762
4763void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4764 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4765 codegen_->AddSlowPath(slow_path);
4766
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004767 LocationSummary* locations = load->GetLocations();
4768 Register out = locations->Out().AsRegister<Register>();
4769 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004770 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004771 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004772 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004773 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004774 __ testl(out, out);
4775 __ j(kEqual, slow_path->GetEntryLabel());
4776 __ Bind(slow_path->GetExitLabel());
4777}
4778
David Brazdilcb1c0552015-08-04 16:22:25 +01004779static Address GetExceptionTlsAddress() {
4780 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
4781}
4782
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004783void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4784 LocationSummary* locations =
4785 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4786 locations->SetOut(Location::RequiresRegister());
4787}
4788
4789void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004790 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
4791}
4792
4793void LocationsBuilderX86::VisitClearException(HClearException* clear) {
4794 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4795}
4796
4797void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4798 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004799}
4800
4801void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4802 LocationSummary* locations =
4803 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4804 InvokeRuntimeCallingConvention calling_convention;
4805 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4806}
4807
4808void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004809 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4810 instruction,
4811 instruction->GetDexPc(),
4812 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004813}
4814
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004815void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004816 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4817 ? LocationSummary::kNoCall
4818 : LocationSummary::kCallOnSlowPath;
4819 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4820 locations->SetInAt(0, Location::RequiresRegister());
4821 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004822 // Note that TypeCheckSlowPathX86 uses this register too.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004823 locations->SetOut(Location::RequiresRegister());
4824}
4825
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004826void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004827 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004828 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004829 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004830 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004831 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4832 Label done, zero;
4833 SlowPathCodeX86* slow_path = nullptr;
4834
4835 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004836 // Avoid null check if we know obj is not null.
4837 if (instruction->MustDoNullCheck()) {
4838 __ testl(obj, obj);
4839 __ j(kEqual, &zero);
4840 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004841 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004842 __ movl(out, Address(obj, class_offset));
4843 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004844 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004845 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004846 } else {
4847 DCHECK(cls.IsStackSlot()) << cls;
4848 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4849 }
4850
4851 if (instruction->IsClassFinal()) {
4852 // Classes must be equal for the instanceof to succeed.
4853 __ j(kNotEqual, &zero);
4854 __ movl(out, Immediate(1));
4855 __ jmp(&done);
4856 } else {
4857 // If the classes are not equal, we go into a slow path.
4858 DCHECK(locations->OnlyCallsOnSlowPath());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004859 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004860 codegen_->AddSlowPath(slow_path);
4861 __ j(kNotEqual, slow_path->GetEntryLabel());
4862 __ movl(out, Immediate(1));
4863 __ jmp(&done);
4864 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004865
4866 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4867 __ Bind(&zero);
4868 __ movl(out, Immediate(0));
4869 }
4870
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004871 if (slow_path != nullptr) {
4872 __ Bind(slow_path->GetExitLabel());
4873 }
4874 __ Bind(&done);
4875}
4876
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004877void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4878 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4879 instruction, LocationSummary::kCallOnSlowPath);
4880 locations->SetInAt(0, Location::RequiresRegister());
4881 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004882 // Note that TypeCheckSlowPathX86 uses this register too.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004883 locations->AddTemp(Location::RequiresRegister());
4884}
4885
4886void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4887 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004888 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004889 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004890 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004891 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004892 SlowPathCodeX86* slow_path =
4893 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004894 codegen_->AddSlowPath(slow_path);
4895
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004896 // Avoid null check if we know obj is not null.
4897 if (instruction->MustDoNullCheck()) {
4898 __ testl(obj, obj);
4899 __ j(kEqual, slow_path->GetExitLabel());
4900 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004901 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004902 __ movl(temp, Address(obj, class_offset));
4903 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004904 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004905 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004906 } else {
4907 DCHECK(cls.IsStackSlot()) << cls;
4908 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4909 }
Roland Levillain4d027112015-07-01 15:41:14 +01004910 // The checkcast succeeds if the classes are equal (fast path).
4911 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004912 __ j(kNotEqual, slow_path->GetEntryLabel());
4913 __ Bind(slow_path->GetExitLabel());
4914}
4915
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004916void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4917 LocationSummary* locations =
4918 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4919 InvokeRuntimeCallingConvention calling_convention;
4920 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4921}
4922
4923void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004924 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4925 : QUICK_ENTRY_POINT(pUnlockObject),
4926 instruction,
4927 instruction->GetDexPc(),
4928 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004929}
4930
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004931void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4932void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4933void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4934
4935void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4936 LocationSummary* locations =
4937 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4938 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4939 || instruction->GetResultType() == Primitive::kPrimLong);
4940 locations->SetInAt(0, Location::RequiresRegister());
4941 locations->SetInAt(1, Location::Any());
4942 locations->SetOut(Location::SameAsFirstInput());
4943}
4944
4945void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4946 HandleBitwiseOperation(instruction);
4947}
4948
4949void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4950 HandleBitwiseOperation(instruction);
4951}
4952
4953void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4954 HandleBitwiseOperation(instruction);
4955}
4956
4957void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4958 LocationSummary* locations = instruction->GetLocations();
4959 Location first = locations->InAt(0);
4960 Location second = locations->InAt(1);
4961 DCHECK(first.Equals(locations->Out()));
4962
4963 if (instruction->GetResultType() == Primitive::kPrimInt) {
4964 if (second.IsRegister()) {
4965 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004966 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004967 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004968 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004969 } else {
4970 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004971 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004972 }
4973 } else if (second.IsConstant()) {
4974 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004975 __ andl(first.AsRegister<Register>(),
4976 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004977 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004978 __ orl(first.AsRegister<Register>(),
4979 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004980 } else {
4981 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004982 __ xorl(first.AsRegister<Register>(),
4983 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004984 }
4985 } else {
4986 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004987 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004988 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004989 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004990 } else {
4991 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004992 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004993 }
4994 }
4995 } else {
4996 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4997 if (second.IsRegisterPair()) {
4998 if (instruction->IsAnd()) {
4999 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5000 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5001 } else if (instruction->IsOr()) {
5002 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5003 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5004 } else {
5005 DCHECK(instruction->IsXor());
5006 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5007 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5008 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005009 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005010 if (instruction->IsAnd()) {
5011 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5012 __ andl(first.AsRegisterPairHigh<Register>(),
5013 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5014 } else if (instruction->IsOr()) {
5015 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5016 __ orl(first.AsRegisterPairHigh<Register>(),
5017 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5018 } else {
5019 DCHECK(instruction->IsXor());
5020 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5021 __ xorl(first.AsRegisterPairHigh<Register>(),
5022 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5023 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005024 } else {
5025 DCHECK(second.IsConstant()) << second;
5026 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005027 int32_t low_value = Low32Bits(value);
5028 int32_t high_value = High32Bits(value);
5029 Immediate low(low_value);
5030 Immediate high(high_value);
5031 Register first_low = first.AsRegisterPairLow<Register>();
5032 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005033 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005034 if (low_value == 0) {
5035 __ xorl(first_low, first_low);
5036 } else if (low_value != -1) {
5037 __ andl(first_low, low);
5038 }
5039 if (high_value == 0) {
5040 __ xorl(first_high, first_high);
5041 } else if (high_value != -1) {
5042 __ andl(first_high, high);
5043 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005044 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005045 if (low_value != 0) {
5046 __ orl(first_low, low);
5047 }
5048 if (high_value != 0) {
5049 __ orl(first_high, high);
5050 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005051 } else {
5052 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005053 if (low_value != 0) {
5054 __ xorl(first_low, low);
5055 }
5056 if (high_value != 0) {
5057 __ xorl(first_high, high);
5058 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005059 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005060 }
5061 }
5062}
5063
Calin Juravleb1498f62015-02-16 13:13:29 +00005064void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5065 // Nothing to do, this should be removed during prepare for register allocator.
5066 UNUSED(instruction);
5067 LOG(FATAL) << "Unreachable";
5068}
5069
5070void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5071 // Nothing to do, this should be removed during prepare for register allocator.
5072 UNUSED(instruction);
5073 LOG(FATAL) << "Unreachable";
5074}
5075
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005076void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5077 DCHECK(codegen_->IsBaseline());
5078 LocationSummary* locations =
5079 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5080 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5081}
5082
5083void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5084 DCHECK(codegen_->IsBaseline());
5085 // Will be generated at use site.
5086}
5087
Roland Levillain4d027112015-07-01 15:41:14 +01005088#undef __
5089
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005090} // namespace x86
5091} // namespace art