blob: 72c690de9a82bfc71b4e6063707e00e3f1ca4674 [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:
Roland Levillain5799fc02014-09-25 12:15:20 +0100119 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
120 Location index_location,
121 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000122 : instruction_(instruction),
123 index_location_(index_location),
124 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100125
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000126 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100127 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100128 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000129 // We're moving two locations to locations that could overlap, so we need a parallel
130 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100131 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000132 x86_codegen->EmitParallelMoves(
133 index_location_,
134 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100135 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000136 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100137 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
138 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100139 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
140 instruction_,
141 instruction_->GetDexPc(),
142 this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100143 }
144
Alexandre Rames8158f282015-08-07 10:26:17 +0100145 bool IsFatal() const OVERRIDE { return true; }
146
Alexandre Rames9931f312015-06-19 14:47:01 +0100147 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
148
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100150 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100151 const Location index_location_;
152 const Location length_location_;
153
154 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
155};
156
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100157class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000159 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100160 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000161
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000162 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100163 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000165 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100166 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
167 instruction_,
168 instruction_->GetDexPc(),
169 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000170 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100171 if (successor_ == nullptr) {
172 __ jmp(GetReturnLabel());
173 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100174 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100175 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000176 }
177
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100178 Label* GetReturnLabel() {
179 DCHECK(successor_ == nullptr);
180 return &return_label_;
181 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000182
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100183 HBasicBlock* GetSuccessor() const {
184 return successor_;
185 }
186
Alexandre Rames9931f312015-06-19 14:47:01 +0100187 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
188
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000189 private:
190 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100191 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000192 Label return_label_;
193
194 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
195};
196
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000197class LoadStringSlowPathX86 : public SlowPathCodeX86 {
198 public:
199 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
200
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000201 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000202 LocationSummary* locations = instruction_->GetLocations();
203 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
204
205 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
206 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000207 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000208
209 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800210 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100211 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
212 instruction_,
213 instruction_->GetDexPc(),
214 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000215 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000216 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000217
218 __ jmp(GetExitLabel());
219 }
220
Alexandre Rames9931f312015-06-19 14:47:01 +0100221 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
222
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000223 private:
224 HLoadString* const instruction_;
225
226 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
227};
228
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229class LoadClassSlowPathX86 : public SlowPathCodeX86 {
230 public:
231 LoadClassSlowPathX86(HLoadClass* cls,
232 HInstruction* at,
233 uint32_t dex_pc,
234 bool do_clinit)
235 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
236 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
237 }
238
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000239 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 LocationSummary* locations = at_->GetLocations();
241 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
242 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000243 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244
245 InvokeRuntimeCallingConvention calling_convention;
246 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100247 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
248 : QUICK_ENTRY_POINT(pInitializeType),
249 at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250
251 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000252 Location out = locations->Out();
253 if (out.IsValid()) {
254 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
255 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000256 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000258 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000259 __ jmp(GetExitLabel());
260 }
261
Alexandre Rames9931f312015-06-19 14:47:01 +0100262 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
263
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000264 private:
265 // The class this slow path will load.
266 HLoadClass* const cls_;
267
268 // The instruction where this slow path is happening.
269 // (Might be the load class or an initialization check).
270 HInstruction* const at_;
271
272 // The dex PC of `at_`.
273 const uint32_t dex_pc_;
274
275 // Whether to initialize the class.
276 const bool do_clinit_;
277
278 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
279};
280
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
282 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000283 TypeCheckSlowPathX86(HInstruction* instruction,
284 Location class_to_check,
Alexandre Rames98596202015-08-19 11:33:36 +0100285 Location object_class)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000287 class_to_check_(class_to_check),
Alexandre Rames98596202015-08-19 11:33:36 +0100288 object_class_(object_class) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000292 DCHECK(instruction_->IsCheckCast()
293 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294
295 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
296 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000297 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298
299 // We're moving two locations to locations that could overlap, so we need a parallel
300 // move resolver.
301 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000302 x86_codegen->EmitParallelMoves(
303 class_to_check_,
304 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100305 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000306 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100307 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
308 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000309
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000310 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100311 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
312 instruction_,
313 instruction_->GetDexPc(),
314 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000315 } else {
316 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100317 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
318 instruction_,
319 instruction_->GetDexPc(),
320 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000321 }
322
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 if (instruction_->IsInstanceOf()) {
324 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
325 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000326 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327
328 __ jmp(GetExitLabel());
329 }
330
Alexandre Rames9931f312015-06-19 14:47:01 +0100331 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
332
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000334 HInstruction* const instruction_;
335 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336 const Location object_class_;
337
338 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
339};
340
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700341class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
342 public:
343 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
344 : instruction_(instruction) {}
345
346 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames98596202015-08-19 11:33:36 +0100347 DCHECK(instruction_->IsDeoptimize());
Alexandre Rames8158f282015-08-07 10:26:17 +0100348 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700349 __ Bind(GetEntryLabel());
350 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100351 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
352 instruction_,
353 instruction_->GetDexPc(),
354 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700355 }
356
Alexandre Rames9931f312015-06-19 14:47:01 +0100357 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
358
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700359 private:
360 HInstruction* const instruction_;
361 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
362};
363
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100364#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100365#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100366
Roland Levillain4fa13f62015-07-06 18:11:54 +0100367inline Condition X86SignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700368 switch (cond) {
369 case kCondEQ: return kEqual;
370 case kCondNE: return kNotEqual;
371 case kCondLT: return kLess;
372 case kCondLE: return kLessEqual;
373 case kCondGT: return kGreater;
374 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700375 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100376 LOG(FATAL) << "Unreachable";
377 UNREACHABLE();
378}
379
380inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
381 switch (cond) {
382 case kCondEQ: return kEqual;
383 case kCondNE: return kNotEqual;
384 case kCondLT: return kBelow;
385 case kCondLE: return kBelowEqual;
386 case kCondGT: return kAbove;
387 case kCondGE: return kAboveEqual;
388 }
389 LOG(FATAL) << "Unreachable";
390 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700391}
392
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100393void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100394 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100395}
396
397void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100398 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100399}
400
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100401size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
402 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
403 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100404}
405
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100406size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
407 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
408 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100409}
410
Mark Mendell7c8d0092015-01-26 11:21:33 -0500411size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
412 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
413 return GetFloatingPointSpillSlotSize();
414}
415
416size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
417 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
418 return GetFloatingPointSpillSlotSize();
419}
420
Alexandre Rames8158f282015-08-07 10:26:17 +0100421void CodeGeneratorX86::InvokeRuntime(Address entry_point,
422 HInstruction* instruction,
423 uint32_t dex_pc,
424 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100425 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100426 __ fs()->call(entry_point);
427 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100428}
429
Mark Mendellfb8d2792015-03-31 22:16:59 -0400430CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
431 const X86InstructionSetFeatures& isa_features,
432 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500433 : CodeGenerator(graph,
434 kNumberOfCpuRegisters,
435 kNumberOfXmmRegisters,
436 kNumberOfRegisterPairs,
437 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
438 arraysize(kCoreCalleeSaves))
439 | (1 << kFakeReturnRegister),
440 0,
441 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100442 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100443 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100444 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400445 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000446 isa_features_(isa_features),
447 method_patches_(graph->GetArena()->Adapter()),
448 relative_call_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000449 // Use a fake return address register to mimic Quick.
450 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100451}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100452
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100453Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100454 switch (type) {
455 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100456 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100457 X86ManagedRegister pair =
458 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100459 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
460 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100461 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
462 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100463 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100464 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100465 }
466
467 case Primitive::kPrimByte:
468 case Primitive::kPrimBoolean:
469 case Primitive::kPrimChar:
470 case Primitive::kPrimShort:
471 case Primitive::kPrimInt:
472 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100473 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100474 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100475 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100476 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
477 X86ManagedRegister current =
478 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
479 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100480 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100481 }
482 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100483 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100484 }
485
486 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100487 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100488 return Location::FpuRegisterLocation(
489 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100490 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100491
492 case Primitive::kPrimVoid:
493 LOG(FATAL) << "Unreachable type " << type;
494 }
495
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100496 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100497}
498
Mark Mendell5f874182015-03-04 15:42:45 -0500499void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100500 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100501 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100502
503 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100504 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100505
Mark Mendell5f874182015-03-04 15:42:45 -0500506 if (is_baseline) {
507 blocked_core_registers_[EBP] = true;
508 blocked_core_registers_[ESI] = true;
509 blocked_core_registers_[EDI] = true;
510 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100511
512 UpdateBlockedPairRegisters();
513}
514
515void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
516 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
517 X86ManagedRegister current =
518 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
519 if (blocked_core_registers_[current.AsRegisterPairLow()]
520 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
521 blocked_register_pairs_[i] = true;
522 }
523 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100524}
525
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100526InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
527 : HGraphVisitor(graph),
528 assembler_(codegen->GetAssembler()),
529 codegen_(codegen) {}
530
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100531static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100532 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100533}
534
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000535void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100536 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000537 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000538 bool skip_overflow_check =
539 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000540 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000541
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000542 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100543 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100544 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100545 }
546
Mark Mendell5f874182015-03-04 15:42:45 -0500547 if (HasEmptyFrame()) {
548 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000549 }
Mark Mendell5f874182015-03-04 15:42:45 -0500550
551 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
552 Register reg = kCoreCalleeSaves[i];
553 if (allocated_registers_.ContainsCoreRegister(reg)) {
554 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100555 __ cfi().AdjustCFAOffset(kX86WordSize);
556 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500557 }
558 }
559
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100560 int adjust = GetFrameSize() - FrameEntrySpillSize();
561 __ subl(ESP, Immediate(adjust));
562 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100563 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000564}
565
566void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100567 __ cfi().RememberState();
568 if (!HasEmptyFrame()) {
569 int adjust = GetFrameSize() - FrameEntrySpillSize();
570 __ addl(ESP, Immediate(adjust));
571 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500572
David Srbeckyc34dc932015-04-12 09:27:43 +0100573 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
574 Register reg = kCoreCalleeSaves[i];
575 if (allocated_registers_.ContainsCoreRegister(reg)) {
576 __ popl(reg);
577 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
578 __ cfi().Restore(DWARFReg(reg));
579 }
Mark Mendell5f874182015-03-04 15:42:45 -0500580 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000581 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100582 __ ret();
583 __ cfi().RestoreState();
584 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000585}
586
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100587void CodeGeneratorX86::Bind(HBasicBlock* block) {
588 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000589}
590
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100591Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
592 switch (load->GetType()) {
593 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100594 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100595 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100596
597 case Primitive::kPrimInt:
598 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100599 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100600 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100601
602 case Primitive::kPrimBoolean:
603 case Primitive::kPrimByte:
604 case Primitive::kPrimChar:
605 case Primitive::kPrimShort:
606 case Primitive::kPrimVoid:
607 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700608 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100609 }
610
611 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700612 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100613}
614
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100615Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
616 switch (type) {
617 case Primitive::kPrimBoolean:
618 case Primitive::kPrimByte:
619 case Primitive::kPrimChar:
620 case Primitive::kPrimShort:
621 case Primitive::kPrimInt:
622 case Primitive::kPrimNot:
623 return Location::RegisterLocation(EAX);
624
625 case Primitive::kPrimLong:
626 return Location::RegisterPairLocation(EAX, EDX);
627
628 case Primitive::kPrimVoid:
629 return Location::NoLocation();
630
631 case Primitive::kPrimDouble:
632 case Primitive::kPrimFloat:
633 return Location::FpuRegisterLocation(XMM0);
634 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100635
636 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100637}
638
639Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
640 return Location::RegisterLocation(kMethodRegisterArgument);
641}
642
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100643Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100644 switch (type) {
645 case Primitive::kPrimBoolean:
646 case Primitive::kPrimByte:
647 case Primitive::kPrimChar:
648 case Primitive::kPrimShort:
649 case Primitive::kPrimInt:
650 case Primitive::kPrimNot: {
651 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000652 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100653 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100654 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100655 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000656 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100657 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100658 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100659
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000660 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100661 uint32_t index = gp_index_;
662 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000663 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100664 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100665 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
666 calling_convention.GetRegisterPairAt(index));
667 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100668 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000669 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
670 }
671 }
672
673 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100674 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000675 stack_index_++;
676 if (index < calling_convention.GetNumberOfFpuRegisters()) {
677 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
678 } else {
679 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
680 }
681 }
682
683 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100684 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000685 stack_index_ += 2;
686 if (index < calling_convention.GetNumberOfFpuRegisters()) {
687 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
688 } else {
689 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100690 }
691 }
692
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100693 case Primitive::kPrimVoid:
694 LOG(FATAL) << "Unexpected parameter type " << type;
695 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100696 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100697 return Location();
698}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100699
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100700void CodeGeneratorX86::Move32(Location destination, Location source) {
701 if (source.Equals(destination)) {
702 return;
703 }
704 if (destination.IsRegister()) {
705 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000706 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100707 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000708 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100709 } else {
710 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000711 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100712 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100713 } else if (destination.IsFpuRegister()) {
714 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000715 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000717 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100718 } else {
719 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000720 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100722 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000723 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100724 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000725 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100726 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000727 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500728 } else if (source.IsConstant()) {
729 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000730 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500731 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100732 } else {
733 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100734 __ pushl(Address(ESP, source.GetStackIndex()));
735 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100736 }
737 }
738}
739
740void CodeGeneratorX86::Move64(Location destination, Location source) {
741 if (source.Equals(destination)) {
742 return;
743 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100744 if (destination.IsRegisterPair()) {
745 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000746 EmitParallelMoves(
747 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
748 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100749 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000750 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100751 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
752 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100753 } else if (source.IsFpuRegister()) {
754 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100755 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000756 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100757 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100758 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
759 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
761 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100762 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500763 if (source.IsFpuRegister()) {
764 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
765 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000766 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100767 } else {
768 LOG(FATAL) << "Unimplemented";
769 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100770 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000771 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100772 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000773 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100774 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100775 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100776 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100777 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000778 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000779 } else if (source.IsConstant()) {
780 HConstant* constant = source.GetConstant();
781 int64_t value;
782 if (constant->IsLongConstant()) {
783 value = constant->AsLongConstant()->GetValue();
784 } else {
785 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000786 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000787 }
788 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
789 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100790 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000791 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000792 EmitParallelMoves(
793 Location::StackSlot(source.GetStackIndex()),
794 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100795 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000796 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100797 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
798 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100799 }
800 }
801}
802
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100803void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000804 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100805 if (instruction->IsCurrentMethod()) {
806 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
807 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000808 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100809 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000810 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000811 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
812 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000813 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000814 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000815 } else if (location.IsStackSlot()) {
816 __ movl(Address(ESP, location.GetStackIndex()), imm);
817 } else {
818 DCHECK(location.IsConstant());
819 DCHECK_EQ(location.GetConstant(), const_to_move);
820 }
821 } else if (const_to_move->IsLongConstant()) {
822 int64_t value = const_to_move->AsLongConstant()->GetValue();
823 if (location.IsRegisterPair()) {
824 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
825 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
826 } else if (location.IsDoubleStackSlot()) {
827 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000828 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
829 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000830 } else {
831 DCHECK(location.IsConstant());
832 DCHECK_EQ(location.GetConstant(), instruction);
833 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100834 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000835 } else if (instruction->IsTemporary()) {
836 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000837 if (temp_location.IsStackSlot()) {
838 Move32(location, temp_location);
839 } else {
840 DCHECK(temp_location.IsDoubleStackSlot());
841 Move64(location, temp_location);
842 }
Roland Levillain476df552014-10-09 17:51:36 +0100843 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100844 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100845 switch (instruction->GetType()) {
846 case Primitive::kPrimBoolean:
847 case Primitive::kPrimByte:
848 case Primitive::kPrimChar:
849 case Primitive::kPrimShort:
850 case Primitive::kPrimInt:
851 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100852 case Primitive::kPrimFloat:
853 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100854 break;
855
856 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100857 case Primitive::kPrimDouble:
858 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100859 break;
860
861 default:
862 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
863 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000864 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100865 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100866 switch (instruction->GetType()) {
867 case Primitive::kPrimBoolean:
868 case Primitive::kPrimByte:
869 case Primitive::kPrimChar:
870 case Primitive::kPrimShort:
871 case Primitive::kPrimInt:
872 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100873 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000874 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100875 break;
876
877 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100878 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000879 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100880 break;
881
882 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100883 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100884 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000885 }
886}
887
David Brazdilfc6a86a2015-06-26 10:33:45 +0000888void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100889 DCHECK(!successor->IsExitBlock());
890
891 HBasicBlock* block = got->GetBlock();
892 HInstruction* previous = got->GetPrevious();
893
894 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000895 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100896 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
897 return;
898 }
899
900 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
901 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
902 }
903 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000904 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000905 }
906}
907
David Brazdilfc6a86a2015-06-26 10:33:45 +0000908void LocationsBuilderX86::VisitGoto(HGoto* got) {
909 got->SetLocations(nullptr);
910}
911
912void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
913 HandleGoto(got, got->GetSuccessor());
914}
915
916void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
917 try_boundary->SetLocations(nullptr);
918}
919
920void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
921 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
922 if (!successor->IsExitBlock()) {
923 HandleGoto(try_boundary, successor);
924 }
925}
926
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000927void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000928 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000929}
930
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000931void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700932 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000933}
934
Mark Mendellc4701932015-04-10 13:18:51 -0400935void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
936 Label* true_label,
937 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100938 if (cond->IsFPConditionTrueIfNaN()) {
939 __ j(kUnordered, true_label);
940 } else if (cond->IsFPConditionFalseIfNaN()) {
941 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400942 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100943 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400944}
945
946void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
947 Label* true_label,
948 Label* false_label) {
949 LocationSummary* locations = cond->GetLocations();
950 Location left = locations->InAt(0);
951 Location right = locations->InAt(1);
952 IfCondition if_cond = cond->GetCondition();
953
Mark Mendellc4701932015-04-10 13:18:51 -0400954 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100955 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -0400956 IfCondition true_high_cond = if_cond;
957 IfCondition false_high_cond = cond->GetOppositeCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100958 Condition final_condition = X86UnsignedOrFPCondition(if_cond);
Mark Mendellc4701932015-04-10 13:18:51 -0400959
960 // Set the conditions for the test, remembering that == needs to be
961 // decided using the low words.
962 switch (if_cond) {
963 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -0400964 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +0100965 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -0400966 break;
967 case kCondLT:
968 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400969 break;
970 case kCondLE:
971 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400972 break;
973 case kCondGT:
974 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400975 break;
976 case kCondGE:
977 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400978 break;
979 }
980
981 if (right.IsConstant()) {
982 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -0400983 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +0100984 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -0400985
986 if (val_high == 0) {
987 __ testl(left_high, left_high);
988 } else {
989 __ cmpl(left_high, Immediate(val_high));
990 }
991 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100992 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400993 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100994 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400995 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100996 __ j(X86SignedCondition(true_high_cond), true_label);
997 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400998 }
999 // Must be equal high, so compare the lows.
1000 if (val_low == 0) {
1001 __ testl(left_low, left_low);
1002 } else {
1003 __ cmpl(left_low, Immediate(val_low));
1004 }
1005 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001006 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001007 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001008
1009 __ cmpl(left_high, right_high);
1010 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001011 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001012 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001013 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001014 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001015 __ j(X86SignedCondition(true_high_cond), true_label);
1016 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001017 }
1018 // Must be equal high, so compare the lows.
1019 __ cmpl(left_low, right_low);
1020 }
1021 // The last comparison might be unsigned.
1022 __ j(final_condition, true_label);
1023}
1024
1025void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1026 HCondition* condition,
1027 Label* true_target,
1028 Label* false_target,
1029 Label* always_true_target) {
1030 LocationSummary* locations = condition->GetLocations();
1031 Location left = locations->InAt(0);
1032 Location right = locations->InAt(1);
1033
1034 // We don't want true_target as a nullptr.
1035 if (true_target == nullptr) {
1036 true_target = always_true_target;
1037 }
1038 bool falls_through = (false_target == nullptr);
1039
1040 // FP compares don't like null false_targets.
1041 if (false_target == nullptr) {
1042 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1043 }
1044
1045 Primitive::Type type = condition->InputAt(0)->GetType();
1046 switch (type) {
1047 case Primitive::kPrimLong:
1048 GenerateLongComparesAndJumps(condition, true_target, false_target);
1049 break;
1050 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001051 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1052 GenerateFPJumps(condition, true_target, false_target);
1053 break;
1054 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001055 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1056 GenerateFPJumps(condition, true_target, false_target);
1057 break;
1058 default:
1059 LOG(FATAL) << "Unexpected compare type " << type;
1060 }
1061
1062 if (!falls_through) {
1063 __ jmp(false_target);
1064 }
1065}
1066
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001067void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1068 Label* true_target,
1069 Label* false_target,
1070 Label* always_true_target) {
1071 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001072 if (cond->IsIntConstant()) {
1073 // Constant condition, statically compared against 1.
1074 int32_t cond_value = cond->AsIntConstant()->GetValue();
1075 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001076 if (always_true_target != nullptr) {
1077 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001078 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001079 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001080 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001081 DCHECK_EQ(cond_value, 0);
1082 }
1083 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001084 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001085 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1086 // Moves do not affect the eflags register, so if the condition is
1087 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001088 // again. We can't use the eflags on long/FP conditions if they are
1089 // materialized due to the complex branching.
1090 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001091 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001092 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001093 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1094 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001095 if (!eflags_set) {
1096 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001097 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001098 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001099 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001100 } else {
1101 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1102 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001103 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001104 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001105 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001106 }
1107 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001108 // Condition has not been materialized, use its inputs as the
1109 // comparison and its condition as the branch condition.
1110
Mark Mendellc4701932015-04-10 13:18:51 -04001111 // Is this a long or FP comparison that has been folded into the HCondition?
1112 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1113 // Generate the comparison directly.
1114 GenerateCompareTestAndBranch(instruction->AsIf(),
1115 cond->AsCondition(),
1116 true_target,
1117 false_target,
1118 always_true_target);
1119 return;
1120 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001121
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001122 Location lhs = cond->GetLocations()->InAt(0);
1123 Location rhs = cond->GetLocations()->InAt(1);
1124 // LHS is guaranteed to be in a register (see
1125 // LocationsBuilderX86::VisitCondition).
1126 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001127 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001128 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001129 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001130 if (constant == 0) {
1131 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1132 } else {
1133 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1134 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001135 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001136 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001137 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001138 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001139 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001140 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001141 if (false_target != nullptr) {
1142 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001143 }
1144}
1145
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001146void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1147 LocationSummary* locations =
1148 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1149 HInstruction* cond = if_instr->InputAt(0);
1150 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1151 locations->SetInAt(0, Location::Any());
1152 }
1153}
1154
1155void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1156 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1157 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1158 Label* always_true_target = true_target;
1159 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1160 if_instr->IfTrueSuccessor())) {
1161 always_true_target = nullptr;
1162 }
1163 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1164 if_instr->IfFalseSuccessor())) {
1165 false_target = nullptr;
1166 }
1167 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1168}
1169
1170void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1171 LocationSummary* locations = new (GetGraph()->GetArena())
1172 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1173 HInstruction* cond = deoptimize->InputAt(0);
1174 DCHECK(cond->IsCondition());
1175 if (cond->AsCondition()->NeedsMaterialization()) {
1176 locations->SetInAt(0, Location::Any());
1177 }
1178}
1179
1180void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1181 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
1182 DeoptimizationSlowPathX86(deoptimize);
1183 codegen_->AddSlowPath(slow_path);
1184 Label* slow_path_entry = slow_path->GetEntryLabel();
1185 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1186}
1187
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001188void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001189 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001190}
1191
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001192void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1193 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001194}
1195
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001196void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001197 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001198}
1199
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001200void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001201 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001202 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001203}
1204
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001205void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001206 LocationSummary* locations =
1207 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001208 switch (store->InputAt(1)->GetType()) {
1209 case Primitive::kPrimBoolean:
1210 case Primitive::kPrimByte:
1211 case Primitive::kPrimChar:
1212 case Primitive::kPrimShort:
1213 case Primitive::kPrimInt:
1214 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001215 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001216 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1217 break;
1218
1219 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001220 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001221 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1222 break;
1223
1224 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001225 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001227}
1228
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001229void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001230 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001231}
1232
Roland Levillain0d37cd02015-05-27 16:39:19 +01001233void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001234 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001235 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001236 // Handle the long/FP comparisons made in instruction simplification.
1237 switch (cond->InputAt(0)->GetType()) {
1238 case Primitive::kPrimLong: {
1239 locations->SetInAt(0, Location::RequiresRegister());
1240 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1241 if (cond->NeedsMaterialization()) {
1242 locations->SetOut(Location::RequiresRegister());
1243 }
1244 break;
1245 }
1246 case Primitive::kPrimFloat:
1247 case Primitive::kPrimDouble: {
1248 locations->SetInAt(0, Location::RequiresFpuRegister());
1249 locations->SetInAt(1, Location::RequiresFpuRegister());
1250 if (cond->NeedsMaterialization()) {
1251 locations->SetOut(Location::RequiresRegister());
1252 }
1253 break;
1254 }
1255 default:
1256 locations->SetInAt(0, Location::RequiresRegister());
1257 locations->SetInAt(1, Location::Any());
1258 if (cond->NeedsMaterialization()) {
1259 // We need a byte register.
1260 locations->SetOut(Location::RegisterLocation(ECX));
1261 }
1262 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001263 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001264}
1265
Roland Levillain0d37cd02015-05-27 16:39:19 +01001266void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001267 if (!cond->NeedsMaterialization()) {
1268 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001269 }
Mark Mendellc4701932015-04-10 13:18:51 -04001270
1271 LocationSummary* locations = cond->GetLocations();
1272 Location lhs = locations->InAt(0);
1273 Location rhs = locations->InAt(1);
1274 Register reg = locations->Out().AsRegister<Register>();
1275 Label true_label, false_label;
1276
1277 switch (cond->InputAt(0)->GetType()) {
1278 default: {
1279 // Integer case.
1280
1281 // Clear output register: setcc only sets the low byte.
1282 __ xorl(reg, reg);
1283
1284 if (rhs.IsRegister()) {
1285 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1286 } else if (rhs.IsConstant()) {
1287 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1288 if (constant == 0) {
1289 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1290 } else {
1291 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1292 }
1293 } else {
1294 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1295 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001296 __ setb(X86SignedCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001297 return;
1298 }
1299 case Primitive::kPrimLong:
1300 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1301 break;
1302 case Primitive::kPrimFloat:
1303 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1304 GenerateFPJumps(cond, &true_label, &false_label);
1305 break;
1306 case Primitive::kPrimDouble:
1307 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1308 GenerateFPJumps(cond, &true_label, &false_label);
1309 break;
1310 }
1311
1312 // Convert the jumps into the result.
1313 Label done_label;
1314
Roland Levillain4fa13f62015-07-06 18:11:54 +01001315 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001316 __ Bind(&false_label);
1317 __ xorl(reg, reg);
1318 __ jmp(&done_label);
1319
Roland Levillain4fa13f62015-07-06 18:11:54 +01001320 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001321 __ Bind(&true_label);
1322 __ movl(reg, Immediate(1));
1323 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001324}
1325
1326void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1327 VisitCondition(comp);
1328}
1329
1330void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1331 VisitCondition(comp);
1332}
1333
1334void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1335 VisitCondition(comp);
1336}
1337
1338void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1339 VisitCondition(comp);
1340}
1341
1342void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1343 VisitCondition(comp);
1344}
1345
1346void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1347 VisitCondition(comp);
1348}
1349
1350void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1351 VisitCondition(comp);
1352}
1353
1354void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1355 VisitCondition(comp);
1356}
1357
1358void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1359 VisitCondition(comp);
1360}
1361
1362void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1363 VisitCondition(comp);
1364}
1365
1366void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1367 VisitCondition(comp);
1368}
1369
1370void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1371 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001372}
1373
1374void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001375 LocationSummary* locations =
1376 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001377 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001378}
1379
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001380void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001381 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001382 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001383}
1384
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001385void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1386 LocationSummary* locations =
1387 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1388 locations->SetOut(Location::ConstantLocation(constant));
1389}
1390
1391void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1392 // Will be generated at use site.
1393 UNUSED(constant);
1394}
1395
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001396void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001397 LocationSummary* locations =
1398 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001399 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001400}
1401
1402void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1403 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001404 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001405}
1406
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001407void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1408 LocationSummary* locations =
1409 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1410 locations->SetOut(Location::ConstantLocation(constant));
1411}
1412
1413void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* 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
1418void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1419 LocationSummary* locations =
1420 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1421 locations->SetOut(Location::ConstantLocation(constant));
1422}
1423
1424void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1425 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001426 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001427}
1428
Calin Juravle27df7582015-04-17 19:12:31 +01001429void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1430 memory_barrier->SetLocations(nullptr);
1431}
1432
1433void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1434 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1435}
1436
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001437void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001438 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001439}
1440
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001441void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001442 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001443 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001444}
1445
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001446void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001447 LocationSummary* locations =
1448 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001449 switch (ret->InputAt(0)->GetType()) {
1450 case Primitive::kPrimBoolean:
1451 case Primitive::kPrimByte:
1452 case Primitive::kPrimChar:
1453 case Primitive::kPrimShort:
1454 case Primitive::kPrimInt:
1455 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001456 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001457 break;
1458
1459 case Primitive::kPrimLong:
1460 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001461 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001462 break;
1463
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001464 case Primitive::kPrimFloat:
1465 case Primitive::kPrimDouble:
1466 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001467 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001468 break;
1469
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001470 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001471 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001472 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001473}
1474
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001475void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001476 if (kIsDebugBuild) {
1477 switch (ret->InputAt(0)->GetType()) {
1478 case Primitive::kPrimBoolean:
1479 case Primitive::kPrimByte:
1480 case Primitive::kPrimChar:
1481 case Primitive::kPrimShort:
1482 case Primitive::kPrimInt:
1483 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001484 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001485 break;
1486
1487 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001488 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1489 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001490 break;
1491
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001492 case Primitive::kPrimFloat:
1493 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001494 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001495 break;
1496
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001497 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001498 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001499 }
1500 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001501 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001502}
1503
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001504void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001505 // When we do not run baseline, explicit clinit checks triggered by static
1506 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1507 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001508
Mark Mendellfb8d2792015-03-31 22:16:59 -04001509 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001510 if (intrinsic.TryDispatch(invoke)) {
1511 return;
1512 }
1513
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001514 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001515
1516 if (codegen_->IsBaseline()) {
1517 // Baseline does not have enough registers if the current method also
1518 // needs a register. We therefore do not require a register for it, and let
1519 // the code generation of the invoke handle it.
1520 LocationSummary* locations = invoke->GetLocations();
1521 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1522 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1523 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1524 }
1525 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001526}
1527
Mark Mendell09ed1a32015-03-25 08:30:06 -04001528static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1529 if (invoke->GetLocations()->Intrinsified()) {
1530 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1531 intrinsic.Dispatch(invoke);
1532 return true;
1533 }
1534 return false;
1535}
1536
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001537void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001538 // When we do not run baseline, explicit clinit checks triggered by static
1539 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1540 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001541
Mark Mendell09ed1a32015-03-25 08:30:06 -04001542 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1543 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001544 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001545
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001546 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001547 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001548 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001549 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001550}
1551
1552void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1553 HandleInvoke(invoke);
1554}
1555
1556void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001557 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001558 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001559}
1560
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001561void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001562 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001563 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1564 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001565 LocationSummary* locations = invoke->GetLocations();
1566 Location receiver = locations->InAt(0);
1567 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001568 // temp = object->GetClass();
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001569 DCHECK(receiver.IsRegister());
1570 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001571 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001572 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001573 // temp = temp->GetMethodAt(method_offset);
1574 __ movl(temp, Address(temp, method_offset));
1575 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001576 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001577 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001578
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001579 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001580 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001581}
1582
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001583void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1584 HandleInvoke(invoke);
1585 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001586 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001587}
1588
1589void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1590 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001591 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001592 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1593 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001594 LocationSummary* locations = invoke->GetLocations();
1595 Location receiver = locations->InAt(0);
1596 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1597
1598 // Set the hidden argument.
1599 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001600 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001601
1602 // temp = object->GetClass();
1603 if (receiver.IsStackSlot()) {
1604 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1605 __ movl(temp, Address(temp, class_offset));
1606 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001607 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001608 }
Roland Levillain4d027112015-07-01 15:41:14 +01001609 codegen_->MaybeRecordImplicitNullCheck(invoke);
1610 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001611 // temp = temp->GetImtEntryAt(method_offset);
1612 __ movl(temp, Address(temp, method_offset));
1613 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001614 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001615 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001616
1617 DCHECK(!codegen_->IsLeafMethod());
1618 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1619}
1620
Roland Levillain88cb1752014-10-20 16:36:47 +01001621void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1622 LocationSummary* locations =
1623 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1624 switch (neg->GetResultType()) {
1625 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001626 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001627 locations->SetInAt(0, Location::RequiresRegister());
1628 locations->SetOut(Location::SameAsFirstInput());
1629 break;
1630
Roland Levillain88cb1752014-10-20 16:36:47 +01001631 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001632 locations->SetInAt(0, Location::RequiresFpuRegister());
1633 locations->SetOut(Location::SameAsFirstInput());
1634 locations->AddTemp(Location::RequiresRegister());
1635 locations->AddTemp(Location::RequiresFpuRegister());
1636 break;
1637
Roland Levillain88cb1752014-10-20 16:36:47 +01001638 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001639 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001640 locations->SetOut(Location::SameAsFirstInput());
1641 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001642 break;
1643
1644 default:
1645 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1646 }
1647}
1648
1649void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1650 LocationSummary* locations = neg->GetLocations();
1651 Location out = locations->Out();
1652 Location in = locations->InAt(0);
1653 switch (neg->GetResultType()) {
1654 case Primitive::kPrimInt:
1655 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001656 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001657 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001658 break;
1659
1660 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001661 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001662 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001663 __ negl(out.AsRegisterPairLow<Register>());
1664 // Negation is similar to subtraction from zero. The least
1665 // significant byte triggers a borrow when it is different from
1666 // zero; to take it into account, add 1 to the most significant
1667 // byte if the carry flag (CF) is set to 1 after the first NEGL
1668 // operation.
1669 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1670 __ negl(out.AsRegisterPairHigh<Register>());
1671 break;
1672
Roland Levillain5368c212014-11-27 15:03:41 +00001673 case Primitive::kPrimFloat: {
1674 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001675 Register constant = locations->GetTemp(0).AsRegister<Register>();
1676 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001677 // Implement float negation with an exclusive or with value
1678 // 0x80000000 (mask for bit 31, representing the sign of a
1679 // single-precision floating-point number).
1680 __ movl(constant, Immediate(INT32_C(0x80000000)));
1681 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001682 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001683 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001684 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001685
Roland Levillain5368c212014-11-27 15:03:41 +00001686 case Primitive::kPrimDouble: {
1687 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001688 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001689 // Implement double negation with an exclusive or with value
1690 // 0x8000000000000000 (mask for bit 63, representing the sign of
1691 // a double-precision floating-point number).
1692 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001693 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001694 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001695 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001696
1697 default:
1698 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1699 }
1700}
1701
Roland Levillaindff1f282014-11-05 14:15:05 +00001702void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001703 Primitive::Type result_type = conversion->GetResultType();
1704 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001705 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001706
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001707 // The float-to-long and double-to-long type conversions rely on a
1708 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001709 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001710 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1711 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001712 ? LocationSummary::kCall
1713 : LocationSummary::kNoCall;
1714 LocationSummary* locations =
1715 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1716
David Brazdilb2bd1c52015-03-25 11:17:37 +00001717 // The Java language does not allow treating boolean as an integral type but
1718 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001719
Roland Levillaindff1f282014-11-05 14:15:05 +00001720 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001721 case Primitive::kPrimByte:
1722 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001723 case Primitive::kPrimBoolean:
1724 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001725 case Primitive::kPrimShort:
1726 case Primitive::kPrimInt:
1727 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001728 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001729 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1730 // Make the output overlap to please the register allocator. This greatly simplifies
1731 // the validation of the linear scan implementation
1732 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001733 break;
1734
1735 default:
1736 LOG(FATAL) << "Unexpected type conversion from " << input_type
1737 << " to " << result_type;
1738 }
1739 break;
1740
Roland Levillain01a8d712014-11-14 16:27:39 +00001741 case Primitive::kPrimShort:
1742 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001743 case Primitive::kPrimBoolean:
1744 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001745 case Primitive::kPrimByte:
1746 case Primitive::kPrimInt:
1747 case Primitive::kPrimChar:
1748 // Processing a Dex `int-to-short' instruction.
1749 locations->SetInAt(0, Location::Any());
1750 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1751 break;
1752
1753 default:
1754 LOG(FATAL) << "Unexpected type conversion from " << input_type
1755 << " to " << result_type;
1756 }
1757 break;
1758
Roland Levillain946e1432014-11-11 17:35:19 +00001759 case Primitive::kPrimInt:
1760 switch (input_type) {
1761 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001762 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001763 locations->SetInAt(0, Location::Any());
1764 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1765 break;
1766
1767 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001768 // Processing a Dex `float-to-int' instruction.
1769 locations->SetInAt(0, Location::RequiresFpuRegister());
1770 locations->SetOut(Location::RequiresRegister());
1771 locations->AddTemp(Location::RequiresFpuRegister());
1772 break;
1773
Roland Levillain946e1432014-11-11 17:35:19 +00001774 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001775 // Processing a Dex `double-to-int' instruction.
1776 locations->SetInAt(0, Location::RequiresFpuRegister());
1777 locations->SetOut(Location::RequiresRegister());
1778 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001779 break;
1780
1781 default:
1782 LOG(FATAL) << "Unexpected type conversion from " << input_type
1783 << " to " << result_type;
1784 }
1785 break;
1786
Roland Levillaindff1f282014-11-05 14:15:05 +00001787 case Primitive::kPrimLong:
1788 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001789 case Primitive::kPrimBoolean:
1790 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001791 case Primitive::kPrimByte:
1792 case Primitive::kPrimShort:
1793 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001794 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001795 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001796 locations->SetInAt(0, Location::RegisterLocation(EAX));
1797 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1798 break;
1799
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001800 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001801 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001802 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001803 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001804 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1805 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1806
Vladimir Marko949c91f2015-01-27 10:48:44 +00001807 // The runtime helper puts the result in EAX, EDX.
1808 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001809 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001810 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001811
1812 default:
1813 LOG(FATAL) << "Unexpected type conversion from " << input_type
1814 << " to " << result_type;
1815 }
1816 break;
1817
Roland Levillain981e4542014-11-14 11:47:14 +00001818 case Primitive::kPrimChar:
1819 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001820 case Primitive::kPrimBoolean:
1821 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001822 case Primitive::kPrimByte:
1823 case Primitive::kPrimShort:
1824 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001825 // Processing a Dex `int-to-char' instruction.
1826 locations->SetInAt(0, Location::Any());
1827 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1828 break;
1829
1830 default:
1831 LOG(FATAL) << "Unexpected type conversion from " << input_type
1832 << " to " << result_type;
1833 }
1834 break;
1835
Roland Levillaindff1f282014-11-05 14:15:05 +00001836 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001837 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001838 case Primitive::kPrimBoolean:
1839 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001840 case Primitive::kPrimByte:
1841 case Primitive::kPrimShort:
1842 case Primitive::kPrimInt:
1843 case Primitive::kPrimChar:
1844 // Processing a Dex `int-to-float' instruction.
1845 locations->SetInAt(0, Location::RequiresRegister());
1846 locations->SetOut(Location::RequiresFpuRegister());
1847 break;
1848
1849 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001850 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001851 locations->SetInAt(0, Location::Any());
1852 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001853 break;
1854
Roland Levillaincff13742014-11-17 14:32:17 +00001855 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001856 // Processing a Dex `double-to-float' instruction.
1857 locations->SetInAt(0, Location::RequiresFpuRegister());
1858 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001859 break;
1860
1861 default:
1862 LOG(FATAL) << "Unexpected type conversion from " << input_type
1863 << " to " << result_type;
1864 };
1865 break;
1866
Roland Levillaindff1f282014-11-05 14:15:05 +00001867 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001868 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001869 case Primitive::kPrimBoolean:
1870 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001871 case Primitive::kPrimByte:
1872 case Primitive::kPrimShort:
1873 case Primitive::kPrimInt:
1874 case Primitive::kPrimChar:
1875 // Processing a Dex `int-to-double' instruction.
1876 locations->SetInAt(0, Location::RequiresRegister());
1877 locations->SetOut(Location::RequiresFpuRegister());
1878 break;
1879
1880 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001881 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001882 locations->SetInAt(0, Location::Any());
1883 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001884 break;
1885
Roland Levillaincff13742014-11-17 14:32:17 +00001886 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001887 // Processing a Dex `float-to-double' instruction.
1888 locations->SetInAt(0, Location::RequiresFpuRegister());
1889 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001890 break;
1891
1892 default:
1893 LOG(FATAL) << "Unexpected type conversion from " << input_type
1894 << " to " << result_type;
1895 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001896 break;
1897
1898 default:
1899 LOG(FATAL) << "Unexpected type conversion from " << input_type
1900 << " to " << result_type;
1901 }
1902}
1903
1904void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1905 LocationSummary* locations = conversion->GetLocations();
1906 Location out = locations->Out();
1907 Location in = locations->InAt(0);
1908 Primitive::Type result_type = conversion->GetResultType();
1909 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001910 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001911 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001912 case Primitive::kPrimByte:
1913 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001914 case Primitive::kPrimBoolean:
1915 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001916 case Primitive::kPrimShort:
1917 case Primitive::kPrimInt:
1918 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001919 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001920 if (in.IsRegister()) {
1921 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001922 } else {
1923 DCHECK(in.GetConstant()->IsIntConstant());
1924 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1925 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1926 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001927 break;
1928
1929 default:
1930 LOG(FATAL) << "Unexpected type conversion from " << input_type
1931 << " to " << result_type;
1932 }
1933 break;
1934
Roland Levillain01a8d712014-11-14 16:27:39 +00001935 case Primitive::kPrimShort:
1936 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001937 case Primitive::kPrimBoolean:
1938 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001939 case Primitive::kPrimByte:
1940 case Primitive::kPrimInt:
1941 case Primitive::kPrimChar:
1942 // Processing a Dex `int-to-short' instruction.
1943 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001944 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001945 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001946 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001947 } else {
1948 DCHECK(in.GetConstant()->IsIntConstant());
1949 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001950 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001951 }
1952 break;
1953
1954 default:
1955 LOG(FATAL) << "Unexpected type conversion from " << input_type
1956 << " to " << result_type;
1957 }
1958 break;
1959
Roland Levillain946e1432014-11-11 17:35:19 +00001960 case Primitive::kPrimInt:
1961 switch (input_type) {
1962 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001963 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001964 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001965 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001966 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001967 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001968 } else {
1969 DCHECK(in.IsConstant());
1970 DCHECK(in.GetConstant()->IsLongConstant());
1971 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001972 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001973 }
1974 break;
1975
Roland Levillain3f8f9362014-12-02 17:45:01 +00001976 case Primitive::kPrimFloat: {
1977 // Processing a Dex `float-to-int' instruction.
1978 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1979 Register output = out.AsRegister<Register>();
1980 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1981 Label done, nan;
1982
1983 __ movl(output, Immediate(kPrimIntMax));
1984 // temp = int-to-float(output)
1985 __ cvtsi2ss(temp, output);
1986 // if input >= temp goto done
1987 __ comiss(input, temp);
1988 __ j(kAboveEqual, &done);
1989 // if input == NaN goto nan
1990 __ j(kUnordered, &nan);
1991 // output = float-to-int-truncate(input)
1992 __ cvttss2si(output, input);
1993 __ jmp(&done);
1994 __ Bind(&nan);
1995 // output = 0
1996 __ xorl(output, output);
1997 __ Bind(&done);
1998 break;
1999 }
2000
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002001 case Primitive::kPrimDouble: {
2002 // Processing a Dex `double-to-int' instruction.
2003 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2004 Register output = out.AsRegister<Register>();
2005 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2006 Label done, nan;
2007
2008 __ movl(output, Immediate(kPrimIntMax));
2009 // temp = int-to-double(output)
2010 __ cvtsi2sd(temp, output);
2011 // if input >= temp goto done
2012 __ comisd(input, temp);
2013 __ j(kAboveEqual, &done);
2014 // if input == NaN goto nan
2015 __ j(kUnordered, &nan);
2016 // output = double-to-int-truncate(input)
2017 __ cvttsd2si(output, input);
2018 __ jmp(&done);
2019 __ Bind(&nan);
2020 // output = 0
2021 __ xorl(output, output);
2022 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002023 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002024 }
Roland Levillain946e1432014-11-11 17:35:19 +00002025
2026 default:
2027 LOG(FATAL) << "Unexpected type conversion from " << input_type
2028 << " to " << result_type;
2029 }
2030 break;
2031
Roland Levillaindff1f282014-11-05 14:15:05 +00002032 case Primitive::kPrimLong:
2033 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002034 case Primitive::kPrimBoolean:
2035 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002036 case Primitive::kPrimByte:
2037 case Primitive::kPrimShort:
2038 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002039 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002040 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002041 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2042 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002043 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002044 __ cdq();
2045 break;
2046
2047 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002048 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002049 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2050 conversion,
2051 conversion->GetDexPc(),
2052 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002053 break;
2054
Roland Levillaindff1f282014-11-05 14:15:05 +00002055 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002056 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002057 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2058 conversion,
2059 conversion->GetDexPc(),
2060 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002061 break;
2062
2063 default:
2064 LOG(FATAL) << "Unexpected type conversion from " << input_type
2065 << " to " << result_type;
2066 }
2067 break;
2068
Roland Levillain981e4542014-11-14 11:47:14 +00002069 case Primitive::kPrimChar:
2070 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002071 case Primitive::kPrimBoolean:
2072 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002073 case Primitive::kPrimByte:
2074 case Primitive::kPrimShort:
2075 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002076 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2077 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002078 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002079 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002080 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002081 } else {
2082 DCHECK(in.GetConstant()->IsIntConstant());
2083 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002084 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002085 }
2086 break;
2087
2088 default:
2089 LOG(FATAL) << "Unexpected type conversion from " << input_type
2090 << " to " << result_type;
2091 }
2092 break;
2093
Roland Levillaindff1f282014-11-05 14:15:05 +00002094 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002095 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002096 case Primitive::kPrimBoolean:
2097 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002098 case Primitive::kPrimByte:
2099 case Primitive::kPrimShort:
2100 case Primitive::kPrimInt:
2101 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002102 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002103 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002104 break;
2105
Roland Levillain6d0e4832014-11-27 18:31:21 +00002106 case Primitive::kPrimLong: {
2107 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002108 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002109
Roland Levillain232ade02015-04-20 15:14:36 +01002110 // Create stack space for the call to
2111 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2112 // TODO: enhance register allocator to ask for stack temporaries.
2113 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2114 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2115 __ subl(ESP, Immediate(adjustment));
2116 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002117
Roland Levillain232ade02015-04-20 15:14:36 +01002118 // Load the value to the FP stack, using temporaries if needed.
2119 PushOntoFPStack(in, 0, adjustment, false, true);
2120
2121 if (out.IsStackSlot()) {
2122 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2123 } else {
2124 __ fstps(Address(ESP, 0));
2125 Location stack_temp = Location::StackSlot(0);
2126 codegen_->Move32(out, stack_temp);
2127 }
2128
2129 // Remove the temporary stack space we allocated.
2130 if (adjustment != 0) {
2131 __ addl(ESP, Immediate(adjustment));
2132 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002133 break;
2134 }
2135
Roland Levillaincff13742014-11-17 14:32:17 +00002136 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002137 // Processing a Dex `double-to-float' instruction.
2138 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002139 break;
2140
2141 default:
2142 LOG(FATAL) << "Unexpected type conversion from " << input_type
2143 << " to " << result_type;
2144 };
2145 break;
2146
Roland Levillaindff1f282014-11-05 14:15:05 +00002147 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002148 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002149 case Primitive::kPrimBoolean:
2150 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002151 case Primitive::kPrimByte:
2152 case Primitive::kPrimShort:
2153 case Primitive::kPrimInt:
2154 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002155 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002156 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002157 break;
2158
Roland Levillain647b9ed2014-11-27 12:06:00 +00002159 case Primitive::kPrimLong: {
2160 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002161 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002162
Roland Levillain232ade02015-04-20 15:14:36 +01002163 // Create stack space for the call to
2164 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2165 // TODO: enhance register allocator to ask for stack temporaries.
2166 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2167 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2168 __ subl(ESP, Immediate(adjustment));
2169 }
2170
2171 // Load the value to the FP stack, using temporaries if needed.
2172 PushOntoFPStack(in, 0, adjustment, false, true);
2173
2174 if (out.IsDoubleStackSlot()) {
2175 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2176 } else {
2177 __ fstpl(Address(ESP, 0));
2178 Location stack_temp = Location::DoubleStackSlot(0);
2179 codegen_->Move64(out, stack_temp);
2180 }
2181
2182 // Remove the temporary stack space we allocated.
2183 if (adjustment != 0) {
2184 __ addl(ESP, Immediate(adjustment));
2185 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002186 break;
2187 }
2188
Roland Levillaincff13742014-11-17 14:32:17 +00002189 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002190 // Processing a Dex `float-to-double' instruction.
2191 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002192 break;
2193
2194 default:
2195 LOG(FATAL) << "Unexpected type conversion from " << input_type
2196 << " to " << result_type;
2197 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002198 break;
2199
2200 default:
2201 LOG(FATAL) << "Unexpected type conversion from " << input_type
2202 << " to " << result_type;
2203 }
2204}
2205
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002206void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002207 LocationSummary* locations =
2208 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002209 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002210 case Primitive::kPrimInt: {
2211 locations->SetInAt(0, Location::RequiresRegister());
2212 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2213 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2214 break;
2215 }
2216
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002217 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002218 locations->SetInAt(0, Location::RequiresRegister());
2219 locations->SetInAt(1, Location::Any());
2220 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002221 break;
2222 }
2223
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002224 case Primitive::kPrimFloat:
2225 case Primitive::kPrimDouble: {
2226 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00002227 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002228 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002229 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002230 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002231
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002232 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002233 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2234 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002235 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002236}
2237
2238void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2239 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002240 Location first = locations->InAt(0);
2241 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002242 Location out = locations->Out();
2243
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002244 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002245 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002246 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002247 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2248 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002249 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2250 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002251 } else {
2252 __ leal(out.AsRegister<Register>(), Address(
2253 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2254 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002255 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002256 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2257 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2258 __ addl(out.AsRegister<Register>(), Immediate(value));
2259 } else {
2260 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2261 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002262 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002263 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002264 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002265 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002266 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002267 }
2268
2269 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002270 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002271 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2272 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002273 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002274 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2275 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002276 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002277 } else {
2278 DCHECK(second.IsConstant()) << second;
2279 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2280 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2281 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002282 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002283 break;
2284 }
2285
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002286 case Primitive::kPrimFloat: {
2287 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002288 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002289 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002290 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002291 }
2292
2293 case Primitive::kPrimDouble: {
2294 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002295 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002296 }
2297 break;
2298 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002299
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002300 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002301 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002302 }
2303}
2304
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002305void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002306 LocationSummary* locations =
2307 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002308 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002309 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002310 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002311 locations->SetInAt(0, Location::RequiresRegister());
2312 locations->SetInAt(1, Location::Any());
2313 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002314 break;
2315 }
Calin Juravle11351682014-10-23 15:38:15 +01002316 case Primitive::kPrimFloat:
2317 case Primitive::kPrimDouble: {
2318 locations->SetInAt(0, Location::RequiresFpuRegister());
2319 locations->SetInAt(1, Location::RequiresFpuRegister());
2320 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002321 break;
Calin Juravle11351682014-10-23 15:38:15 +01002322 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002323
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002324 default:
Calin Juravle11351682014-10-23 15:38:15 +01002325 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002326 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002327}
2328
2329void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2330 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002331 Location first = locations->InAt(0);
2332 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002333 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002334 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002335 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002336 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002337 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002338 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002339 __ subl(first.AsRegister<Register>(),
2340 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002341 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002342 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002343 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002344 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002345 }
2346
2347 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002348 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002349 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2350 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002351 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002352 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002353 __ sbbl(first.AsRegisterPairHigh<Register>(),
2354 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002355 } else {
2356 DCHECK(second.IsConstant()) << second;
2357 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2358 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2359 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002360 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002361 break;
2362 }
2363
Calin Juravle11351682014-10-23 15:38:15 +01002364 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002365 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002366 break;
Calin Juravle11351682014-10-23 15:38:15 +01002367 }
2368
2369 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002370 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002371 break;
2372 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002373
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002374 default:
Calin Juravle11351682014-10-23 15:38:15 +01002375 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002376 }
2377}
2378
Calin Juravle34bacdf2014-10-07 20:23:36 +01002379void LocationsBuilderX86::VisitMul(HMul* mul) {
2380 LocationSummary* locations =
2381 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2382 switch (mul->GetResultType()) {
2383 case Primitive::kPrimInt:
2384 locations->SetInAt(0, Location::RequiresRegister());
2385 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002386 if (mul->InputAt(1)->IsIntConstant()) {
2387 // Can use 3 operand multiply.
2388 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2389 } else {
2390 locations->SetOut(Location::SameAsFirstInput());
2391 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002392 break;
2393 case Primitive::kPrimLong: {
2394 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002395 locations->SetInAt(1, Location::Any());
2396 locations->SetOut(Location::SameAsFirstInput());
2397 // Needed for imul on 32bits with 64bits output.
2398 locations->AddTemp(Location::RegisterLocation(EAX));
2399 locations->AddTemp(Location::RegisterLocation(EDX));
2400 break;
2401 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002402 case Primitive::kPrimFloat:
2403 case Primitive::kPrimDouble: {
2404 locations->SetInAt(0, Location::RequiresFpuRegister());
2405 locations->SetInAt(1, Location::RequiresFpuRegister());
2406 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002407 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002408 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002409
2410 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002411 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002412 }
2413}
2414
2415void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2416 LocationSummary* locations = mul->GetLocations();
2417 Location first = locations->InAt(0);
2418 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002419 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002420
2421 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002422 case Primitive::kPrimInt:
2423 // The constant may have ended up in a register, so test explicitly to avoid
2424 // problems where the output may not be the same as the first operand.
2425 if (mul->InputAt(1)->IsIntConstant()) {
2426 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2427 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2428 } else if (second.IsRegister()) {
2429 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002430 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002431 } else {
2432 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002433 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002434 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002435 }
2436 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002437
2438 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002439 Register in1_hi = first.AsRegisterPairHigh<Register>();
2440 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002441 Register eax = locations->GetTemp(0).AsRegister<Register>();
2442 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002443
2444 DCHECK_EQ(EAX, eax);
2445 DCHECK_EQ(EDX, edx);
2446
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002447 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002448 // output: in1
2449 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2450 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2451 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002452 if (second.IsConstant()) {
2453 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002454
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002455 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2456 int32_t low_value = Low32Bits(value);
2457 int32_t high_value = High32Bits(value);
2458 Immediate low(low_value);
2459 Immediate high(high_value);
2460
2461 __ movl(eax, high);
2462 // eax <- in1.lo * in2.hi
2463 __ imull(eax, in1_lo);
2464 // in1.hi <- in1.hi * in2.lo
2465 __ imull(in1_hi, low);
2466 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2467 __ addl(in1_hi, eax);
2468 // move in2_lo to eax to prepare for double precision
2469 __ movl(eax, low);
2470 // edx:eax <- in1.lo * in2.lo
2471 __ mull(in1_lo);
2472 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2473 __ addl(in1_hi, edx);
2474 // in1.lo <- (in1.lo * in2.lo)[31:0];
2475 __ movl(in1_lo, eax);
2476 } else if (second.IsRegisterPair()) {
2477 Register in2_hi = second.AsRegisterPairHigh<Register>();
2478 Register in2_lo = second.AsRegisterPairLow<Register>();
2479
2480 __ movl(eax, in2_hi);
2481 // eax <- in1.lo * in2.hi
2482 __ imull(eax, in1_lo);
2483 // in1.hi <- in1.hi * in2.lo
2484 __ imull(in1_hi, in2_lo);
2485 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2486 __ addl(in1_hi, eax);
2487 // move in1_lo to eax to prepare for double precision
2488 __ movl(eax, in1_lo);
2489 // edx:eax <- in1.lo * in2.lo
2490 __ mull(in2_lo);
2491 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2492 __ addl(in1_hi, edx);
2493 // in1.lo <- (in1.lo * in2.lo)[31:0];
2494 __ movl(in1_lo, eax);
2495 } else {
2496 DCHECK(second.IsDoubleStackSlot()) << second;
2497 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2498 Address in2_lo(ESP, second.GetStackIndex());
2499
2500 __ movl(eax, in2_hi);
2501 // eax <- in1.lo * in2.hi
2502 __ imull(eax, in1_lo);
2503 // in1.hi <- in1.hi * in2.lo
2504 __ imull(in1_hi, in2_lo);
2505 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2506 __ addl(in1_hi, eax);
2507 // move in1_lo to eax to prepare for double precision
2508 __ movl(eax, in1_lo);
2509 // edx:eax <- in1.lo * in2.lo
2510 __ mull(in2_lo);
2511 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2512 __ addl(in1_hi, edx);
2513 // in1.lo <- (in1.lo * in2.lo)[31:0];
2514 __ movl(in1_lo, eax);
2515 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002516
2517 break;
2518 }
2519
Calin Juravleb5bfa962014-10-21 18:02:24 +01002520 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002521 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002522 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002523 }
2524
2525 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002526 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002527 break;
2528 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002529
2530 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002531 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002532 }
2533}
2534
Roland Levillain232ade02015-04-20 15:14:36 +01002535void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2536 uint32_t temp_offset,
2537 uint32_t stack_adjustment,
2538 bool is_fp,
2539 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002540 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002541 DCHECK(!is_wide);
2542 if (is_fp) {
2543 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2544 } else {
2545 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2546 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002547 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002548 DCHECK(is_wide);
2549 if (is_fp) {
2550 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2551 } else {
2552 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2553 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002554 } else {
2555 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002556 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002557 Location stack_temp = Location::StackSlot(temp_offset);
2558 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002559 if (is_fp) {
2560 __ flds(Address(ESP, temp_offset));
2561 } else {
2562 __ filds(Address(ESP, temp_offset));
2563 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002564 } else {
2565 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2566 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002567 if (is_fp) {
2568 __ fldl(Address(ESP, temp_offset));
2569 } else {
2570 __ fildl(Address(ESP, temp_offset));
2571 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002572 }
2573 }
2574}
2575
2576void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2577 Primitive::Type type = rem->GetResultType();
2578 bool is_float = type == Primitive::kPrimFloat;
2579 size_t elem_size = Primitive::ComponentSize(type);
2580 LocationSummary* locations = rem->GetLocations();
2581 Location first = locations->InAt(0);
2582 Location second = locations->InAt(1);
2583 Location out = locations->Out();
2584
2585 // Create stack space for 2 elements.
2586 // TODO: enhance register allocator to ask for stack temporaries.
2587 __ subl(ESP, Immediate(2 * elem_size));
2588
2589 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002590 const bool is_wide = !is_float;
2591 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2592 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002593
2594 // Loop doing FPREM until we stabilize.
2595 Label retry;
2596 __ Bind(&retry);
2597 __ fprem();
2598
2599 // Move FP status to AX.
2600 __ fstsw();
2601
2602 // And see if the argument reduction is complete. This is signaled by the
2603 // C2 FPU flag bit set to 0.
2604 __ andl(EAX, Immediate(kC2ConditionMask));
2605 __ j(kNotEqual, &retry);
2606
2607 // We have settled on the final value. Retrieve it into an XMM register.
2608 // Store FP top of stack to real stack.
2609 if (is_float) {
2610 __ fsts(Address(ESP, 0));
2611 } else {
2612 __ fstl(Address(ESP, 0));
2613 }
2614
2615 // Pop the 2 items from the FP stack.
2616 __ fucompp();
2617
2618 // Load the value from the stack into an XMM register.
2619 DCHECK(out.IsFpuRegister()) << out;
2620 if (is_float) {
2621 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2622 } else {
2623 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2624 }
2625
2626 // And remove the temporary stack space we allocated.
2627 __ addl(ESP, Immediate(2 * elem_size));
2628}
2629
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002630
2631void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2632 DCHECK(instruction->IsDiv() || instruction->IsRem());
2633
2634 LocationSummary* locations = instruction->GetLocations();
2635 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002636 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002637
2638 Register out_register = locations->Out().AsRegister<Register>();
2639 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002640 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002641
2642 DCHECK(imm == 1 || imm == -1);
2643
2644 if (instruction->IsRem()) {
2645 __ xorl(out_register, out_register);
2646 } else {
2647 __ movl(out_register, input_register);
2648 if (imm == -1) {
2649 __ negl(out_register);
2650 }
2651 }
2652}
2653
2654
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002655void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002656 LocationSummary* locations = instruction->GetLocations();
2657
2658 Register out_register = locations->Out().AsRegister<Register>();
2659 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002660 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002661
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002662 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002663 Register num = locations->GetTemp(0).AsRegister<Register>();
2664
2665 __ leal(num, Address(input_register, std::abs(imm) - 1));
2666 __ testl(input_register, input_register);
2667 __ cmovl(kGreaterEqual, num, input_register);
2668 int shift = CTZ(imm);
2669 __ sarl(num, Immediate(shift));
2670
2671 if (imm < 0) {
2672 __ negl(num);
2673 }
2674
2675 __ movl(out_register, num);
2676}
2677
2678void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2679 DCHECK(instruction->IsDiv() || instruction->IsRem());
2680
2681 LocationSummary* locations = instruction->GetLocations();
2682 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2683
2684 Register eax = locations->InAt(0).AsRegister<Register>();
2685 Register out = locations->Out().AsRegister<Register>();
2686 Register num;
2687 Register edx;
2688
2689 if (instruction->IsDiv()) {
2690 edx = locations->GetTemp(0).AsRegister<Register>();
2691 num = locations->GetTemp(1).AsRegister<Register>();
2692 } else {
2693 edx = locations->Out().AsRegister<Register>();
2694 num = locations->GetTemp(0).AsRegister<Register>();
2695 }
2696
2697 DCHECK_EQ(EAX, eax);
2698 DCHECK_EQ(EDX, edx);
2699 if (instruction->IsDiv()) {
2700 DCHECK_EQ(EAX, out);
2701 } else {
2702 DCHECK_EQ(EDX, out);
2703 }
2704
2705 int64_t magic;
2706 int shift;
2707 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2708
2709 Label ndiv;
2710 Label end;
2711 // If numerator is 0, the result is 0, no computation needed.
2712 __ testl(eax, eax);
2713 __ j(kNotEqual, &ndiv);
2714
2715 __ xorl(out, out);
2716 __ jmp(&end);
2717
2718 __ Bind(&ndiv);
2719
2720 // Save the numerator.
2721 __ movl(num, eax);
2722
2723 // EAX = magic
2724 __ movl(eax, Immediate(magic));
2725
2726 // EDX:EAX = magic * numerator
2727 __ imull(num);
2728
2729 if (imm > 0 && magic < 0) {
2730 // EDX += num
2731 __ addl(edx, num);
2732 } else if (imm < 0 && magic > 0) {
2733 __ subl(edx, num);
2734 }
2735
2736 // Shift if needed.
2737 if (shift != 0) {
2738 __ sarl(edx, Immediate(shift));
2739 }
2740
2741 // EDX += 1 if EDX < 0
2742 __ movl(eax, edx);
2743 __ shrl(edx, Immediate(31));
2744 __ addl(edx, eax);
2745
2746 if (instruction->IsRem()) {
2747 __ movl(eax, num);
2748 __ imull(edx, Immediate(imm));
2749 __ subl(eax, edx);
2750 __ movl(edx, eax);
2751 } else {
2752 __ movl(eax, edx);
2753 }
2754 __ Bind(&end);
2755}
2756
Calin Juravlebacfec32014-11-14 15:54:36 +00002757void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2758 DCHECK(instruction->IsDiv() || instruction->IsRem());
2759
2760 LocationSummary* locations = instruction->GetLocations();
2761 Location out = locations->Out();
2762 Location first = locations->InAt(0);
2763 Location second = locations->InAt(1);
2764 bool is_div = instruction->IsDiv();
2765
2766 switch (instruction->GetResultType()) {
2767 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002768 DCHECK_EQ(EAX, first.AsRegister<Register>());
2769 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002770
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002771 if (instruction->InputAt(1)->IsIntConstant()) {
2772 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002773
2774 if (imm == 0) {
2775 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2776 } else if (imm == 1 || imm == -1) {
2777 DivRemOneOrMinusOne(instruction);
2778 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002779 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002780 } else {
2781 DCHECK(imm <= -2 || imm >= 2);
2782 GenerateDivRemWithAnyConstant(instruction);
2783 }
2784 } else {
2785 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002786 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002787 is_div);
2788 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002789
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002790 Register second_reg = second.AsRegister<Register>();
2791 // 0x80000000/-1 triggers an arithmetic exception!
2792 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2793 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002794
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002795 __ cmpl(second_reg, Immediate(-1));
2796 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002797
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002798 // edx:eax <- sign-extended of eax
2799 __ cdq();
2800 // eax = quotient, edx = remainder
2801 __ idivl(second_reg);
2802 __ Bind(slow_path->GetExitLabel());
2803 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002804 break;
2805 }
2806
2807 case Primitive::kPrimLong: {
2808 InvokeRuntimeCallingConvention calling_convention;
2809 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2810 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2811 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2812 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2813 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2814 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2815
2816 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002817 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2818 instruction,
2819 instruction->GetDexPc(),
2820 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002821 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002822 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2823 instruction,
2824 instruction->GetDexPc(),
2825 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002826 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002827 break;
2828 }
2829
2830 default:
2831 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2832 }
2833}
2834
Calin Juravle7c4954d2014-10-28 16:57:40 +00002835void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002836 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002837 ? LocationSummary::kCall
2838 : LocationSummary::kNoCall;
2839 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2840
Calin Juravle7c4954d2014-10-28 16:57:40 +00002841 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002842 case Primitive::kPrimInt: {
2843 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002844 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002845 locations->SetOut(Location::SameAsFirstInput());
2846 // Intel uses edx:eax as the dividend.
2847 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002848 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2849 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2850 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002851 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002852 locations->AddTemp(Location::RequiresRegister());
2853 }
Calin Juravled0d48522014-11-04 16:40:20 +00002854 break;
2855 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002856 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002857 InvokeRuntimeCallingConvention calling_convention;
2858 locations->SetInAt(0, Location::RegisterPairLocation(
2859 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2860 locations->SetInAt(1, Location::RegisterPairLocation(
2861 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2862 // Runtime helper puts the result in EAX, EDX.
2863 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002864 break;
2865 }
2866 case Primitive::kPrimFloat:
2867 case Primitive::kPrimDouble: {
2868 locations->SetInAt(0, Location::RequiresFpuRegister());
2869 locations->SetInAt(1, Location::RequiresFpuRegister());
2870 locations->SetOut(Location::SameAsFirstInput());
2871 break;
2872 }
2873
2874 default:
2875 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2876 }
2877}
2878
2879void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2880 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002881 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002882 Location first = locations->InAt(0);
2883 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002884
2885 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002886 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002887 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002888 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002889 break;
2890 }
2891
2892 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002893 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002894 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002895 break;
2896 }
2897
2898 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002899 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002900 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002901 break;
2902 }
2903
2904 default:
2905 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2906 }
2907}
2908
Calin Juravlebacfec32014-11-14 15:54:36 +00002909void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002910 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002911
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002912 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2913 ? LocationSummary::kCall
2914 : LocationSummary::kNoCall;
2915 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002916
Calin Juravled2ec87d2014-12-08 14:24:46 +00002917 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002918 case Primitive::kPrimInt: {
2919 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002920 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002921 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002922 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2923 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2924 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002925 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002926 locations->AddTemp(Location::RequiresRegister());
2927 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002928 break;
2929 }
2930 case Primitive::kPrimLong: {
2931 InvokeRuntimeCallingConvention calling_convention;
2932 locations->SetInAt(0, Location::RegisterPairLocation(
2933 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2934 locations->SetInAt(1, Location::RegisterPairLocation(
2935 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2936 // Runtime helper puts the result in EAX, EDX.
2937 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2938 break;
2939 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002940 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002941 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002942 locations->SetInAt(0, Location::Any());
2943 locations->SetInAt(1, Location::Any());
2944 locations->SetOut(Location::RequiresFpuRegister());
2945 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002946 break;
2947 }
2948
2949 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002950 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002951 }
2952}
2953
2954void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2955 Primitive::Type type = rem->GetResultType();
2956 switch (type) {
2957 case Primitive::kPrimInt:
2958 case Primitive::kPrimLong: {
2959 GenerateDivRemIntegral(rem);
2960 break;
2961 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002962 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002963 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002964 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002965 break;
2966 }
2967 default:
2968 LOG(FATAL) << "Unexpected rem type " << type;
2969 }
2970}
2971
Calin Juravled0d48522014-11-04 16:40:20 +00002972void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2973 LocationSummary* locations =
2974 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002975 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002976 case Primitive::kPrimByte:
2977 case Primitive::kPrimChar:
2978 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002979 case Primitive::kPrimInt: {
2980 locations->SetInAt(0, Location::Any());
2981 break;
2982 }
2983 case Primitive::kPrimLong: {
2984 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2985 if (!instruction->IsConstant()) {
2986 locations->AddTemp(Location::RequiresRegister());
2987 }
2988 break;
2989 }
2990 default:
2991 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2992 }
Calin Juravled0d48522014-11-04 16:40:20 +00002993 if (instruction->HasUses()) {
2994 locations->SetOut(Location::SameAsFirstInput());
2995 }
2996}
2997
2998void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2999 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
3000 codegen_->AddSlowPath(slow_path);
3001
3002 LocationSummary* locations = instruction->GetLocations();
3003 Location value = locations->InAt(0);
3004
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003005 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003006 case Primitive::kPrimByte:
3007 case Primitive::kPrimChar:
3008 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003009 case Primitive::kPrimInt: {
3010 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003011 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003012 __ j(kEqual, slow_path->GetEntryLabel());
3013 } else if (value.IsStackSlot()) {
3014 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3015 __ j(kEqual, slow_path->GetEntryLabel());
3016 } else {
3017 DCHECK(value.IsConstant()) << value;
3018 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3019 __ jmp(slow_path->GetEntryLabel());
3020 }
3021 }
3022 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003023 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003024 case Primitive::kPrimLong: {
3025 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003026 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003027 __ movl(temp, value.AsRegisterPairLow<Register>());
3028 __ orl(temp, value.AsRegisterPairHigh<Register>());
3029 __ j(kEqual, slow_path->GetEntryLabel());
3030 } else {
3031 DCHECK(value.IsConstant()) << value;
3032 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3033 __ jmp(slow_path->GetEntryLabel());
3034 }
3035 }
3036 break;
3037 }
3038 default:
3039 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003040 }
Calin Juravled0d48522014-11-04 16:40:20 +00003041}
3042
Calin Juravle9aec02f2014-11-18 23:06:35 +00003043void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3044 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3045
3046 LocationSummary* locations =
3047 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3048
3049 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003050 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003051 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003052 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003053 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003054 // The shift count needs to be in CL or a constant.
3055 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003056 locations->SetOut(Location::SameAsFirstInput());
3057 break;
3058 }
3059 default:
3060 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3061 }
3062}
3063
3064void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3065 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3066
3067 LocationSummary* locations = op->GetLocations();
3068 Location first = locations->InAt(0);
3069 Location second = locations->InAt(1);
3070 DCHECK(first.Equals(locations->Out()));
3071
3072 switch (op->GetResultType()) {
3073 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003074 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003075 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003076 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003077 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003078 DCHECK_EQ(ECX, second_reg);
3079 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003080 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003081 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003082 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003083 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003084 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003085 }
3086 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003087 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3088 if (shift == 0) {
3089 return;
3090 }
3091 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003092 if (op->IsShl()) {
3093 __ shll(first_reg, imm);
3094 } else if (op->IsShr()) {
3095 __ sarl(first_reg, imm);
3096 } else {
3097 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003098 }
3099 }
3100 break;
3101 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003102 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003103 if (second.IsRegister()) {
3104 Register second_reg = second.AsRegister<Register>();
3105 DCHECK_EQ(ECX, second_reg);
3106 if (op->IsShl()) {
3107 GenerateShlLong(first, second_reg);
3108 } else if (op->IsShr()) {
3109 GenerateShrLong(first, second_reg);
3110 } else {
3111 GenerateUShrLong(first, second_reg);
3112 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003113 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003114 // Shift by a constant.
3115 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3116 // Nothing to do if the shift is 0, as the input is already the output.
3117 if (shift != 0) {
3118 if (op->IsShl()) {
3119 GenerateShlLong(first, shift);
3120 } else if (op->IsShr()) {
3121 GenerateShrLong(first, shift);
3122 } else {
3123 GenerateUShrLong(first, shift);
3124 }
3125 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003126 }
3127 break;
3128 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003129 default:
3130 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3131 }
3132}
3133
Mark P Mendell73945692015-04-29 14:56:17 +00003134void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3135 Register low = loc.AsRegisterPairLow<Register>();
3136 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003137 if (shift == 1) {
3138 // This is just an addition.
3139 __ addl(low, low);
3140 __ adcl(high, high);
3141 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003142 // Shift by 32 is easy. High gets low, and low gets 0.
3143 codegen_->EmitParallelMoves(
3144 loc.ToLow(),
3145 loc.ToHigh(),
3146 Primitive::kPrimInt,
3147 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3148 loc.ToLow(),
3149 Primitive::kPrimInt);
3150 } else if (shift > 32) {
3151 // Low part becomes 0. High part is low part << (shift-32).
3152 __ movl(high, low);
3153 __ shll(high, Immediate(shift - 32));
3154 __ xorl(low, low);
3155 } else {
3156 // Between 1 and 31.
3157 __ shld(high, low, Immediate(shift));
3158 __ shll(low, Immediate(shift));
3159 }
3160}
3161
Calin Juravle9aec02f2014-11-18 23:06:35 +00003162void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
3163 Label done;
3164 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3165 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3166 __ testl(shifter, Immediate(32));
3167 __ j(kEqual, &done);
3168 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3169 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3170 __ Bind(&done);
3171}
3172
Mark P Mendell73945692015-04-29 14:56:17 +00003173void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3174 Register low = loc.AsRegisterPairLow<Register>();
3175 Register high = loc.AsRegisterPairHigh<Register>();
3176 if (shift == 32) {
3177 // Need to copy the sign.
3178 DCHECK_NE(low, high);
3179 __ movl(low, high);
3180 __ sarl(high, Immediate(31));
3181 } else if (shift > 32) {
3182 DCHECK_NE(low, high);
3183 // High part becomes sign. Low part is shifted by shift - 32.
3184 __ movl(low, high);
3185 __ sarl(high, Immediate(31));
3186 __ sarl(low, Immediate(shift - 32));
3187 } else {
3188 // Between 1 and 31.
3189 __ shrd(low, high, Immediate(shift));
3190 __ sarl(high, Immediate(shift));
3191 }
3192}
3193
Calin Juravle9aec02f2014-11-18 23:06:35 +00003194void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
3195 Label done;
3196 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3197 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3198 __ testl(shifter, Immediate(32));
3199 __ j(kEqual, &done);
3200 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3201 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3202 __ Bind(&done);
3203}
3204
Mark P Mendell73945692015-04-29 14:56:17 +00003205void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3206 Register low = loc.AsRegisterPairLow<Register>();
3207 Register high = loc.AsRegisterPairHigh<Register>();
3208 if (shift == 32) {
3209 // Shift by 32 is easy. Low gets high, and high gets 0.
3210 codegen_->EmitParallelMoves(
3211 loc.ToHigh(),
3212 loc.ToLow(),
3213 Primitive::kPrimInt,
3214 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3215 loc.ToHigh(),
3216 Primitive::kPrimInt);
3217 } else if (shift > 32) {
3218 // Low part is high >> (shift - 32). High part becomes 0.
3219 __ movl(low, high);
3220 __ shrl(low, Immediate(shift - 32));
3221 __ xorl(high, high);
3222 } else {
3223 // Between 1 and 31.
3224 __ shrd(low, high, Immediate(shift));
3225 __ shrl(high, Immediate(shift));
3226 }
3227}
3228
Calin Juravle9aec02f2014-11-18 23:06:35 +00003229void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
3230 Label done;
3231 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3232 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3233 __ testl(shifter, Immediate(32));
3234 __ j(kEqual, &done);
3235 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3236 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3237 __ Bind(&done);
3238}
3239
3240void LocationsBuilderX86::VisitShl(HShl* shl) {
3241 HandleShift(shl);
3242}
3243
3244void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3245 HandleShift(shl);
3246}
3247
3248void LocationsBuilderX86::VisitShr(HShr* shr) {
3249 HandleShift(shr);
3250}
3251
3252void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3253 HandleShift(shr);
3254}
3255
3256void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3257 HandleShift(ushr);
3258}
3259
3260void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3261 HandleShift(ushr);
3262}
3263
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003264void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003265 LocationSummary* locations =
3266 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003267 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003268 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003269 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003270 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003271}
3272
3273void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3274 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003275 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003276 // Note: if heap poisoning is enabled, the entry point takes cares
3277 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003278 codegen_->InvokeRuntime(
3279 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3280 instruction,
3281 instruction->GetDexPc(),
3282 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003283 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003284}
3285
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003286void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3287 LocationSummary* locations =
3288 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3289 locations->SetOut(Location::RegisterLocation(EAX));
3290 InvokeRuntimeCallingConvention calling_convention;
3291 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003292 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003293 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003294}
3295
3296void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3297 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003298 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3299
Roland Levillain4d027112015-07-01 15:41:14 +01003300 // Note: if heap poisoning is enabled, the entry point takes cares
3301 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003302 codegen_->InvokeRuntime(
3303 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3304 instruction,
3305 instruction->GetDexPc(),
3306 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003307 DCHECK(!codegen_->IsLeafMethod());
3308}
3309
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003310void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003311 LocationSummary* locations =
3312 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003313 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3314 if (location.IsStackSlot()) {
3315 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3316 } else if (location.IsDoubleStackSlot()) {
3317 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003318 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003319 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003320}
3321
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003322void InstructionCodeGeneratorX86::VisitParameterValue(
3323 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3324}
3325
3326void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3327 LocationSummary* locations =
3328 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3329 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3330}
3331
3332void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003333}
3334
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003335void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003336 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003337 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003338 locations->SetInAt(0, Location::RequiresRegister());
3339 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003340}
3341
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003342void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3343 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003344 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003345 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003346 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003347 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003348 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003349 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003350 break;
3351
3352 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003353 __ notl(out.AsRegisterPairLow<Register>());
3354 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003355 break;
3356
3357 default:
3358 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3359 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003360}
3361
David Brazdil66d126e2015-04-03 16:02:44 +01003362void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3363 LocationSummary* locations =
3364 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3365 locations->SetInAt(0, Location::RequiresRegister());
3366 locations->SetOut(Location::SameAsFirstInput());
3367}
3368
3369void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003370 LocationSummary* locations = bool_not->GetLocations();
3371 Location in = locations->InAt(0);
3372 Location out = locations->Out();
3373 DCHECK(in.Equals(out));
3374 __ xorl(out.AsRegister<Register>(), Immediate(1));
3375}
3376
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003377void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003378 LocationSummary* locations =
3379 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003380 switch (compare->InputAt(0)->GetType()) {
3381 case Primitive::kPrimLong: {
3382 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003383 locations->SetInAt(1, Location::Any());
3384 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3385 break;
3386 }
3387 case Primitive::kPrimFloat:
3388 case Primitive::kPrimDouble: {
3389 locations->SetInAt(0, Location::RequiresFpuRegister());
3390 locations->SetInAt(1, Location::RequiresFpuRegister());
3391 locations->SetOut(Location::RequiresRegister());
3392 break;
3393 }
3394 default:
3395 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3396 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003397}
3398
3399void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003400 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003401 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003402 Location left = locations->InAt(0);
3403 Location right = locations->InAt(1);
3404
3405 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003406 switch (compare->InputAt(0)->GetType()) {
3407 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003408 Register left_low = left.AsRegisterPairLow<Register>();
3409 Register left_high = left.AsRegisterPairHigh<Register>();
3410 int32_t val_low = 0;
3411 int32_t val_high = 0;
3412 bool right_is_const = false;
3413
3414 if (right.IsConstant()) {
3415 DCHECK(right.GetConstant()->IsLongConstant());
3416 right_is_const = true;
3417 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3418 val_low = Low32Bits(val);
3419 val_high = High32Bits(val);
3420 }
3421
Calin Juravleddb7df22014-11-25 20:56:51 +00003422 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003423 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003424 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003425 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003426 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003427 DCHECK(right_is_const) << right;
3428 if (val_high == 0) {
3429 __ testl(left_high, left_high);
3430 } else {
3431 __ cmpl(left_high, Immediate(val_high));
3432 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003433 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003434 __ j(kLess, &less); // Signed compare.
3435 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003436 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003437 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003438 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003439 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003440 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003441 DCHECK(right_is_const) << right;
3442 if (val_low == 0) {
3443 __ testl(left_low, left_low);
3444 } else {
3445 __ cmpl(left_low, Immediate(val_low));
3446 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003447 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003448 break;
3449 }
3450 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003451 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003452 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3453 break;
3454 }
3455 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003456 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003457 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003458 break;
3459 }
3460 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003461 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003462 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003463 __ movl(out, Immediate(0));
3464 __ j(kEqual, &done);
3465 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3466
3467 __ Bind(&greater);
3468 __ movl(out, Immediate(1));
3469 __ jmp(&done);
3470
3471 __ Bind(&less);
3472 __ movl(out, Immediate(-1));
3473
3474 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003475}
3476
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003477void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003478 LocationSummary* locations =
3479 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003480 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3481 locations->SetInAt(i, Location::Any());
3482 }
3483 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003484}
3485
3486void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003487 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003488 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003489}
3490
Calin Juravle52c48962014-12-16 17:02:57 +00003491void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3492 /*
3493 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3494 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3495 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3496 */
3497 switch (kind) {
3498 case MemBarrierKind::kAnyAny: {
3499 __ mfence();
3500 break;
3501 }
3502 case MemBarrierKind::kAnyStore:
3503 case MemBarrierKind::kLoadAny:
3504 case MemBarrierKind::kStoreStore: {
3505 // nop
3506 break;
3507 }
3508 default:
3509 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003510 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003511}
3512
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003513
Vladimir Marko58155012015-08-19 12:49:41 +00003514void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3515 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3516 switch (invoke->GetMethodLoadKind()) {
3517 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3518 // temp = thread->string_init_entrypoint
3519 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3520 break;
3521 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3522 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3523 break;
3524 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3525 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3526 break;
3527 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3528 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3529 method_patches_.emplace_back(invoke->GetTargetMethod());
3530 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3531 break;
3532 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3533 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3534 FALLTHROUGH_INTENDED;
3535 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3536 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3537 Register method_reg;
3538 Register reg = temp.AsRegister<Register>();
3539 if (current_method.IsRegister()) {
3540 method_reg = current_method.AsRegister<Register>();
3541 } else {
3542 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3543 DCHECK(!current_method.IsValid());
3544 method_reg = reg;
3545 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3546 }
3547 // temp = temp->dex_cache_resolved_methods_;
3548 __ movl(reg, Address(method_reg, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3549 // temp = temp[index_in_cache]
3550 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3551 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3552 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003553 }
Vladimir Marko58155012015-08-19 12:49:41 +00003554 }
3555
3556 switch (invoke->GetCodePtrLocation()) {
3557 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3558 __ call(GetFrameEntryLabel());
3559 break;
3560 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3561 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3562 Label* label = &relative_call_patches_.back().label;
3563 __ call(label); // Bind to the patch label, override at link time.
3564 __ Bind(label); // Bind the label at the end of the "call" insn.
3565 break;
3566 }
3567 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3568 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3569 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3570 // (Though the direct CALL ptr16:32 is available for consideration).
3571 FALLTHROUGH_INTENDED;
3572 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3573 // (callee_method + offset_of_quick_compiled_code)()
3574 __ call(Address(callee_method.AsRegister<Register>(),
3575 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3576 kX86WordSize).Int32Value()));
3577 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003578 }
3579
3580 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003581}
3582
Vladimir Marko58155012015-08-19 12:49:41 +00003583void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3584 DCHECK(linker_patches->empty());
3585 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3586 for (const MethodPatchInfo<Label>& info : method_patches_) {
3587 // The label points to the end of the "movl" insn but the literal offset for method
3588 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3589 uint32_t literal_offset = info.label.Position() - 4;
3590 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3591 info.target_method.dex_file,
3592 info.target_method.dex_method_index));
3593 }
3594 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3595 // The label points to the end of the "call" insn but the literal offset for method
3596 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3597 uint32_t literal_offset = info.label.Position() - 4;
3598 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3599 info.target_method.dex_file,
3600 info.target_method.dex_method_index));
3601 }
3602}
3603
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003604void CodeGeneratorX86::MarkGCCard(Register temp,
3605 Register card,
3606 Register object,
3607 Register value,
3608 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003609 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003610 if (value_can_be_null) {
3611 __ testl(value, value);
3612 __ j(kEqual, &is_null);
3613 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003614 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3615 __ movl(temp, object);
3616 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003617 __ movb(Address(temp, card, TIMES_1, 0),
3618 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003619 if (value_can_be_null) {
3620 __ Bind(&is_null);
3621 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003622}
3623
Calin Juravle52c48962014-12-16 17:02:57 +00003624void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3625 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003626 LocationSummary* locations =
3627 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003628 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003629
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003630 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3631 locations->SetOut(Location::RequiresFpuRegister());
3632 } else {
3633 // The output overlaps in case of long: we don't want the low move to overwrite
3634 // the object's location.
3635 locations->SetOut(Location::RequiresRegister(),
3636 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3637 : Location::kNoOutputOverlap);
3638 }
Calin Juravle52c48962014-12-16 17:02:57 +00003639
3640 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3641 // Long values can be loaded atomically into an XMM using movsd.
3642 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3643 // and then copy the XMM into the output 32bits at a time).
3644 locations->AddTemp(Location::RequiresFpuRegister());
3645 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003646}
3647
Calin Juravle52c48962014-12-16 17:02:57 +00003648void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3649 const FieldInfo& field_info) {
3650 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003651
Calin Juravle52c48962014-12-16 17:02:57 +00003652 LocationSummary* locations = instruction->GetLocations();
3653 Register base = locations->InAt(0).AsRegister<Register>();
3654 Location out = locations->Out();
3655 bool is_volatile = field_info.IsVolatile();
3656 Primitive::Type field_type = field_info.GetFieldType();
3657 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3658
3659 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003660 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003661 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003662 break;
3663 }
3664
3665 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003666 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003667 break;
3668 }
3669
3670 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003671 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003672 break;
3673 }
3674
3675 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003676 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003677 break;
3678 }
3679
3680 case Primitive::kPrimInt:
3681 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003682 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003683 break;
3684 }
3685
3686 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003687 if (is_volatile) {
3688 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3689 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003690 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003691 __ movd(out.AsRegisterPairLow<Register>(), temp);
3692 __ psrlq(temp, Immediate(32));
3693 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3694 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003695 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003696 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003697 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003698 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3699 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003700 break;
3701 }
3702
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003703 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003704 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003705 break;
3706 }
3707
3708 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003709 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003710 break;
3711 }
3712
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003713 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003714 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003715 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003716 }
Calin Juravle52c48962014-12-16 17:02:57 +00003717
Calin Juravle77520bc2015-01-12 18:45:46 +00003718 // Longs are handled in the switch.
3719 if (field_type != Primitive::kPrimLong) {
3720 codegen_->MaybeRecordImplicitNullCheck(instruction);
3721 }
3722
Calin Juravle52c48962014-12-16 17:02:57 +00003723 if (is_volatile) {
3724 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3725 }
Roland Levillain4d027112015-07-01 15:41:14 +01003726
3727 if (field_type == Primitive::kPrimNot) {
3728 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3729 }
Calin Juravle52c48962014-12-16 17:02:57 +00003730}
3731
3732void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3733 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3734
3735 LocationSummary* locations =
3736 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3737 locations->SetInAt(0, Location::RequiresRegister());
3738 bool is_volatile = field_info.IsVolatile();
3739 Primitive::Type field_type = field_info.GetFieldType();
3740 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3741 || (field_type == Primitive::kPrimByte);
3742
3743 // The register allocator does not support multiple
3744 // inputs that die at entry with one in a specific register.
3745 if (is_byte_type) {
3746 // Ensure the value is in a byte register.
3747 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003748 } else if (Primitive::IsFloatingPointType(field_type)) {
3749 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003750 } else {
3751 locations->SetInAt(1, Location::RequiresRegister());
3752 }
Calin Juravle52c48962014-12-16 17:02:57 +00003753 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003754 // Temporary registers for the write barrier.
3755 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003756 // Ensure the card is in a byte register.
3757 locations->AddTemp(Location::RegisterLocation(ECX));
3758 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3759 // 64bits value can be atomically written to an address with movsd and an XMM register.
3760 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3761 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3762 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3763 // isolated cases when we need this it isn't worth adding the extra complexity.
3764 locations->AddTemp(Location::RequiresFpuRegister());
3765 locations->AddTemp(Location::RequiresFpuRegister());
3766 }
3767}
3768
3769void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003770 const FieldInfo& field_info,
3771 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003772 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3773
3774 LocationSummary* locations = instruction->GetLocations();
3775 Register base = locations->InAt(0).AsRegister<Register>();
3776 Location value = locations->InAt(1);
3777 bool is_volatile = field_info.IsVolatile();
3778 Primitive::Type field_type = field_info.GetFieldType();
3779 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003780 bool needs_write_barrier =
3781 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003782
3783 if (is_volatile) {
3784 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3785 }
3786
3787 switch (field_type) {
3788 case Primitive::kPrimBoolean:
3789 case Primitive::kPrimByte: {
3790 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3791 break;
3792 }
3793
3794 case Primitive::kPrimShort:
3795 case Primitive::kPrimChar: {
3796 __ movw(Address(base, offset), value.AsRegister<Register>());
3797 break;
3798 }
3799
3800 case Primitive::kPrimInt:
3801 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003802 if (kPoisonHeapReferences && needs_write_barrier) {
3803 // Note that in the case where `value` is a null reference,
3804 // we do not enter this block, as the reference does not
3805 // need poisoning.
3806 DCHECK_EQ(field_type, Primitive::kPrimNot);
3807 Register temp = locations->GetTemp(0).AsRegister<Register>();
3808 __ movl(temp, value.AsRegister<Register>());
3809 __ PoisonHeapReference(temp);
3810 __ movl(Address(base, offset), temp);
3811 } else {
3812 __ movl(Address(base, offset), value.AsRegister<Register>());
3813 }
Calin Juravle52c48962014-12-16 17:02:57 +00003814 break;
3815 }
3816
3817 case Primitive::kPrimLong: {
3818 if (is_volatile) {
3819 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3820 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3821 __ movd(temp1, value.AsRegisterPairLow<Register>());
3822 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3823 __ punpckldq(temp1, temp2);
3824 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003825 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003826 } else {
3827 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003828 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003829 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3830 }
3831 break;
3832 }
3833
3834 case Primitive::kPrimFloat: {
3835 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3836 break;
3837 }
3838
3839 case Primitive::kPrimDouble: {
3840 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3841 break;
3842 }
3843
3844 case Primitive::kPrimVoid:
3845 LOG(FATAL) << "Unreachable type " << field_type;
3846 UNREACHABLE();
3847 }
3848
Calin Juravle77520bc2015-01-12 18:45:46 +00003849 // Longs are handled in the switch.
3850 if (field_type != Primitive::kPrimLong) {
3851 codegen_->MaybeRecordImplicitNullCheck(instruction);
3852 }
3853
Roland Levillain4d027112015-07-01 15:41:14 +01003854 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003855 Register temp = locations->GetTemp(0).AsRegister<Register>();
3856 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003857 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003858 }
3859
Calin Juravle52c48962014-12-16 17:02:57 +00003860 if (is_volatile) {
3861 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3862 }
3863}
3864
3865void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3866 HandleFieldGet(instruction, instruction->GetFieldInfo());
3867}
3868
3869void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3870 HandleFieldGet(instruction, instruction->GetFieldInfo());
3871}
3872
3873void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3874 HandleFieldSet(instruction, instruction->GetFieldInfo());
3875}
3876
3877void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003878 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003879}
3880
3881void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3882 HandleFieldSet(instruction, instruction->GetFieldInfo());
3883}
3884
3885void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003886 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003887}
3888
3889void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3890 HandleFieldGet(instruction, instruction->GetFieldInfo());
3891}
3892
3893void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3894 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003895}
3896
3897void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003898 LocationSummary* locations =
3899 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003900 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3901 ? Location::RequiresRegister()
3902 : Location::Any();
3903 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003904 if (instruction->HasUses()) {
3905 locations->SetOut(Location::SameAsFirstInput());
3906 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003907}
3908
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003909void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003910 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3911 return;
3912 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003913 LocationSummary* locations = instruction->GetLocations();
3914 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003915
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003916 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3917 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3918}
3919
3920void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003921 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003922 codegen_->AddSlowPath(slow_path);
3923
3924 LocationSummary* locations = instruction->GetLocations();
3925 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003926
3927 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003928 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003929 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003930 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003931 } else {
3932 DCHECK(obj.IsConstant()) << obj;
3933 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3934 __ jmp(slow_path->GetEntryLabel());
3935 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003936 }
3937 __ j(kEqual, slow_path->GetEntryLabel());
3938}
3939
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003940void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3941 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3942 GenerateImplicitNullCheck(instruction);
3943 } else {
3944 GenerateExplicitNullCheck(instruction);
3945 }
3946}
3947
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003948void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003949 LocationSummary* locations =
3950 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003951 locations->SetInAt(0, Location::RequiresRegister());
3952 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003953 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3954 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3955 } else {
3956 // The output overlaps in case of long: we don't want the low move to overwrite
3957 // the array's location.
3958 locations->SetOut(Location::RequiresRegister(),
3959 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3960 : Location::kNoOutputOverlap);
3961 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003962}
3963
3964void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3965 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003966 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003967 Location index = locations->InAt(1);
3968
Calin Juravle77520bc2015-01-12 18:45:46 +00003969 Primitive::Type type = instruction->GetType();
3970 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003971 case Primitive::kPrimBoolean: {
3972 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003973 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003974 if (index.IsConstant()) {
3975 __ movzxb(out, Address(obj,
3976 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3977 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003978 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003979 }
3980 break;
3981 }
3982
3983 case Primitive::kPrimByte: {
3984 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003985 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003986 if (index.IsConstant()) {
3987 __ movsxb(out, Address(obj,
3988 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3989 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003990 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003991 }
3992 break;
3993 }
3994
3995 case Primitive::kPrimShort: {
3996 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003997 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003998 if (index.IsConstant()) {
3999 __ movsxw(out, Address(obj,
4000 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4001 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004002 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004003 }
4004 break;
4005 }
4006
4007 case Primitive::kPrimChar: {
4008 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004009 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004010 if (index.IsConstant()) {
4011 __ movzxw(out, Address(obj,
4012 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4013 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004014 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004015 }
4016 break;
4017 }
4018
4019 case Primitive::kPrimInt:
4020 case Primitive::kPrimNot: {
4021 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004022 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004023 if (index.IsConstant()) {
4024 __ movl(out, Address(obj,
4025 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4026 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004027 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004028 }
4029 break;
4030 }
4031
4032 case Primitive::kPrimLong: {
4033 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004034 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004035 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004036 if (index.IsConstant()) {
4037 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004038 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004039 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004040 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004041 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004042 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004043 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004044 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004045 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004046 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004047 }
4048 break;
4049 }
4050
Mark Mendell7c8d0092015-01-26 11:21:33 -05004051 case Primitive::kPrimFloat: {
4052 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4053 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4054 if (index.IsConstant()) {
4055 __ movss(out, Address(obj,
4056 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4057 } else {
4058 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4059 }
4060 break;
4061 }
4062
4063 case Primitive::kPrimDouble: {
4064 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4065 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4066 if (index.IsConstant()) {
4067 __ movsd(out, Address(obj,
4068 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4069 } else {
4070 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4071 }
4072 break;
4073 }
4074
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004075 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004076 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004077 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004078 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004079
4080 if (type != Primitive::kPrimLong) {
4081 codegen_->MaybeRecordImplicitNullCheck(instruction);
4082 }
Roland Levillain4d027112015-07-01 15:41:14 +01004083
4084 if (type == Primitive::kPrimNot) {
4085 Register out = locations->Out().AsRegister<Register>();
4086 __ MaybeUnpoisonHeapReference(out);
4087 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004088}
4089
4090void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004091 // This location builder might end up asking to up to four registers, which is
4092 // not currently possible for baseline. The situation in which we need four
4093 // registers cannot be met by baseline though, because it has not run any
4094 // optimization.
4095
Nicolas Geoffray39468442014-09-02 15:17:15 +01004096 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004097 bool needs_write_barrier =
4098 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4099
Mark Mendell5f874182015-03-04 15:42:45 -05004100 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004101
Nicolas Geoffray39468442014-09-02 15:17:15 +01004102 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4103 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004104 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004105
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004106 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004107 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004108 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4109 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4110 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004111 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004112 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4113 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004114 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004115 // In case of a byte operation, the register allocator does not support multiple
4116 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004117 locations->SetInAt(0, Location::RequiresRegister());
4118 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004119 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004120 // Ensure the value is in a byte register.
4121 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004122 } else if (Primitive::IsFloatingPointType(value_type)) {
4123 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004124 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004125 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004126 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004127 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004128 // Temporary registers for the write barrier.
4129 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004130 // Ensure the card is in a byte register.
4131 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004132 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004133 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004134}
4135
4136void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4137 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004138 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004139 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004140 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004141 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004142 bool needs_runtime_call = locations->WillCall();
4143 bool needs_write_barrier =
4144 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004145
4146 switch (value_type) {
4147 case Primitive::kPrimBoolean:
4148 case Primitive::kPrimByte: {
4149 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004150 if (index.IsConstant()) {
4151 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004152 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004153 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004154 } else {
4155 __ movb(Address(obj, offset),
4156 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4157 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004158 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004159 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004160 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004161 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004162 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004163 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004164 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4165 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004166 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004167 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004168 break;
4169 }
4170
4171 case Primitive::kPrimShort:
4172 case Primitive::kPrimChar: {
4173 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004174 if (index.IsConstant()) {
4175 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004176 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004177 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004178 } else {
4179 __ movw(Address(obj, offset),
4180 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4181 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004182 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004183 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004184 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4185 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004186 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004187 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004188 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4189 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004190 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004191 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004192 break;
4193 }
4194
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004195 case Primitive::kPrimInt:
4196 case Primitive::kPrimNot: {
4197 if (!needs_runtime_call) {
4198 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4199 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004200 size_t offset =
4201 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004202 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004203 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4204 Register temp = locations->GetTemp(0).AsRegister<Register>();
4205 __ movl(temp, value.AsRegister<Register>());
4206 __ PoisonHeapReference(temp);
4207 __ movl(Address(obj, offset), temp);
4208 } else {
4209 __ movl(Address(obj, offset), value.AsRegister<Register>());
4210 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004211 } else {
4212 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004213 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4214 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4215 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4216 // Note: if heap poisoning is enabled, no need to poison
4217 // (negate) `v` if it is a reference, as it would be null.
4218 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004219 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004220 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004221 DCHECK(index.IsRegister()) << index;
4222 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004223 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4224 Register temp = locations->GetTemp(0).AsRegister<Register>();
4225 __ movl(temp, value.AsRegister<Register>());
4226 __ PoisonHeapReference(temp);
4227 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4228 } else {
4229 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4230 value.AsRegister<Register>());
4231 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004232 } else {
4233 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004234 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4235 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4236 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4237 // Note: if heap poisoning is enabled, no need to poison
4238 // (negate) `v` if it is a reference, as it would be null.
4239 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004240 }
4241 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004242 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004243
4244 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004245 Register temp = locations->GetTemp(0).AsRegister<Register>();
4246 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004247 codegen_->MarkGCCard(
4248 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004249 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004250 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004251 DCHECK_EQ(value_type, Primitive::kPrimNot);
4252 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004253 // Note: if heap poisoning is enabled, pAputObject takes cares
4254 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004255 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4256 instruction,
4257 instruction->GetDexPc(),
4258 nullptr);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004259 }
4260 break;
4261 }
4262
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004263 case Primitive::kPrimLong: {
4264 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004265 if (index.IsConstant()) {
4266 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004267 if (value.IsRegisterPair()) {
4268 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004269 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004270 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004271 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004272 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004273 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4274 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004275 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004276 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4277 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004278 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004279 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004280 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004281 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004282 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004283 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004284 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004285 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004286 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004287 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004288 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004289 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004290 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004291 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004292 Immediate(High32Bits(val)));
4293 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004294 }
4295 break;
4296 }
4297
Mark Mendell7c8d0092015-01-26 11:21:33 -05004298 case Primitive::kPrimFloat: {
4299 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4300 DCHECK(value.IsFpuRegister());
4301 if (index.IsConstant()) {
4302 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4303 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4304 } else {
4305 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4306 value.AsFpuRegister<XmmRegister>());
4307 }
4308 break;
4309 }
4310
4311 case Primitive::kPrimDouble: {
4312 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4313 DCHECK(value.IsFpuRegister());
4314 if (index.IsConstant()) {
4315 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4316 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4317 } else {
4318 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4319 value.AsFpuRegister<XmmRegister>());
4320 }
4321 break;
4322 }
4323
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004324 case Primitive::kPrimVoid:
4325 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004326 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004327 }
4328}
4329
4330void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4331 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004332 locations->SetInAt(0, Location::RequiresRegister());
4333 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004334}
4335
4336void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4337 LocationSummary* locations = instruction->GetLocations();
4338 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004339 Register obj = locations->InAt(0).AsRegister<Register>();
4340 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004341 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004342 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004343}
4344
4345void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004346 LocationSummary* locations =
4347 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004348 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004349 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004350 if (instruction->HasUses()) {
4351 locations->SetOut(Location::SameAsFirstInput());
4352 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004353}
4354
4355void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4356 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004357 Location index_loc = locations->InAt(0);
4358 Location length_loc = locations->InAt(1);
4359 SlowPathCodeX86* slow_path =
4360 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004361
Mark Mendell99dbd682015-04-22 16:18:52 -04004362 if (length_loc.IsConstant()) {
4363 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4364 if (index_loc.IsConstant()) {
4365 // BCE will remove the bounds check if we are guarenteed to pass.
4366 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4367 if (index < 0 || index >= length) {
4368 codegen_->AddSlowPath(slow_path);
4369 __ jmp(slow_path->GetEntryLabel());
4370 } else {
4371 // Some optimization after BCE may have generated this, and we should not
4372 // generate a bounds check if it is a valid range.
4373 }
4374 return;
4375 }
4376
4377 // We have to reverse the jump condition because the length is the constant.
4378 Register index_reg = index_loc.AsRegister<Register>();
4379 __ cmpl(index_reg, Immediate(length));
4380 codegen_->AddSlowPath(slow_path);
4381 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004382 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004383 Register length = length_loc.AsRegister<Register>();
4384 if (index_loc.IsConstant()) {
4385 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4386 __ cmpl(length, Immediate(value));
4387 } else {
4388 __ cmpl(length, index_loc.AsRegister<Register>());
4389 }
4390 codegen_->AddSlowPath(slow_path);
4391 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004392 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004393}
4394
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004395void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4396 temp->SetLocations(nullptr);
4397}
4398
4399void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4400 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004401 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004402}
4403
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004404void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004405 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004406 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004407}
4408
4409void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004410 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4411}
4412
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004413void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4414 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4415}
4416
4417void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004418 HBasicBlock* block = instruction->GetBlock();
4419 if (block->GetLoopInformation() != nullptr) {
4420 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4421 // The back edge will generate the suspend check.
4422 return;
4423 }
4424 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4425 // The goto will generate the suspend check.
4426 return;
4427 }
4428 GenerateSuspendCheck(instruction, nullptr);
4429}
4430
4431void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4432 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004433 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004434 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4435 if (slow_path == nullptr) {
4436 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4437 instruction->SetSlowPath(slow_path);
4438 codegen_->AddSlowPath(slow_path);
4439 if (successor != nullptr) {
4440 DCHECK(successor->IsLoopHeader());
4441 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4442 }
4443 } else {
4444 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4445 }
4446
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004447 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004448 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004449 if (successor == nullptr) {
4450 __ j(kNotEqual, slow_path->GetEntryLabel());
4451 __ Bind(slow_path->GetReturnLabel());
4452 } else {
4453 __ j(kEqual, codegen_->GetLabelOf(successor));
4454 __ jmp(slow_path->GetEntryLabel());
4455 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004456}
4457
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004458X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4459 return codegen_->GetAssembler();
4460}
4461
Mark Mendell7c8d0092015-01-26 11:21:33 -05004462void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004463 ScratchRegisterScope ensure_scratch(
4464 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4465 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4466 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4467 __ movl(temp_reg, Address(ESP, src + stack_offset));
4468 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004469}
4470
4471void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004472 ScratchRegisterScope ensure_scratch(
4473 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4474 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4475 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4476 __ movl(temp_reg, Address(ESP, src + stack_offset));
4477 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4478 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4479 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004480}
4481
4482void ParallelMoveResolverX86::EmitMove(size_t index) {
4483 MoveOperands* move = moves_.Get(index);
4484 Location source = move->GetSource();
4485 Location destination = move->GetDestination();
4486
4487 if (source.IsRegister()) {
4488 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004489 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004490 } else {
4491 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004492 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004493 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004494 } else if (source.IsFpuRegister()) {
4495 if (destination.IsFpuRegister()) {
4496 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4497 } else if (destination.IsStackSlot()) {
4498 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4499 } else {
4500 DCHECK(destination.IsDoubleStackSlot());
4501 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4502 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004503 } else if (source.IsStackSlot()) {
4504 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004505 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004506 } else if (destination.IsFpuRegister()) {
4507 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004508 } else {
4509 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004510 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4511 }
4512 } else if (source.IsDoubleStackSlot()) {
4513 if (destination.IsFpuRegister()) {
4514 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4515 } else {
4516 DCHECK(destination.IsDoubleStackSlot()) << destination;
4517 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004518 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004519 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004520 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004521 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004522 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004523 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004524 if (value == 0) {
4525 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4526 } else {
4527 __ movl(destination.AsRegister<Register>(), Immediate(value));
4528 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004529 } else {
4530 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004531 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004532 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004533 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004534 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004535 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004536 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004537 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004538 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4539 if (value == 0) {
4540 // Easy handling of 0.0.
4541 __ xorps(dest, dest);
4542 } else {
4543 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004544 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4545 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4546 __ movl(temp, Immediate(value));
4547 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004548 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004549 } else {
4550 DCHECK(destination.IsStackSlot()) << destination;
4551 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4552 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004553 } else if (constant->IsLongConstant()) {
4554 int64_t value = constant->AsLongConstant()->GetValue();
4555 int32_t low_value = Low32Bits(value);
4556 int32_t high_value = High32Bits(value);
4557 Immediate low(low_value);
4558 Immediate high(high_value);
4559 if (destination.IsDoubleStackSlot()) {
4560 __ movl(Address(ESP, destination.GetStackIndex()), low);
4561 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4562 } else {
4563 __ movl(destination.AsRegisterPairLow<Register>(), low);
4564 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4565 }
4566 } else {
4567 DCHECK(constant->IsDoubleConstant());
4568 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004569 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004570 int32_t low_value = Low32Bits(value);
4571 int32_t high_value = High32Bits(value);
4572 Immediate low(low_value);
4573 Immediate high(high_value);
4574 if (destination.IsFpuRegister()) {
4575 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4576 if (value == 0) {
4577 // Easy handling of 0.0.
4578 __ xorpd(dest, dest);
4579 } else {
4580 __ pushl(high);
4581 __ pushl(low);
4582 __ movsd(dest, Address(ESP, 0));
4583 __ addl(ESP, Immediate(8));
4584 }
4585 } else {
4586 DCHECK(destination.IsDoubleStackSlot()) << destination;
4587 __ movl(Address(ESP, destination.GetStackIndex()), low);
4588 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4589 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004590 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004591 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004592 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004593 }
4594}
4595
Mark Mendella5c19ce2015-04-01 12:51:05 -04004596void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004597 Register suggested_scratch = reg == EAX ? EBX : EAX;
4598 ScratchRegisterScope ensure_scratch(
4599 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4600
4601 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4602 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4603 __ movl(Address(ESP, mem + stack_offset), reg);
4604 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004605}
4606
Mark Mendell7c8d0092015-01-26 11:21:33 -05004607void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004608 ScratchRegisterScope ensure_scratch(
4609 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4610
4611 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4612 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4613 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4614 __ movss(Address(ESP, mem + stack_offset), reg);
4615 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004616}
4617
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004618void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004619 ScratchRegisterScope ensure_scratch1(
4620 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004621
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004622 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4623 ScratchRegisterScope ensure_scratch2(
4624 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004625
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004626 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4627 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4628 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4629 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4630 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4631 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004632}
4633
4634void ParallelMoveResolverX86::EmitSwap(size_t index) {
4635 MoveOperands* move = moves_.Get(index);
4636 Location source = move->GetSource();
4637 Location destination = move->GetDestination();
4638
4639 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004640 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4641 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4642 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4643 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4644 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004645 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004646 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004647 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004648 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004649 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4650 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004651 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4652 // Use XOR Swap algorithm to avoid a temporary.
4653 DCHECK_NE(source.reg(), destination.reg());
4654 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4655 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4656 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4657 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4658 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4659 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4660 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004661 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4662 // Take advantage of the 16 bytes in the XMM register.
4663 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4664 Address stack(ESP, destination.GetStackIndex());
4665 // Load the double into the high doubleword.
4666 __ movhpd(reg, stack);
4667
4668 // Store the low double into the destination.
4669 __ movsd(stack, reg);
4670
4671 // Move the high double to the low double.
4672 __ psrldq(reg, Immediate(8));
4673 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4674 // Take advantage of the 16 bytes in the XMM register.
4675 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4676 Address stack(ESP, source.GetStackIndex());
4677 // Load the double into the high doubleword.
4678 __ movhpd(reg, stack);
4679
4680 // Store the low double into the destination.
4681 __ movsd(stack, reg);
4682
4683 // Move the high double to the low double.
4684 __ psrldq(reg, Immediate(8));
4685 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4686 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4687 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004688 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004689 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004690 }
4691}
4692
4693void ParallelMoveResolverX86::SpillScratch(int reg) {
4694 __ pushl(static_cast<Register>(reg));
4695}
4696
4697void ParallelMoveResolverX86::RestoreScratch(int reg) {
4698 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004699}
4700
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004701void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004702 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4703 ? LocationSummary::kCallOnSlowPath
4704 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004705 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004706 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004707 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004708 locations->SetOut(Location::RequiresRegister());
4709}
4710
4711void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004712 LocationSummary* locations = cls->GetLocations();
4713 Register out = locations->Out().AsRegister<Register>();
4714 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004715 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004716 DCHECK(!cls->CanCallRuntime());
4717 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004718 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004719 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004720 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004721 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004722 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004723 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004724 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004725
4726 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4727 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4728 codegen_->AddSlowPath(slow_path);
4729 __ testl(out, out);
4730 __ j(kEqual, slow_path->GetEntryLabel());
4731 if (cls->MustGenerateClinitCheck()) {
4732 GenerateClassInitializationCheck(slow_path, out);
4733 } else {
4734 __ Bind(slow_path->GetExitLabel());
4735 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004736 }
4737}
4738
4739void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4740 LocationSummary* locations =
4741 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4742 locations->SetInAt(0, Location::RequiresRegister());
4743 if (check->HasUses()) {
4744 locations->SetOut(Location::SameAsFirstInput());
4745 }
4746}
4747
4748void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004749 // We assume the class to not be null.
4750 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4751 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004752 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004753 GenerateClassInitializationCheck(slow_path,
4754 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004755}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004756
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004757void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4758 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004759 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4760 Immediate(mirror::Class::kStatusInitialized));
4761 __ j(kLess, slow_path->GetEntryLabel());
4762 __ Bind(slow_path->GetExitLabel());
4763 // No need for memory fence, thanks to the X86 memory model.
4764}
4765
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004766void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4767 LocationSummary* locations =
4768 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004769 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004770 locations->SetOut(Location::RequiresRegister());
4771}
4772
4773void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4774 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4775 codegen_->AddSlowPath(slow_path);
4776
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004777 LocationSummary* locations = load->GetLocations();
4778 Register out = locations->Out().AsRegister<Register>();
4779 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004780 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004781 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004782 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004783 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004784 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004785 __ testl(out, out);
4786 __ j(kEqual, slow_path->GetEntryLabel());
4787 __ Bind(slow_path->GetExitLabel());
4788}
4789
David Brazdilcb1c0552015-08-04 16:22:25 +01004790static Address GetExceptionTlsAddress() {
4791 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
4792}
4793
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004794void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4795 LocationSummary* locations =
4796 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4797 locations->SetOut(Location::RequiresRegister());
4798}
4799
4800void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004801 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
4802}
4803
4804void LocationsBuilderX86::VisitClearException(HClearException* clear) {
4805 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4806}
4807
4808void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4809 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004810}
4811
4812void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4813 LocationSummary* locations =
4814 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4815 InvokeRuntimeCallingConvention calling_convention;
4816 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4817}
4818
4819void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004820 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4821 instruction,
4822 instruction->GetDexPc(),
4823 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004824}
4825
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004826void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004827 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4828 ? LocationSummary::kNoCall
4829 : LocationSummary::kCallOnSlowPath;
4830 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4831 locations->SetInAt(0, Location::RequiresRegister());
4832 locations->SetInAt(1, Location::Any());
4833 locations->SetOut(Location::RequiresRegister());
4834}
4835
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004836void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004837 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004838 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004839 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004840 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004841 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4842 Label done, zero;
4843 SlowPathCodeX86* slow_path = nullptr;
4844
4845 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004846 // Avoid null check if we know obj is not null.
4847 if (instruction->MustDoNullCheck()) {
4848 __ testl(obj, obj);
4849 __ j(kEqual, &zero);
4850 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004851 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004852 __ movl(out, Address(obj, class_offset));
4853 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004854 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004855 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004856 } else {
4857 DCHECK(cls.IsStackSlot()) << cls;
4858 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4859 }
4860
4861 if (instruction->IsClassFinal()) {
4862 // Classes must be equal for the instanceof to succeed.
4863 __ j(kNotEqual, &zero);
4864 __ movl(out, Immediate(1));
4865 __ jmp(&done);
4866 } else {
4867 // If the classes are not equal, we go into a slow path.
4868 DCHECK(locations->OnlyCallsOnSlowPath());
4869 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Alexandre Rames98596202015-08-19 11:33:36 +01004870 instruction, locations->InAt(1), locations->Out());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004871 codegen_->AddSlowPath(slow_path);
4872 __ j(kNotEqual, slow_path->GetEntryLabel());
4873 __ movl(out, Immediate(1));
4874 __ jmp(&done);
4875 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004876
4877 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4878 __ Bind(&zero);
4879 __ movl(out, Immediate(0));
4880 }
4881
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004882 if (slow_path != nullptr) {
4883 __ Bind(slow_path->GetExitLabel());
4884 }
4885 __ Bind(&done);
4886}
4887
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004888void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4889 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4890 instruction, LocationSummary::kCallOnSlowPath);
4891 locations->SetInAt(0, Location::RequiresRegister());
4892 locations->SetInAt(1, Location::Any());
4893 locations->AddTemp(Location::RequiresRegister());
4894}
4895
4896void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4897 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004898 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004899 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004900 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004901 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4902 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Alexandre Rames98596202015-08-19 11:33:36 +01004903 instruction, locations->InAt(1), locations->GetTemp(0));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004904 codegen_->AddSlowPath(slow_path);
4905
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004906 // Avoid null check if we know obj is not null.
4907 if (instruction->MustDoNullCheck()) {
4908 __ testl(obj, obj);
4909 __ j(kEqual, slow_path->GetExitLabel());
4910 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004911 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004912 __ movl(temp, Address(obj, class_offset));
4913 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004914 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004915 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004916 } else {
4917 DCHECK(cls.IsStackSlot()) << cls;
4918 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4919 }
Roland Levillain4d027112015-07-01 15:41:14 +01004920 // The checkcast succeeds if the classes are equal (fast path).
4921 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004922 __ j(kNotEqual, slow_path->GetEntryLabel());
4923 __ Bind(slow_path->GetExitLabel());
4924}
4925
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004926void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4927 LocationSummary* locations =
4928 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4929 InvokeRuntimeCallingConvention calling_convention;
4930 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4931}
4932
4933void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004934 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4935 : QUICK_ENTRY_POINT(pUnlockObject),
4936 instruction,
4937 instruction->GetDexPc(),
4938 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004939}
4940
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004941void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4942void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4943void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4944
4945void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4946 LocationSummary* locations =
4947 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4948 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4949 || instruction->GetResultType() == Primitive::kPrimLong);
4950 locations->SetInAt(0, Location::RequiresRegister());
4951 locations->SetInAt(1, Location::Any());
4952 locations->SetOut(Location::SameAsFirstInput());
4953}
4954
4955void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4956 HandleBitwiseOperation(instruction);
4957}
4958
4959void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4960 HandleBitwiseOperation(instruction);
4961}
4962
4963void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4964 HandleBitwiseOperation(instruction);
4965}
4966
4967void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4968 LocationSummary* locations = instruction->GetLocations();
4969 Location first = locations->InAt(0);
4970 Location second = locations->InAt(1);
4971 DCHECK(first.Equals(locations->Out()));
4972
4973 if (instruction->GetResultType() == Primitive::kPrimInt) {
4974 if (second.IsRegister()) {
4975 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004976 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004977 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004978 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004979 } else {
4980 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004981 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004982 }
4983 } else if (second.IsConstant()) {
4984 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004985 __ andl(first.AsRegister<Register>(),
4986 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004987 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004988 __ orl(first.AsRegister<Register>(),
4989 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004990 } else {
4991 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004992 __ xorl(first.AsRegister<Register>(),
4993 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004994 }
4995 } else {
4996 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004997 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004998 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004999 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005000 } else {
5001 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005002 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005003 }
5004 }
5005 } else {
5006 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5007 if (second.IsRegisterPair()) {
5008 if (instruction->IsAnd()) {
5009 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5010 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5011 } else if (instruction->IsOr()) {
5012 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5013 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5014 } else {
5015 DCHECK(instruction->IsXor());
5016 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5017 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5018 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005019 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005020 if (instruction->IsAnd()) {
5021 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5022 __ andl(first.AsRegisterPairHigh<Register>(),
5023 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5024 } else if (instruction->IsOr()) {
5025 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5026 __ orl(first.AsRegisterPairHigh<Register>(),
5027 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5028 } else {
5029 DCHECK(instruction->IsXor());
5030 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5031 __ xorl(first.AsRegisterPairHigh<Register>(),
5032 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5033 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005034 } else {
5035 DCHECK(second.IsConstant()) << second;
5036 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005037 int32_t low_value = Low32Bits(value);
5038 int32_t high_value = High32Bits(value);
5039 Immediate low(low_value);
5040 Immediate high(high_value);
5041 Register first_low = first.AsRegisterPairLow<Register>();
5042 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005043 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005044 if (low_value == 0) {
5045 __ xorl(first_low, first_low);
5046 } else if (low_value != -1) {
5047 __ andl(first_low, low);
5048 }
5049 if (high_value == 0) {
5050 __ xorl(first_high, first_high);
5051 } else if (high_value != -1) {
5052 __ andl(first_high, high);
5053 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005054 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005055 if (low_value != 0) {
5056 __ orl(first_low, low);
5057 }
5058 if (high_value != 0) {
5059 __ orl(first_high, high);
5060 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005061 } else {
5062 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005063 if (low_value != 0) {
5064 __ xorl(first_low, low);
5065 }
5066 if (high_value != 0) {
5067 __ xorl(first_high, high);
5068 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005069 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005070 }
5071 }
5072}
5073
Calin Juravleb1498f62015-02-16 13:13:29 +00005074void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5075 // Nothing to do, this should be removed during prepare for register allocator.
5076 UNUSED(instruction);
5077 LOG(FATAL) << "Unreachable";
5078}
5079
5080void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5081 // Nothing to do, this should be removed during prepare for register allocator.
5082 UNUSED(instruction);
5083 LOG(FATAL) << "Unreachable";
5084}
5085
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005086void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5087 DCHECK(codegen_->IsBaseline());
5088 LocationSummary* locations =
5089 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5090 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5091}
5092
5093void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5094 DCHECK(codegen_->IsBaseline());
5095 // Will be generated at use site.
5096}
5097
Roland Levillain4d027112015-07-01 15:41:14 +01005098#undef __
5099
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005100} // namespace x86
5101} // namespace art