blob: 96ef863f780a46485761830b5c9857b6a09723a5 [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 Marko9b688a02015-05-06 14:12:42 +010021#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,
285 Location object_class,
286 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000288 class_to_check_(class_to_check),
289 object_class_(object_class),
290 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000292 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000294 DCHECK(instruction_->IsCheckCast()
295 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296
297 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
298 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000299 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300
301 // We're moving two locations to locations that could overlap, so we need a parallel
302 // move resolver.
303 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000304 x86_codegen->EmitParallelMoves(
305 class_to_check_,
306 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100307 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000308 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100309 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
310 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000311
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000312 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100313 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
314 instruction_,
315 instruction_->GetDexPc(),
316 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000317 } else {
318 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100319 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
320 instruction_,
321 instruction_->GetDexPc(),
322 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 }
324
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000325 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000326 if (instruction_->IsInstanceOf()) {
327 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
328 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000329 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000330
331 __ jmp(GetExitLabel());
332 }
333
Alexandre Rames9931f312015-06-19 14:47:01 +0100334 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
335
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000337 HInstruction* const instruction_;
338 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000340 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000341
342 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
343};
344
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700345class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
346 public:
347 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
348 : instruction_(instruction) {}
349
350 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100351 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700352 __ Bind(GetEntryLabel());
353 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100354 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
355 instruction_,
356 instruction_->GetDexPc(),
357 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700358 // No need to restore live registers.
359 DCHECK(instruction_->IsDeoptimize());
360 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
361 uint32_t dex_pc = deoptimize->GetDexPc();
362 codegen->RecordPcInfo(instruction_, dex_pc, this);
363 }
364
Alexandre Rames9931f312015-06-19 14:47:01 +0100365 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
366
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700367 private:
368 HInstruction* const instruction_;
369 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
370};
371
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100372#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100373#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100374
Roland Levillain4fa13f62015-07-06 18:11:54 +0100375inline Condition X86SignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700376 switch (cond) {
377 case kCondEQ: return kEqual;
378 case kCondNE: return kNotEqual;
379 case kCondLT: return kLess;
380 case kCondLE: return kLessEqual;
381 case kCondGT: return kGreater;
382 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700383 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100384 LOG(FATAL) << "Unreachable";
385 UNREACHABLE();
386}
387
388inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
389 switch (cond) {
390 case kCondEQ: return kEqual;
391 case kCondNE: return kNotEqual;
392 case kCondLT: return kBelow;
393 case kCondLE: return kBelowEqual;
394 case kCondGT: return kAbove;
395 case kCondGE: return kAboveEqual;
396 }
397 LOG(FATAL) << "Unreachable";
398 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700399}
400
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100401void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100402 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100403}
404
405void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100406 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100407}
408
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100409size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
410 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
411 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100412}
413
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100414size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
415 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
416 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100417}
418
Mark Mendell7c8d0092015-01-26 11:21:33 -0500419size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
420 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
421 return GetFloatingPointSpillSlotSize();
422}
423
424size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
425 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
426 return GetFloatingPointSpillSlotSize();
427}
428
Alexandre Rames8158f282015-08-07 10:26:17 +0100429void CodeGeneratorX86::InvokeRuntime(Address entry_point,
430 HInstruction* instruction,
431 uint32_t dex_pc,
432 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100433 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100434 __ fs()->call(entry_point);
435 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100436}
437
Mark Mendellfb8d2792015-03-31 22:16:59 -0400438CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
439 const X86InstructionSetFeatures& isa_features,
440 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500441 : CodeGenerator(graph,
442 kNumberOfCpuRegisters,
443 kNumberOfXmmRegisters,
444 kNumberOfRegisterPairs,
445 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
446 arraysize(kCoreCalleeSaves))
447 | (1 << kFakeReturnRegister),
448 0,
449 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100450 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100451 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100452 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400453 move_resolver_(graph->GetArena(), this),
Vladimir Marko9b688a02015-05-06 14:12:42 +0100454 isa_features_(isa_features),
455 method_patches_(graph->GetArena()->Adapter()),
456 relative_call_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000457 // Use a fake return address register to mimic Quick.
458 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100459}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100460
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100461Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100462 switch (type) {
463 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100464 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100465 X86ManagedRegister pair =
466 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100467 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
468 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100469 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
470 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100471 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100472 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100473 }
474
475 case Primitive::kPrimByte:
476 case Primitive::kPrimBoolean:
477 case Primitive::kPrimChar:
478 case Primitive::kPrimShort:
479 case Primitive::kPrimInt:
480 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100481 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100482 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100483 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100484 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
485 X86ManagedRegister current =
486 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
487 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100488 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100489 }
490 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100491 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100492 }
493
494 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100495 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100496 return Location::FpuRegisterLocation(
497 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100498 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100499
500 case Primitive::kPrimVoid:
501 LOG(FATAL) << "Unreachable type " << type;
502 }
503
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100504 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100505}
506
Mark Mendell5f874182015-03-04 15:42:45 -0500507void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100508 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100509 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100510
511 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100512 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100513
Mark Mendell5f874182015-03-04 15:42:45 -0500514 if (is_baseline) {
515 blocked_core_registers_[EBP] = true;
516 blocked_core_registers_[ESI] = true;
517 blocked_core_registers_[EDI] = true;
518 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100519
520 UpdateBlockedPairRegisters();
521}
522
523void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
524 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
525 X86ManagedRegister current =
526 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
527 if (blocked_core_registers_[current.AsRegisterPairLow()]
528 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
529 blocked_register_pairs_[i] = true;
530 }
531 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100532}
533
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100534InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
535 : HGraphVisitor(graph),
536 assembler_(codegen->GetAssembler()),
537 codegen_(codegen) {}
538
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100539static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100540 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100541}
542
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000543void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100544 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000545 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000546 bool skip_overflow_check =
547 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000548 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000549
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000550 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100551 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100552 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100553 }
554
Mark Mendell5f874182015-03-04 15:42:45 -0500555 if (HasEmptyFrame()) {
556 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000557 }
Mark Mendell5f874182015-03-04 15:42:45 -0500558
559 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
560 Register reg = kCoreCalleeSaves[i];
561 if (allocated_registers_.ContainsCoreRegister(reg)) {
562 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100563 __ cfi().AdjustCFAOffset(kX86WordSize);
564 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500565 }
566 }
567
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100568 int adjust = GetFrameSize() - FrameEntrySpillSize();
569 __ subl(ESP, Immediate(adjust));
570 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100571 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000572}
573
574void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100575 __ cfi().RememberState();
576 if (!HasEmptyFrame()) {
577 int adjust = GetFrameSize() - FrameEntrySpillSize();
578 __ addl(ESP, Immediate(adjust));
579 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500580
David Srbeckyc34dc932015-04-12 09:27:43 +0100581 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
582 Register reg = kCoreCalleeSaves[i];
583 if (allocated_registers_.ContainsCoreRegister(reg)) {
584 __ popl(reg);
585 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
586 __ cfi().Restore(DWARFReg(reg));
587 }
Mark Mendell5f874182015-03-04 15:42:45 -0500588 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000589 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100590 __ ret();
591 __ cfi().RestoreState();
592 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000593}
594
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100595void CodeGeneratorX86::Bind(HBasicBlock* block) {
596 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000597}
598
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100599Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
600 switch (load->GetType()) {
601 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100602 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100603 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100604
605 case Primitive::kPrimInt:
606 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100607 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100608 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100609
610 case Primitive::kPrimBoolean:
611 case Primitive::kPrimByte:
612 case Primitive::kPrimChar:
613 case Primitive::kPrimShort:
614 case Primitive::kPrimVoid:
615 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700616 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100617 }
618
619 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700620 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100621}
622
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100623Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
624 switch (type) {
625 case Primitive::kPrimBoolean:
626 case Primitive::kPrimByte:
627 case Primitive::kPrimChar:
628 case Primitive::kPrimShort:
629 case Primitive::kPrimInt:
630 case Primitive::kPrimNot:
631 return Location::RegisterLocation(EAX);
632
633 case Primitive::kPrimLong:
634 return Location::RegisterPairLocation(EAX, EDX);
635
636 case Primitive::kPrimVoid:
637 return Location::NoLocation();
638
639 case Primitive::kPrimDouble:
640 case Primitive::kPrimFloat:
641 return Location::FpuRegisterLocation(XMM0);
642 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100643
644 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100645}
646
647Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
648 return Location::RegisterLocation(kMethodRegisterArgument);
649}
650
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100651Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100652 switch (type) {
653 case Primitive::kPrimBoolean:
654 case Primitive::kPrimByte:
655 case Primitive::kPrimChar:
656 case Primitive::kPrimShort:
657 case Primitive::kPrimInt:
658 case Primitive::kPrimNot: {
659 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000660 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100661 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100662 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100663 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000664 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100665 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100666 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100667
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000668 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100669 uint32_t index = gp_index_;
670 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000671 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100672 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100673 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
674 calling_convention.GetRegisterPairAt(index));
675 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100676 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000677 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
678 }
679 }
680
681 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100682 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000683 stack_index_++;
684 if (index < calling_convention.GetNumberOfFpuRegisters()) {
685 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
686 } else {
687 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
688 }
689 }
690
691 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100692 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000693 stack_index_ += 2;
694 if (index < calling_convention.GetNumberOfFpuRegisters()) {
695 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
696 } else {
697 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100698 }
699 }
700
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100701 case Primitive::kPrimVoid:
702 LOG(FATAL) << "Unexpected parameter type " << type;
703 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100704 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100705 return Location();
706}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100707
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100708void CodeGeneratorX86::Move32(Location destination, Location source) {
709 if (source.Equals(destination)) {
710 return;
711 }
712 if (destination.IsRegister()) {
713 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000714 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100715 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000716 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100717 } else {
718 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000719 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100720 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 } else if (destination.IsFpuRegister()) {
722 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000723 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100724 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000725 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100726 } else {
727 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000728 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100729 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100730 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000731 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100732 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000733 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100734 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000735 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500736 } else if (source.IsConstant()) {
737 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000738 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500739 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740 } else {
741 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100742 __ pushl(Address(ESP, source.GetStackIndex()));
743 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100744 }
745 }
746}
747
748void CodeGeneratorX86::Move64(Location destination, Location source) {
749 if (source.Equals(destination)) {
750 return;
751 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100752 if (destination.IsRegisterPair()) {
753 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000754 EmitParallelMoves(
755 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
756 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100757 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000758 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100759 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
760 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100761 } else if (source.IsFpuRegister()) {
762 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100763 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000764 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100765 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100766 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
767 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100768 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
769 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100770 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500771 if (source.IsFpuRegister()) {
772 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
773 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000774 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100775 } else {
776 LOG(FATAL) << "Unimplemented";
777 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100778 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000779 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100780 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000781 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100782 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100783 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100784 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100785 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000786 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000787 } else if (source.IsConstant()) {
788 HConstant* constant = source.GetConstant();
789 int64_t value;
790 if (constant->IsLongConstant()) {
791 value = constant->AsLongConstant()->GetValue();
792 } else {
793 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000794 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000795 }
796 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
797 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100798 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000799 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000800 EmitParallelMoves(
801 Location::StackSlot(source.GetStackIndex()),
802 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100803 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000804 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100805 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
806 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100807 }
808 }
809}
810
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100811void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000812 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100813 if (instruction->IsCurrentMethod()) {
814 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
815 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000816 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100817 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000818 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000819 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
820 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000821 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000822 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000823 } else if (location.IsStackSlot()) {
824 __ movl(Address(ESP, location.GetStackIndex()), imm);
825 } else {
826 DCHECK(location.IsConstant());
827 DCHECK_EQ(location.GetConstant(), const_to_move);
828 }
829 } else if (const_to_move->IsLongConstant()) {
830 int64_t value = const_to_move->AsLongConstant()->GetValue();
831 if (location.IsRegisterPair()) {
832 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
833 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
834 } else if (location.IsDoubleStackSlot()) {
835 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000836 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
837 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000838 } else {
839 DCHECK(location.IsConstant());
840 DCHECK_EQ(location.GetConstant(), instruction);
841 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100842 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000843 } else if (instruction->IsTemporary()) {
844 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000845 if (temp_location.IsStackSlot()) {
846 Move32(location, temp_location);
847 } else {
848 DCHECK(temp_location.IsDoubleStackSlot());
849 Move64(location, temp_location);
850 }
Roland Levillain476df552014-10-09 17:51:36 +0100851 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100852 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100853 switch (instruction->GetType()) {
854 case Primitive::kPrimBoolean:
855 case Primitive::kPrimByte:
856 case Primitive::kPrimChar:
857 case Primitive::kPrimShort:
858 case Primitive::kPrimInt:
859 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100860 case Primitive::kPrimFloat:
861 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100862 break;
863
864 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100865 case Primitive::kPrimDouble:
866 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100867 break;
868
869 default:
870 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
871 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000872 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100873 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100874 switch (instruction->GetType()) {
875 case Primitive::kPrimBoolean:
876 case Primitive::kPrimByte:
877 case Primitive::kPrimChar:
878 case Primitive::kPrimShort:
879 case Primitive::kPrimInt:
880 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100881 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000882 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100883 break;
884
885 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100886 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000887 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100888 break;
889
890 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100891 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100892 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000893 }
894}
895
David Brazdilfc6a86a2015-06-26 10:33:45 +0000896void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100897 DCHECK(!successor->IsExitBlock());
898
899 HBasicBlock* block = got->GetBlock();
900 HInstruction* previous = got->GetPrevious();
901
902 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000903 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100904 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
905 return;
906 }
907
908 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
909 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
910 }
911 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000912 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000913 }
914}
915
David Brazdilfc6a86a2015-06-26 10:33:45 +0000916void LocationsBuilderX86::VisitGoto(HGoto* got) {
917 got->SetLocations(nullptr);
918}
919
920void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
921 HandleGoto(got, got->GetSuccessor());
922}
923
924void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
925 try_boundary->SetLocations(nullptr);
926}
927
928void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
929 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
930 if (!successor->IsExitBlock()) {
931 HandleGoto(try_boundary, successor);
932 }
933}
934
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000935void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000936 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000937}
938
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000939void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700940 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000941}
942
Mark Mendellc4701932015-04-10 13:18:51 -0400943void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
944 Label* true_label,
945 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100946 if (cond->IsFPConditionTrueIfNaN()) {
947 __ j(kUnordered, true_label);
948 } else if (cond->IsFPConditionFalseIfNaN()) {
949 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400950 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100951 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400952}
953
954void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
955 Label* true_label,
956 Label* false_label) {
957 LocationSummary* locations = cond->GetLocations();
958 Location left = locations->InAt(0);
959 Location right = locations->InAt(1);
960 IfCondition if_cond = cond->GetCondition();
961
Mark Mendellc4701932015-04-10 13:18:51 -0400962 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100963 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -0400964 IfCondition true_high_cond = if_cond;
965 IfCondition false_high_cond = cond->GetOppositeCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100966 Condition final_condition = X86UnsignedOrFPCondition(if_cond);
Mark Mendellc4701932015-04-10 13:18:51 -0400967
968 // Set the conditions for the test, remembering that == needs to be
969 // decided using the low words.
970 switch (if_cond) {
971 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -0400972 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +0100973 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -0400974 break;
975 case kCondLT:
976 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400977 break;
978 case kCondLE:
979 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400980 break;
981 case kCondGT:
982 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400983 break;
984 case kCondGE:
985 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400986 break;
987 }
988
989 if (right.IsConstant()) {
990 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -0400991 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +0100992 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -0400993
994 if (val_high == 0) {
995 __ testl(left_high, left_high);
996 } else {
997 __ cmpl(left_high, Immediate(val_high));
998 }
999 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001000 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001001 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001002 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001003 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001004 __ j(X86SignedCondition(true_high_cond), true_label);
1005 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001006 }
1007 // Must be equal high, so compare the lows.
1008 if (val_low == 0) {
1009 __ testl(left_low, left_low);
1010 } else {
1011 __ cmpl(left_low, Immediate(val_low));
1012 }
1013 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001014 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001015 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001016
1017 __ cmpl(left_high, right_high);
1018 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001019 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001020 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001021 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001022 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001023 __ j(X86SignedCondition(true_high_cond), true_label);
1024 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001025 }
1026 // Must be equal high, so compare the lows.
1027 __ cmpl(left_low, right_low);
1028 }
1029 // The last comparison might be unsigned.
1030 __ j(final_condition, true_label);
1031}
1032
1033void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1034 HCondition* condition,
1035 Label* true_target,
1036 Label* false_target,
1037 Label* always_true_target) {
1038 LocationSummary* locations = condition->GetLocations();
1039 Location left = locations->InAt(0);
1040 Location right = locations->InAt(1);
1041
1042 // We don't want true_target as a nullptr.
1043 if (true_target == nullptr) {
1044 true_target = always_true_target;
1045 }
1046 bool falls_through = (false_target == nullptr);
1047
1048 // FP compares don't like null false_targets.
1049 if (false_target == nullptr) {
1050 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1051 }
1052
1053 Primitive::Type type = condition->InputAt(0)->GetType();
1054 switch (type) {
1055 case Primitive::kPrimLong:
1056 GenerateLongComparesAndJumps(condition, true_target, false_target);
1057 break;
1058 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001059 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1060 GenerateFPJumps(condition, true_target, false_target);
1061 break;
1062 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001063 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1064 GenerateFPJumps(condition, true_target, false_target);
1065 break;
1066 default:
1067 LOG(FATAL) << "Unexpected compare type " << type;
1068 }
1069
1070 if (!falls_through) {
1071 __ jmp(false_target);
1072 }
1073}
1074
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001075void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1076 Label* true_target,
1077 Label* false_target,
1078 Label* always_true_target) {
1079 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001080 if (cond->IsIntConstant()) {
1081 // Constant condition, statically compared against 1.
1082 int32_t cond_value = cond->AsIntConstant()->GetValue();
1083 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001084 if (always_true_target != nullptr) {
1085 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001086 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001087 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001088 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001089 DCHECK_EQ(cond_value, 0);
1090 }
1091 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001092 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001093 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1094 // Moves do not affect the eflags register, so if the condition is
1095 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001096 // again. We can't use the eflags on long/FP conditions if they are
1097 // materialized due to the complex branching.
1098 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001099 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001100 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001101 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1102 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001103 if (!eflags_set) {
1104 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001105 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001106 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001107 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001108 } else {
1109 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1110 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001111 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001112 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001113 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001114 }
1115 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001116 // Condition has not been materialized, use its inputs as the
1117 // comparison and its condition as the branch condition.
1118
Mark Mendellc4701932015-04-10 13:18:51 -04001119 // Is this a long or FP comparison that has been folded into the HCondition?
1120 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1121 // Generate the comparison directly.
1122 GenerateCompareTestAndBranch(instruction->AsIf(),
1123 cond->AsCondition(),
1124 true_target,
1125 false_target,
1126 always_true_target);
1127 return;
1128 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001129
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001130 Location lhs = cond->GetLocations()->InAt(0);
1131 Location rhs = cond->GetLocations()->InAt(1);
1132 // LHS is guaranteed to be in a register (see
1133 // LocationsBuilderX86::VisitCondition).
1134 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001135 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001136 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001137 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001138 if (constant == 0) {
1139 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1140 } else {
1141 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1142 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001143 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001144 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001145 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001146 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001147 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001148 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001149 if (false_target != nullptr) {
1150 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001151 }
1152}
1153
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001154void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1155 LocationSummary* locations =
1156 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1157 HInstruction* cond = if_instr->InputAt(0);
1158 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1159 locations->SetInAt(0, Location::Any());
1160 }
1161}
1162
1163void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1164 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1165 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1166 Label* always_true_target = true_target;
1167 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1168 if_instr->IfTrueSuccessor())) {
1169 always_true_target = nullptr;
1170 }
1171 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1172 if_instr->IfFalseSuccessor())) {
1173 false_target = nullptr;
1174 }
1175 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1176}
1177
1178void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1179 LocationSummary* locations = new (GetGraph()->GetArena())
1180 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1181 HInstruction* cond = deoptimize->InputAt(0);
1182 DCHECK(cond->IsCondition());
1183 if (cond->AsCondition()->NeedsMaterialization()) {
1184 locations->SetInAt(0, Location::Any());
1185 }
1186}
1187
1188void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1189 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
1190 DeoptimizationSlowPathX86(deoptimize);
1191 codegen_->AddSlowPath(slow_path);
1192 Label* slow_path_entry = slow_path->GetEntryLabel();
1193 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1194}
1195
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001196void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001197 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001198}
1199
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001200void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1201 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001202}
1203
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001204void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001205 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001206}
1207
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001208void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001209 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001210 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001211}
1212
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001214 LocationSummary* locations =
1215 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001216 switch (store->InputAt(1)->GetType()) {
1217 case Primitive::kPrimBoolean:
1218 case Primitive::kPrimByte:
1219 case Primitive::kPrimChar:
1220 case Primitive::kPrimShort:
1221 case Primitive::kPrimInt:
1222 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001223 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001224 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1225 break;
1226
1227 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001228 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001229 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1230 break;
1231
1232 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001233 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001234 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001235}
1236
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001237void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001238 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001239}
1240
Roland Levillain0d37cd02015-05-27 16:39:19 +01001241void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001242 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001243 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001244 // Handle the long/FP comparisons made in instruction simplification.
1245 switch (cond->InputAt(0)->GetType()) {
1246 case Primitive::kPrimLong: {
1247 locations->SetInAt(0, Location::RequiresRegister());
1248 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1249 if (cond->NeedsMaterialization()) {
1250 locations->SetOut(Location::RequiresRegister());
1251 }
1252 break;
1253 }
1254 case Primitive::kPrimFloat:
1255 case Primitive::kPrimDouble: {
1256 locations->SetInAt(0, Location::RequiresFpuRegister());
1257 locations->SetInAt(1, Location::RequiresFpuRegister());
1258 if (cond->NeedsMaterialization()) {
1259 locations->SetOut(Location::RequiresRegister());
1260 }
1261 break;
1262 }
1263 default:
1264 locations->SetInAt(0, Location::RequiresRegister());
1265 locations->SetInAt(1, Location::Any());
1266 if (cond->NeedsMaterialization()) {
1267 // We need a byte register.
1268 locations->SetOut(Location::RegisterLocation(ECX));
1269 }
1270 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001271 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001272}
1273
Roland Levillain0d37cd02015-05-27 16:39:19 +01001274void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001275 if (!cond->NeedsMaterialization()) {
1276 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001277 }
Mark Mendellc4701932015-04-10 13:18:51 -04001278
1279 LocationSummary* locations = cond->GetLocations();
1280 Location lhs = locations->InAt(0);
1281 Location rhs = locations->InAt(1);
1282 Register reg = locations->Out().AsRegister<Register>();
1283 Label true_label, false_label;
1284
1285 switch (cond->InputAt(0)->GetType()) {
1286 default: {
1287 // Integer case.
1288
1289 // Clear output register: setcc only sets the low byte.
1290 __ xorl(reg, reg);
1291
1292 if (rhs.IsRegister()) {
1293 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1294 } else if (rhs.IsConstant()) {
1295 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1296 if (constant == 0) {
1297 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1298 } else {
1299 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1300 }
1301 } else {
1302 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1303 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001304 __ setb(X86SignedCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001305 return;
1306 }
1307 case Primitive::kPrimLong:
1308 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1309 break;
1310 case Primitive::kPrimFloat:
1311 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1312 GenerateFPJumps(cond, &true_label, &false_label);
1313 break;
1314 case Primitive::kPrimDouble:
1315 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1316 GenerateFPJumps(cond, &true_label, &false_label);
1317 break;
1318 }
1319
1320 // Convert the jumps into the result.
1321 Label done_label;
1322
Roland Levillain4fa13f62015-07-06 18:11:54 +01001323 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001324 __ Bind(&false_label);
1325 __ xorl(reg, reg);
1326 __ jmp(&done_label);
1327
Roland Levillain4fa13f62015-07-06 18:11:54 +01001328 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001329 __ Bind(&true_label);
1330 __ movl(reg, Immediate(1));
1331 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001332}
1333
1334void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1335 VisitCondition(comp);
1336}
1337
1338void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1339 VisitCondition(comp);
1340}
1341
1342void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1343 VisitCondition(comp);
1344}
1345
1346void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1347 VisitCondition(comp);
1348}
1349
1350void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1351 VisitCondition(comp);
1352}
1353
1354void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1355 VisitCondition(comp);
1356}
1357
1358void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1359 VisitCondition(comp);
1360}
1361
1362void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1363 VisitCondition(comp);
1364}
1365
1366void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1367 VisitCondition(comp);
1368}
1369
1370void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1371 VisitCondition(comp);
1372}
1373
1374void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1375 VisitCondition(comp);
1376}
1377
1378void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1379 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001380}
1381
1382void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001383 LocationSummary* locations =
1384 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001385 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001386}
1387
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001388void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001389 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001390 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001391}
1392
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001393void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1394 LocationSummary* locations =
1395 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1396 locations->SetOut(Location::ConstantLocation(constant));
1397}
1398
1399void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1400 // Will be generated at use site.
1401 UNUSED(constant);
1402}
1403
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001404void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001405 LocationSummary* locations =
1406 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001407 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001408}
1409
1410void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1411 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001412 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001413}
1414
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001415void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1416 LocationSummary* locations =
1417 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1418 locations->SetOut(Location::ConstantLocation(constant));
1419}
1420
1421void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1422 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001423 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001424}
1425
1426void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1427 LocationSummary* locations =
1428 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1429 locations->SetOut(Location::ConstantLocation(constant));
1430}
1431
1432void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1433 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001434 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001435}
1436
Calin Juravle27df7582015-04-17 19:12:31 +01001437void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1438 memory_barrier->SetLocations(nullptr);
1439}
1440
1441void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1442 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1443}
1444
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001445void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001446 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001447}
1448
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001449void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001450 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001451 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001452}
1453
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001454void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001455 LocationSummary* locations =
1456 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001457 switch (ret->InputAt(0)->GetType()) {
1458 case Primitive::kPrimBoolean:
1459 case Primitive::kPrimByte:
1460 case Primitive::kPrimChar:
1461 case Primitive::kPrimShort:
1462 case Primitive::kPrimInt:
1463 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001464 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001465 break;
1466
1467 case Primitive::kPrimLong:
1468 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001469 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001470 break;
1471
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001472 case Primitive::kPrimFloat:
1473 case Primitive::kPrimDouble:
1474 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001475 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001476 break;
1477
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001478 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001479 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001480 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001481}
1482
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001483void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001484 if (kIsDebugBuild) {
1485 switch (ret->InputAt(0)->GetType()) {
1486 case Primitive::kPrimBoolean:
1487 case Primitive::kPrimByte:
1488 case Primitive::kPrimChar:
1489 case Primitive::kPrimShort:
1490 case Primitive::kPrimInt:
1491 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001492 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001493 break;
1494
1495 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001496 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1497 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001498 break;
1499
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001500 case Primitive::kPrimFloat:
1501 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001502 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001503 break;
1504
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001505 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001506 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001507 }
1508 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001509 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001510}
1511
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001512void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001513 // When we do not run baseline, explicit clinit checks triggered by static
1514 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1515 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001516
Mark Mendellfb8d2792015-03-31 22:16:59 -04001517 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001518 if (intrinsic.TryDispatch(invoke)) {
1519 return;
1520 }
1521
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001522 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001523
1524 if (codegen_->IsBaseline()) {
1525 // Baseline does not have enough registers if the current method also
1526 // needs a register. We therefore do not require a register for it, and let
1527 // the code generation of the invoke handle it.
1528 LocationSummary* locations = invoke->GetLocations();
1529 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1530 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1531 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1532 }
1533 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001534}
1535
Mark Mendell09ed1a32015-03-25 08:30:06 -04001536static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1537 if (invoke->GetLocations()->Intrinsified()) {
1538 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1539 intrinsic.Dispatch(invoke);
1540 return true;
1541 }
1542 return false;
1543}
1544
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001545void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001546 // When we do not run baseline, explicit clinit checks triggered by static
1547 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1548 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001549
Mark Mendell09ed1a32015-03-25 08:30:06 -04001550 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1551 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001552 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001553
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001554 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001555 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001556 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001557 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001558}
1559
1560void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1561 HandleInvoke(invoke);
1562}
1563
1564void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001565 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001566 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001567}
1568
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001569void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001570 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001571 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1572 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001573 LocationSummary* locations = invoke->GetLocations();
1574 Location receiver = locations->InAt(0);
1575 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001576 // temp = object->GetClass();
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001577 DCHECK(receiver.IsRegister());
1578 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001579 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001580 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001581 // temp = temp->GetMethodAt(method_offset);
1582 __ movl(temp, Address(temp, method_offset));
1583 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001584 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001585 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001586
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001587 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001588 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001589}
1590
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001591void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1592 HandleInvoke(invoke);
1593 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001594 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001595}
1596
1597void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1598 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001599 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001600 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1601 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001602 LocationSummary* locations = invoke->GetLocations();
1603 Location receiver = locations->InAt(0);
1604 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1605
1606 // Set the hidden argument.
1607 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001608 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001609
1610 // temp = object->GetClass();
1611 if (receiver.IsStackSlot()) {
1612 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1613 __ movl(temp, Address(temp, class_offset));
1614 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001615 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001616 }
Roland Levillain4d027112015-07-01 15:41:14 +01001617 codegen_->MaybeRecordImplicitNullCheck(invoke);
1618 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001619 // temp = temp->GetImtEntryAt(method_offset);
1620 __ movl(temp, Address(temp, method_offset));
1621 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001622 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001623 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001624
1625 DCHECK(!codegen_->IsLeafMethod());
1626 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1627}
1628
Roland Levillain88cb1752014-10-20 16:36:47 +01001629void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1630 LocationSummary* locations =
1631 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1632 switch (neg->GetResultType()) {
1633 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001634 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001635 locations->SetInAt(0, Location::RequiresRegister());
1636 locations->SetOut(Location::SameAsFirstInput());
1637 break;
1638
Roland Levillain88cb1752014-10-20 16:36:47 +01001639 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001640 locations->SetInAt(0, Location::RequiresFpuRegister());
1641 locations->SetOut(Location::SameAsFirstInput());
1642 locations->AddTemp(Location::RequiresRegister());
1643 locations->AddTemp(Location::RequiresFpuRegister());
1644 break;
1645
Roland Levillain88cb1752014-10-20 16:36:47 +01001646 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001647 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001648 locations->SetOut(Location::SameAsFirstInput());
1649 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001650 break;
1651
1652 default:
1653 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1654 }
1655}
1656
1657void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1658 LocationSummary* locations = neg->GetLocations();
1659 Location out = locations->Out();
1660 Location in = locations->InAt(0);
1661 switch (neg->GetResultType()) {
1662 case Primitive::kPrimInt:
1663 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001664 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001665 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001666 break;
1667
1668 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001669 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001670 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001671 __ negl(out.AsRegisterPairLow<Register>());
1672 // Negation is similar to subtraction from zero. The least
1673 // significant byte triggers a borrow when it is different from
1674 // zero; to take it into account, add 1 to the most significant
1675 // byte if the carry flag (CF) is set to 1 after the first NEGL
1676 // operation.
1677 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1678 __ negl(out.AsRegisterPairHigh<Register>());
1679 break;
1680
Roland Levillain5368c212014-11-27 15:03:41 +00001681 case Primitive::kPrimFloat: {
1682 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001683 Register constant = locations->GetTemp(0).AsRegister<Register>();
1684 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001685 // Implement float negation with an exclusive or with value
1686 // 0x80000000 (mask for bit 31, representing the sign of a
1687 // single-precision floating-point number).
1688 __ movl(constant, Immediate(INT32_C(0x80000000)));
1689 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001690 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001691 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001692 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001693
Roland Levillain5368c212014-11-27 15:03:41 +00001694 case Primitive::kPrimDouble: {
1695 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001696 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001697 // Implement double negation with an exclusive or with value
1698 // 0x8000000000000000 (mask for bit 63, representing the sign of
1699 // a double-precision floating-point number).
1700 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001701 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001702 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001703 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001704
1705 default:
1706 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1707 }
1708}
1709
Roland Levillaindff1f282014-11-05 14:15:05 +00001710void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001711 Primitive::Type result_type = conversion->GetResultType();
1712 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001713 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001714
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001715 // The float-to-long and double-to-long type conversions rely on a
1716 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001717 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001718 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1719 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001720 ? LocationSummary::kCall
1721 : LocationSummary::kNoCall;
1722 LocationSummary* locations =
1723 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1724
David Brazdilb2bd1c52015-03-25 11:17:37 +00001725 // The Java language does not allow treating boolean as an integral type but
1726 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001727
Roland Levillaindff1f282014-11-05 14:15:05 +00001728 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001729 case Primitive::kPrimByte:
1730 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001731 case Primitive::kPrimBoolean:
1732 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001733 case Primitive::kPrimShort:
1734 case Primitive::kPrimInt:
1735 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001736 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001737 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1738 // Make the output overlap to please the register allocator. This greatly simplifies
1739 // the validation of the linear scan implementation
1740 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001741 break;
1742
1743 default:
1744 LOG(FATAL) << "Unexpected type conversion from " << input_type
1745 << " to " << result_type;
1746 }
1747 break;
1748
Roland Levillain01a8d712014-11-14 16:27:39 +00001749 case Primitive::kPrimShort:
1750 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001751 case Primitive::kPrimBoolean:
1752 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001753 case Primitive::kPrimByte:
1754 case Primitive::kPrimInt:
1755 case Primitive::kPrimChar:
1756 // Processing a Dex `int-to-short' instruction.
1757 locations->SetInAt(0, Location::Any());
1758 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1759 break;
1760
1761 default:
1762 LOG(FATAL) << "Unexpected type conversion from " << input_type
1763 << " to " << result_type;
1764 }
1765 break;
1766
Roland Levillain946e1432014-11-11 17:35:19 +00001767 case Primitive::kPrimInt:
1768 switch (input_type) {
1769 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001770 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001771 locations->SetInAt(0, Location::Any());
1772 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1773 break;
1774
1775 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001776 // Processing a Dex `float-to-int' instruction.
1777 locations->SetInAt(0, Location::RequiresFpuRegister());
1778 locations->SetOut(Location::RequiresRegister());
1779 locations->AddTemp(Location::RequiresFpuRegister());
1780 break;
1781
Roland Levillain946e1432014-11-11 17:35:19 +00001782 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001783 // Processing a Dex `double-to-int' instruction.
1784 locations->SetInAt(0, Location::RequiresFpuRegister());
1785 locations->SetOut(Location::RequiresRegister());
1786 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001787 break;
1788
1789 default:
1790 LOG(FATAL) << "Unexpected type conversion from " << input_type
1791 << " to " << result_type;
1792 }
1793 break;
1794
Roland Levillaindff1f282014-11-05 14:15:05 +00001795 case Primitive::kPrimLong:
1796 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001797 case Primitive::kPrimBoolean:
1798 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001799 case Primitive::kPrimByte:
1800 case Primitive::kPrimShort:
1801 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001802 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001803 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001804 locations->SetInAt(0, Location::RegisterLocation(EAX));
1805 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1806 break;
1807
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001808 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001809 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001810 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001811 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001812 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1813 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1814
Vladimir Marko949c91f2015-01-27 10:48:44 +00001815 // The runtime helper puts the result in EAX, EDX.
1816 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001817 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001818 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001819
1820 default:
1821 LOG(FATAL) << "Unexpected type conversion from " << input_type
1822 << " to " << result_type;
1823 }
1824 break;
1825
Roland Levillain981e4542014-11-14 11:47:14 +00001826 case Primitive::kPrimChar:
1827 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001828 case Primitive::kPrimBoolean:
1829 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001830 case Primitive::kPrimByte:
1831 case Primitive::kPrimShort:
1832 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001833 // Processing a Dex `int-to-char' instruction.
1834 locations->SetInAt(0, Location::Any());
1835 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1836 break;
1837
1838 default:
1839 LOG(FATAL) << "Unexpected type conversion from " << input_type
1840 << " to " << result_type;
1841 }
1842 break;
1843
Roland Levillaindff1f282014-11-05 14:15:05 +00001844 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001845 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001846 case Primitive::kPrimBoolean:
1847 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001848 case Primitive::kPrimByte:
1849 case Primitive::kPrimShort:
1850 case Primitive::kPrimInt:
1851 case Primitive::kPrimChar:
1852 // Processing a Dex `int-to-float' instruction.
1853 locations->SetInAt(0, Location::RequiresRegister());
1854 locations->SetOut(Location::RequiresFpuRegister());
1855 break;
1856
1857 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001858 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001859 locations->SetInAt(0, Location::Any());
1860 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001861 break;
1862
Roland Levillaincff13742014-11-17 14:32:17 +00001863 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001864 // Processing a Dex `double-to-float' instruction.
1865 locations->SetInAt(0, Location::RequiresFpuRegister());
1866 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001867 break;
1868
1869 default:
1870 LOG(FATAL) << "Unexpected type conversion from " << input_type
1871 << " to " << result_type;
1872 };
1873 break;
1874
Roland Levillaindff1f282014-11-05 14:15:05 +00001875 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001876 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001877 case Primitive::kPrimBoolean:
1878 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001879 case Primitive::kPrimByte:
1880 case Primitive::kPrimShort:
1881 case Primitive::kPrimInt:
1882 case Primitive::kPrimChar:
1883 // Processing a Dex `int-to-double' instruction.
1884 locations->SetInAt(0, Location::RequiresRegister());
1885 locations->SetOut(Location::RequiresFpuRegister());
1886 break;
1887
1888 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001889 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001890 locations->SetInAt(0, Location::Any());
1891 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001892 break;
1893
Roland Levillaincff13742014-11-17 14:32:17 +00001894 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001895 // Processing a Dex `float-to-double' instruction.
1896 locations->SetInAt(0, Location::RequiresFpuRegister());
1897 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001898 break;
1899
1900 default:
1901 LOG(FATAL) << "Unexpected type conversion from " << input_type
1902 << " to " << result_type;
1903 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001904 break;
1905
1906 default:
1907 LOG(FATAL) << "Unexpected type conversion from " << input_type
1908 << " to " << result_type;
1909 }
1910}
1911
1912void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1913 LocationSummary* locations = conversion->GetLocations();
1914 Location out = locations->Out();
1915 Location in = locations->InAt(0);
1916 Primitive::Type result_type = conversion->GetResultType();
1917 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001918 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001919 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001920 case Primitive::kPrimByte:
1921 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001922 case Primitive::kPrimBoolean:
1923 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001924 case Primitive::kPrimShort:
1925 case Primitive::kPrimInt:
1926 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001927 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001928 if (in.IsRegister()) {
1929 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001930 } else {
1931 DCHECK(in.GetConstant()->IsIntConstant());
1932 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1933 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1934 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001935 break;
1936
1937 default:
1938 LOG(FATAL) << "Unexpected type conversion from " << input_type
1939 << " to " << result_type;
1940 }
1941 break;
1942
Roland Levillain01a8d712014-11-14 16:27:39 +00001943 case Primitive::kPrimShort:
1944 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001945 case Primitive::kPrimBoolean:
1946 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001947 case Primitive::kPrimByte:
1948 case Primitive::kPrimInt:
1949 case Primitive::kPrimChar:
1950 // Processing a Dex `int-to-short' instruction.
1951 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001952 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001953 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001954 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001955 } else {
1956 DCHECK(in.GetConstant()->IsIntConstant());
1957 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001958 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001959 }
1960 break;
1961
1962 default:
1963 LOG(FATAL) << "Unexpected type conversion from " << input_type
1964 << " to " << result_type;
1965 }
1966 break;
1967
Roland Levillain946e1432014-11-11 17:35:19 +00001968 case Primitive::kPrimInt:
1969 switch (input_type) {
1970 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001971 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001972 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001973 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001974 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001975 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001976 } else {
1977 DCHECK(in.IsConstant());
1978 DCHECK(in.GetConstant()->IsLongConstant());
1979 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001980 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001981 }
1982 break;
1983
Roland Levillain3f8f9362014-12-02 17:45:01 +00001984 case Primitive::kPrimFloat: {
1985 // Processing a Dex `float-to-int' instruction.
1986 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1987 Register output = out.AsRegister<Register>();
1988 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1989 Label done, nan;
1990
1991 __ movl(output, Immediate(kPrimIntMax));
1992 // temp = int-to-float(output)
1993 __ cvtsi2ss(temp, output);
1994 // if input >= temp goto done
1995 __ comiss(input, temp);
1996 __ j(kAboveEqual, &done);
1997 // if input == NaN goto nan
1998 __ j(kUnordered, &nan);
1999 // output = float-to-int-truncate(input)
2000 __ cvttss2si(output, input);
2001 __ jmp(&done);
2002 __ Bind(&nan);
2003 // output = 0
2004 __ xorl(output, output);
2005 __ Bind(&done);
2006 break;
2007 }
2008
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002009 case Primitive::kPrimDouble: {
2010 // Processing a Dex `double-to-int' instruction.
2011 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2012 Register output = out.AsRegister<Register>();
2013 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2014 Label done, nan;
2015
2016 __ movl(output, Immediate(kPrimIntMax));
2017 // temp = int-to-double(output)
2018 __ cvtsi2sd(temp, output);
2019 // if input >= temp goto done
2020 __ comisd(input, temp);
2021 __ j(kAboveEqual, &done);
2022 // if input == NaN goto nan
2023 __ j(kUnordered, &nan);
2024 // output = double-to-int-truncate(input)
2025 __ cvttsd2si(output, input);
2026 __ jmp(&done);
2027 __ Bind(&nan);
2028 // output = 0
2029 __ xorl(output, output);
2030 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002031 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002032 }
Roland Levillain946e1432014-11-11 17:35:19 +00002033
2034 default:
2035 LOG(FATAL) << "Unexpected type conversion from " << input_type
2036 << " to " << result_type;
2037 }
2038 break;
2039
Roland Levillaindff1f282014-11-05 14:15:05 +00002040 case Primitive::kPrimLong:
2041 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002042 case Primitive::kPrimBoolean:
2043 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002044 case Primitive::kPrimByte:
2045 case Primitive::kPrimShort:
2046 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002047 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002048 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002049 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2050 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002051 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002052 __ cdq();
2053 break;
2054
2055 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002056 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002057 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2058 conversion,
2059 conversion->GetDexPc(),
2060 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002061 break;
2062
Roland Levillaindff1f282014-11-05 14:15:05 +00002063 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002064 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002065 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2066 conversion,
2067 conversion->GetDexPc(),
2068 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002069 break;
2070
2071 default:
2072 LOG(FATAL) << "Unexpected type conversion from " << input_type
2073 << " to " << result_type;
2074 }
2075 break;
2076
Roland Levillain981e4542014-11-14 11:47:14 +00002077 case Primitive::kPrimChar:
2078 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002079 case Primitive::kPrimBoolean:
2080 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002081 case Primitive::kPrimByte:
2082 case Primitive::kPrimShort:
2083 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002084 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2085 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002086 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002087 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002088 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002089 } else {
2090 DCHECK(in.GetConstant()->IsIntConstant());
2091 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002092 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002093 }
2094 break;
2095
2096 default:
2097 LOG(FATAL) << "Unexpected type conversion from " << input_type
2098 << " to " << result_type;
2099 }
2100 break;
2101
Roland Levillaindff1f282014-11-05 14:15:05 +00002102 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002103 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002104 case Primitive::kPrimBoolean:
2105 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002106 case Primitive::kPrimByte:
2107 case Primitive::kPrimShort:
2108 case Primitive::kPrimInt:
2109 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002110 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002111 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002112 break;
2113
Roland Levillain6d0e4832014-11-27 18:31:21 +00002114 case Primitive::kPrimLong: {
2115 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002116 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002117
Roland Levillain232ade02015-04-20 15:14:36 +01002118 // Create stack space for the call to
2119 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2120 // TODO: enhance register allocator to ask for stack temporaries.
2121 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2122 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2123 __ subl(ESP, Immediate(adjustment));
2124 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002125
Roland Levillain232ade02015-04-20 15:14:36 +01002126 // Load the value to the FP stack, using temporaries if needed.
2127 PushOntoFPStack(in, 0, adjustment, false, true);
2128
2129 if (out.IsStackSlot()) {
2130 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2131 } else {
2132 __ fstps(Address(ESP, 0));
2133 Location stack_temp = Location::StackSlot(0);
2134 codegen_->Move32(out, stack_temp);
2135 }
2136
2137 // Remove the temporary stack space we allocated.
2138 if (adjustment != 0) {
2139 __ addl(ESP, Immediate(adjustment));
2140 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002141 break;
2142 }
2143
Roland Levillaincff13742014-11-17 14:32:17 +00002144 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002145 // Processing a Dex `double-to-float' instruction.
2146 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002147 break;
2148
2149 default:
2150 LOG(FATAL) << "Unexpected type conversion from " << input_type
2151 << " to " << result_type;
2152 };
2153 break;
2154
Roland Levillaindff1f282014-11-05 14:15:05 +00002155 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002156 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002157 case Primitive::kPrimBoolean:
2158 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002159 case Primitive::kPrimByte:
2160 case Primitive::kPrimShort:
2161 case Primitive::kPrimInt:
2162 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002163 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002164 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002165 break;
2166
Roland Levillain647b9ed2014-11-27 12:06:00 +00002167 case Primitive::kPrimLong: {
2168 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002169 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002170
Roland Levillain232ade02015-04-20 15:14:36 +01002171 // Create stack space for the call to
2172 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2173 // TODO: enhance register allocator to ask for stack temporaries.
2174 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2175 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2176 __ subl(ESP, Immediate(adjustment));
2177 }
2178
2179 // Load the value to the FP stack, using temporaries if needed.
2180 PushOntoFPStack(in, 0, adjustment, false, true);
2181
2182 if (out.IsDoubleStackSlot()) {
2183 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2184 } else {
2185 __ fstpl(Address(ESP, 0));
2186 Location stack_temp = Location::DoubleStackSlot(0);
2187 codegen_->Move64(out, stack_temp);
2188 }
2189
2190 // Remove the temporary stack space we allocated.
2191 if (adjustment != 0) {
2192 __ addl(ESP, Immediate(adjustment));
2193 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002194 break;
2195 }
2196
Roland Levillaincff13742014-11-17 14:32:17 +00002197 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002198 // Processing a Dex `float-to-double' instruction.
2199 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002200 break;
2201
2202 default:
2203 LOG(FATAL) << "Unexpected type conversion from " << input_type
2204 << " to " << result_type;
2205 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002206 break;
2207
2208 default:
2209 LOG(FATAL) << "Unexpected type conversion from " << input_type
2210 << " to " << result_type;
2211 }
2212}
2213
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002214void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002215 LocationSummary* locations =
2216 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002217 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002218 case Primitive::kPrimInt: {
2219 locations->SetInAt(0, Location::RequiresRegister());
2220 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2221 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2222 break;
2223 }
2224
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002225 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002226 locations->SetInAt(0, Location::RequiresRegister());
2227 locations->SetInAt(1, Location::Any());
2228 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002229 break;
2230 }
2231
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002232 case Primitive::kPrimFloat:
2233 case Primitive::kPrimDouble: {
2234 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00002235 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002236 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002237 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002238 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002239
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002240 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002241 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2242 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002243 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002244}
2245
2246void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2247 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002248 Location first = locations->InAt(0);
2249 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002250 Location out = locations->Out();
2251
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002252 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002253 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002254 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002255 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2256 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002257 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2258 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002259 } else {
2260 __ leal(out.AsRegister<Register>(), Address(
2261 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2262 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002263 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002264 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2265 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2266 __ addl(out.AsRegister<Register>(), Immediate(value));
2267 } else {
2268 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2269 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002270 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002271 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002272 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002273 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002274 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002275 }
2276
2277 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002278 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002279 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2280 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002281 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002282 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2283 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002284 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002285 } else {
2286 DCHECK(second.IsConstant()) << second;
2287 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2288 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2289 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002290 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002291 break;
2292 }
2293
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002294 case Primitive::kPrimFloat: {
2295 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002296 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002297 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002298 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002299 }
2300
2301 case Primitive::kPrimDouble: {
2302 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002303 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002304 }
2305 break;
2306 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002307
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002308 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002309 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002310 }
2311}
2312
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002313void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002314 LocationSummary* locations =
2315 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002316 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002317 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002318 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002319 locations->SetInAt(0, Location::RequiresRegister());
2320 locations->SetInAt(1, Location::Any());
2321 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002322 break;
2323 }
Calin Juravle11351682014-10-23 15:38:15 +01002324 case Primitive::kPrimFloat:
2325 case Primitive::kPrimDouble: {
2326 locations->SetInAt(0, Location::RequiresFpuRegister());
2327 locations->SetInAt(1, Location::RequiresFpuRegister());
2328 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002329 break;
Calin Juravle11351682014-10-23 15:38:15 +01002330 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002331
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002332 default:
Calin Juravle11351682014-10-23 15:38:15 +01002333 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002334 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002335}
2336
2337void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2338 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002339 Location first = locations->InAt(0);
2340 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002341 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002342 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002343 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002344 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002345 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002346 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002347 __ subl(first.AsRegister<Register>(),
2348 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002349 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002350 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002351 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002352 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002353 }
2354
2355 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002356 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002357 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2358 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002359 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002360 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002361 __ sbbl(first.AsRegisterPairHigh<Register>(),
2362 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002363 } else {
2364 DCHECK(second.IsConstant()) << second;
2365 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2366 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2367 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002368 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002369 break;
2370 }
2371
Calin Juravle11351682014-10-23 15:38:15 +01002372 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002373 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002374 break;
Calin Juravle11351682014-10-23 15:38:15 +01002375 }
2376
2377 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002378 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002379 break;
2380 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002381
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002382 default:
Calin Juravle11351682014-10-23 15:38:15 +01002383 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002384 }
2385}
2386
Calin Juravle34bacdf2014-10-07 20:23:36 +01002387void LocationsBuilderX86::VisitMul(HMul* mul) {
2388 LocationSummary* locations =
2389 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2390 switch (mul->GetResultType()) {
2391 case Primitive::kPrimInt:
2392 locations->SetInAt(0, Location::RequiresRegister());
2393 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002394 if (mul->InputAt(1)->IsIntConstant()) {
2395 // Can use 3 operand multiply.
2396 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2397 } else {
2398 locations->SetOut(Location::SameAsFirstInput());
2399 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002400 break;
2401 case Primitive::kPrimLong: {
2402 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002403 locations->SetInAt(1, Location::Any());
2404 locations->SetOut(Location::SameAsFirstInput());
2405 // Needed for imul on 32bits with 64bits output.
2406 locations->AddTemp(Location::RegisterLocation(EAX));
2407 locations->AddTemp(Location::RegisterLocation(EDX));
2408 break;
2409 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002410 case Primitive::kPrimFloat:
2411 case Primitive::kPrimDouble: {
2412 locations->SetInAt(0, Location::RequiresFpuRegister());
2413 locations->SetInAt(1, Location::RequiresFpuRegister());
2414 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002415 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002416 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002417
2418 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002419 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002420 }
2421}
2422
2423void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2424 LocationSummary* locations = mul->GetLocations();
2425 Location first = locations->InAt(0);
2426 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002427 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002428
2429 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002430 case Primitive::kPrimInt:
2431 // The constant may have ended up in a register, so test explicitly to avoid
2432 // problems where the output may not be the same as the first operand.
2433 if (mul->InputAt(1)->IsIntConstant()) {
2434 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2435 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2436 } else if (second.IsRegister()) {
2437 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002438 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002439 } else {
2440 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002441 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002442 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002443 }
2444 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002445
2446 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002447 Register in1_hi = first.AsRegisterPairHigh<Register>();
2448 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002449 Register eax = locations->GetTemp(0).AsRegister<Register>();
2450 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002451
2452 DCHECK_EQ(EAX, eax);
2453 DCHECK_EQ(EDX, edx);
2454
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002455 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002456 // output: in1
2457 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2458 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2459 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002460 if (second.IsConstant()) {
2461 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002462
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002463 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2464 int32_t low_value = Low32Bits(value);
2465 int32_t high_value = High32Bits(value);
2466 Immediate low(low_value);
2467 Immediate high(high_value);
2468
2469 __ movl(eax, high);
2470 // eax <- in1.lo * in2.hi
2471 __ imull(eax, in1_lo);
2472 // in1.hi <- in1.hi * in2.lo
2473 __ imull(in1_hi, low);
2474 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2475 __ addl(in1_hi, eax);
2476 // move in2_lo to eax to prepare for double precision
2477 __ movl(eax, low);
2478 // edx:eax <- in1.lo * in2.lo
2479 __ mull(in1_lo);
2480 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2481 __ addl(in1_hi, edx);
2482 // in1.lo <- (in1.lo * in2.lo)[31:0];
2483 __ movl(in1_lo, eax);
2484 } else if (second.IsRegisterPair()) {
2485 Register in2_hi = second.AsRegisterPairHigh<Register>();
2486 Register in2_lo = second.AsRegisterPairLow<Register>();
2487
2488 __ movl(eax, in2_hi);
2489 // eax <- in1.lo * in2.hi
2490 __ imull(eax, in1_lo);
2491 // in1.hi <- in1.hi * in2.lo
2492 __ imull(in1_hi, in2_lo);
2493 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2494 __ addl(in1_hi, eax);
2495 // move in1_lo to eax to prepare for double precision
2496 __ movl(eax, in1_lo);
2497 // edx:eax <- in1.lo * in2.lo
2498 __ mull(in2_lo);
2499 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2500 __ addl(in1_hi, edx);
2501 // in1.lo <- (in1.lo * in2.lo)[31:0];
2502 __ movl(in1_lo, eax);
2503 } else {
2504 DCHECK(second.IsDoubleStackSlot()) << second;
2505 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2506 Address in2_lo(ESP, second.GetStackIndex());
2507
2508 __ movl(eax, in2_hi);
2509 // eax <- in1.lo * in2.hi
2510 __ imull(eax, in1_lo);
2511 // in1.hi <- in1.hi * in2.lo
2512 __ imull(in1_hi, in2_lo);
2513 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2514 __ addl(in1_hi, eax);
2515 // move in1_lo to eax to prepare for double precision
2516 __ movl(eax, in1_lo);
2517 // edx:eax <- in1.lo * in2.lo
2518 __ mull(in2_lo);
2519 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2520 __ addl(in1_hi, edx);
2521 // in1.lo <- (in1.lo * in2.lo)[31:0];
2522 __ movl(in1_lo, eax);
2523 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002524
2525 break;
2526 }
2527
Calin Juravleb5bfa962014-10-21 18:02:24 +01002528 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002529 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002530 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002531 }
2532
2533 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002534 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002535 break;
2536 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002537
2538 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002539 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002540 }
2541}
2542
Roland Levillain232ade02015-04-20 15:14:36 +01002543void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2544 uint32_t temp_offset,
2545 uint32_t stack_adjustment,
2546 bool is_fp,
2547 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002548 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002549 DCHECK(!is_wide);
2550 if (is_fp) {
2551 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2552 } else {
2553 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2554 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002555 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002556 DCHECK(is_wide);
2557 if (is_fp) {
2558 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2559 } else {
2560 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2561 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002562 } else {
2563 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002564 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002565 Location stack_temp = Location::StackSlot(temp_offset);
2566 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002567 if (is_fp) {
2568 __ flds(Address(ESP, temp_offset));
2569 } else {
2570 __ filds(Address(ESP, temp_offset));
2571 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002572 } else {
2573 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2574 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002575 if (is_fp) {
2576 __ fldl(Address(ESP, temp_offset));
2577 } else {
2578 __ fildl(Address(ESP, temp_offset));
2579 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002580 }
2581 }
2582}
2583
2584void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2585 Primitive::Type type = rem->GetResultType();
2586 bool is_float = type == Primitive::kPrimFloat;
2587 size_t elem_size = Primitive::ComponentSize(type);
2588 LocationSummary* locations = rem->GetLocations();
2589 Location first = locations->InAt(0);
2590 Location second = locations->InAt(1);
2591 Location out = locations->Out();
2592
2593 // Create stack space for 2 elements.
2594 // TODO: enhance register allocator to ask for stack temporaries.
2595 __ subl(ESP, Immediate(2 * elem_size));
2596
2597 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002598 const bool is_wide = !is_float;
2599 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2600 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002601
2602 // Loop doing FPREM until we stabilize.
2603 Label retry;
2604 __ Bind(&retry);
2605 __ fprem();
2606
2607 // Move FP status to AX.
2608 __ fstsw();
2609
2610 // And see if the argument reduction is complete. This is signaled by the
2611 // C2 FPU flag bit set to 0.
2612 __ andl(EAX, Immediate(kC2ConditionMask));
2613 __ j(kNotEqual, &retry);
2614
2615 // We have settled on the final value. Retrieve it into an XMM register.
2616 // Store FP top of stack to real stack.
2617 if (is_float) {
2618 __ fsts(Address(ESP, 0));
2619 } else {
2620 __ fstl(Address(ESP, 0));
2621 }
2622
2623 // Pop the 2 items from the FP stack.
2624 __ fucompp();
2625
2626 // Load the value from the stack into an XMM register.
2627 DCHECK(out.IsFpuRegister()) << out;
2628 if (is_float) {
2629 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2630 } else {
2631 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2632 }
2633
2634 // And remove the temporary stack space we allocated.
2635 __ addl(ESP, Immediate(2 * elem_size));
2636}
2637
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002638
2639void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2640 DCHECK(instruction->IsDiv() || instruction->IsRem());
2641
2642 LocationSummary* locations = instruction->GetLocations();
2643 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002644 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002645
2646 Register out_register = locations->Out().AsRegister<Register>();
2647 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002648 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002649
2650 DCHECK(imm == 1 || imm == -1);
2651
2652 if (instruction->IsRem()) {
2653 __ xorl(out_register, out_register);
2654 } else {
2655 __ movl(out_register, input_register);
2656 if (imm == -1) {
2657 __ negl(out_register);
2658 }
2659 }
2660}
2661
2662
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002663void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002664 LocationSummary* locations = instruction->GetLocations();
2665
2666 Register out_register = locations->Out().AsRegister<Register>();
2667 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002668 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002669
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002670 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002671 Register num = locations->GetTemp(0).AsRegister<Register>();
2672
2673 __ leal(num, Address(input_register, std::abs(imm) - 1));
2674 __ testl(input_register, input_register);
2675 __ cmovl(kGreaterEqual, num, input_register);
2676 int shift = CTZ(imm);
2677 __ sarl(num, Immediate(shift));
2678
2679 if (imm < 0) {
2680 __ negl(num);
2681 }
2682
2683 __ movl(out_register, num);
2684}
2685
2686void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2687 DCHECK(instruction->IsDiv() || instruction->IsRem());
2688
2689 LocationSummary* locations = instruction->GetLocations();
2690 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2691
2692 Register eax = locations->InAt(0).AsRegister<Register>();
2693 Register out = locations->Out().AsRegister<Register>();
2694 Register num;
2695 Register edx;
2696
2697 if (instruction->IsDiv()) {
2698 edx = locations->GetTemp(0).AsRegister<Register>();
2699 num = locations->GetTemp(1).AsRegister<Register>();
2700 } else {
2701 edx = locations->Out().AsRegister<Register>();
2702 num = locations->GetTemp(0).AsRegister<Register>();
2703 }
2704
2705 DCHECK_EQ(EAX, eax);
2706 DCHECK_EQ(EDX, edx);
2707 if (instruction->IsDiv()) {
2708 DCHECK_EQ(EAX, out);
2709 } else {
2710 DCHECK_EQ(EDX, out);
2711 }
2712
2713 int64_t magic;
2714 int shift;
2715 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2716
2717 Label ndiv;
2718 Label end;
2719 // If numerator is 0, the result is 0, no computation needed.
2720 __ testl(eax, eax);
2721 __ j(kNotEqual, &ndiv);
2722
2723 __ xorl(out, out);
2724 __ jmp(&end);
2725
2726 __ Bind(&ndiv);
2727
2728 // Save the numerator.
2729 __ movl(num, eax);
2730
2731 // EAX = magic
2732 __ movl(eax, Immediate(magic));
2733
2734 // EDX:EAX = magic * numerator
2735 __ imull(num);
2736
2737 if (imm > 0 && magic < 0) {
2738 // EDX += num
2739 __ addl(edx, num);
2740 } else if (imm < 0 && magic > 0) {
2741 __ subl(edx, num);
2742 }
2743
2744 // Shift if needed.
2745 if (shift != 0) {
2746 __ sarl(edx, Immediate(shift));
2747 }
2748
2749 // EDX += 1 if EDX < 0
2750 __ movl(eax, edx);
2751 __ shrl(edx, Immediate(31));
2752 __ addl(edx, eax);
2753
2754 if (instruction->IsRem()) {
2755 __ movl(eax, num);
2756 __ imull(edx, Immediate(imm));
2757 __ subl(eax, edx);
2758 __ movl(edx, eax);
2759 } else {
2760 __ movl(eax, edx);
2761 }
2762 __ Bind(&end);
2763}
2764
Calin Juravlebacfec32014-11-14 15:54:36 +00002765void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2766 DCHECK(instruction->IsDiv() || instruction->IsRem());
2767
2768 LocationSummary* locations = instruction->GetLocations();
2769 Location out = locations->Out();
2770 Location first = locations->InAt(0);
2771 Location second = locations->InAt(1);
2772 bool is_div = instruction->IsDiv();
2773
2774 switch (instruction->GetResultType()) {
2775 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002776 DCHECK_EQ(EAX, first.AsRegister<Register>());
2777 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002778
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002779 if (instruction->InputAt(1)->IsIntConstant()) {
2780 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002781
2782 if (imm == 0) {
2783 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2784 } else if (imm == 1 || imm == -1) {
2785 DivRemOneOrMinusOne(instruction);
2786 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002787 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002788 } else {
2789 DCHECK(imm <= -2 || imm >= 2);
2790 GenerateDivRemWithAnyConstant(instruction);
2791 }
2792 } else {
2793 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002794 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002795 is_div);
2796 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002797
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002798 Register second_reg = second.AsRegister<Register>();
2799 // 0x80000000/-1 triggers an arithmetic exception!
2800 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2801 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002802
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002803 __ cmpl(second_reg, Immediate(-1));
2804 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002805
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002806 // edx:eax <- sign-extended of eax
2807 __ cdq();
2808 // eax = quotient, edx = remainder
2809 __ idivl(second_reg);
2810 __ Bind(slow_path->GetExitLabel());
2811 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002812 break;
2813 }
2814
2815 case Primitive::kPrimLong: {
2816 InvokeRuntimeCallingConvention calling_convention;
2817 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2818 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2819 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2820 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2821 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2822 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2823
2824 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002825 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2826 instruction,
2827 instruction->GetDexPc(),
2828 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002829 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002830 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2831 instruction,
2832 instruction->GetDexPc(),
2833 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002834 }
2835 uint32_t dex_pc = is_div
2836 ? instruction->AsDiv()->GetDexPc()
2837 : instruction->AsRem()->GetDexPc();
2838 codegen_->RecordPcInfo(instruction, dex_pc);
2839
2840 break;
2841 }
2842
2843 default:
2844 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2845 }
2846}
2847
Calin Juravle7c4954d2014-10-28 16:57:40 +00002848void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002849 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002850 ? LocationSummary::kCall
2851 : LocationSummary::kNoCall;
2852 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2853
Calin Juravle7c4954d2014-10-28 16:57:40 +00002854 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002855 case Primitive::kPrimInt: {
2856 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002857 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002858 locations->SetOut(Location::SameAsFirstInput());
2859 // Intel uses edx:eax as the dividend.
2860 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002861 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2862 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2863 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002864 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002865 locations->AddTemp(Location::RequiresRegister());
2866 }
Calin Juravled0d48522014-11-04 16:40:20 +00002867 break;
2868 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002869 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002870 InvokeRuntimeCallingConvention calling_convention;
2871 locations->SetInAt(0, Location::RegisterPairLocation(
2872 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2873 locations->SetInAt(1, Location::RegisterPairLocation(
2874 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2875 // Runtime helper puts the result in EAX, EDX.
2876 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002877 break;
2878 }
2879 case Primitive::kPrimFloat:
2880 case Primitive::kPrimDouble: {
2881 locations->SetInAt(0, Location::RequiresFpuRegister());
2882 locations->SetInAt(1, Location::RequiresFpuRegister());
2883 locations->SetOut(Location::SameAsFirstInput());
2884 break;
2885 }
2886
2887 default:
2888 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2889 }
2890}
2891
2892void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2893 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002894 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002895 Location first = locations->InAt(0);
2896 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002897
2898 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002899 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002900 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002901 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002902 break;
2903 }
2904
2905 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002906 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002907 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002908 break;
2909 }
2910
2911 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002912 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002913 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002914 break;
2915 }
2916
2917 default:
2918 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2919 }
2920}
2921
Calin Juravlebacfec32014-11-14 15:54:36 +00002922void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002923 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002924
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002925 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2926 ? LocationSummary::kCall
2927 : LocationSummary::kNoCall;
2928 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002929
Calin Juravled2ec87d2014-12-08 14:24:46 +00002930 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002931 case Primitive::kPrimInt: {
2932 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002933 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002934 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002935 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2936 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2937 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002938 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002939 locations->AddTemp(Location::RequiresRegister());
2940 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002941 break;
2942 }
2943 case Primitive::kPrimLong: {
2944 InvokeRuntimeCallingConvention calling_convention;
2945 locations->SetInAt(0, Location::RegisterPairLocation(
2946 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2947 locations->SetInAt(1, Location::RegisterPairLocation(
2948 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2949 // Runtime helper puts the result in EAX, EDX.
2950 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2951 break;
2952 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002953 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002954 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002955 locations->SetInAt(0, Location::Any());
2956 locations->SetInAt(1, Location::Any());
2957 locations->SetOut(Location::RequiresFpuRegister());
2958 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002959 break;
2960 }
2961
2962 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002963 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002964 }
2965}
2966
2967void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2968 Primitive::Type type = rem->GetResultType();
2969 switch (type) {
2970 case Primitive::kPrimInt:
2971 case Primitive::kPrimLong: {
2972 GenerateDivRemIntegral(rem);
2973 break;
2974 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002975 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002976 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002977 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002978 break;
2979 }
2980 default:
2981 LOG(FATAL) << "Unexpected rem type " << type;
2982 }
2983}
2984
Calin Juravled0d48522014-11-04 16:40:20 +00002985void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2986 LocationSummary* locations =
2987 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002988 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002989 case Primitive::kPrimByte:
2990 case Primitive::kPrimChar:
2991 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002992 case Primitive::kPrimInt: {
2993 locations->SetInAt(0, Location::Any());
2994 break;
2995 }
2996 case Primitive::kPrimLong: {
2997 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2998 if (!instruction->IsConstant()) {
2999 locations->AddTemp(Location::RequiresRegister());
3000 }
3001 break;
3002 }
3003 default:
3004 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3005 }
Calin Juravled0d48522014-11-04 16:40:20 +00003006 if (instruction->HasUses()) {
3007 locations->SetOut(Location::SameAsFirstInput());
3008 }
3009}
3010
3011void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3012 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
3013 codegen_->AddSlowPath(slow_path);
3014
3015 LocationSummary* locations = instruction->GetLocations();
3016 Location value = locations->InAt(0);
3017
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003018 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003019 case Primitive::kPrimByte:
3020 case Primitive::kPrimChar:
3021 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003022 case Primitive::kPrimInt: {
3023 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003024 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003025 __ j(kEqual, slow_path->GetEntryLabel());
3026 } else if (value.IsStackSlot()) {
3027 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3028 __ j(kEqual, slow_path->GetEntryLabel());
3029 } else {
3030 DCHECK(value.IsConstant()) << value;
3031 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3032 __ jmp(slow_path->GetEntryLabel());
3033 }
3034 }
3035 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003036 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003037 case Primitive::kPrimLong: {
3038 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003039 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003040 __ movl(temp, value.AsRegisterPairLow<Register>());
3041 __ orl(temp, value.AsRegisterPairHigh<Register>());
3042 __ j(kEqual, slow_path->GetEntryLabel());
3043 } else {
3044 DCHECK(value.IsConstant()) << value;
3045 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3046 __ jmp(slow_path->GetEntryLabel());
3047 }
3048 }
3049 break;
3050 }
3051 default:
3052 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003053 }
Calin Juravled0d48522014-11-04 16:40:20 +00003054}
3055
Calin Juravle9aec02f2014-11-18 23:06:35 +00003056void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3057 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3058
3059 LocationSummary* locations =
3060 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3061
3062 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003063 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003064 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003065 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003066 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003067 // The shift count needs to be in CL or a constant.
3068 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003069 locations->SetOut(Location::SameAsFirstInput());
3070 break;
3071 }
3072 default:
3073 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3074 }
3075}
3076
3077void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3078 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3079
3080 LocationSummary* locations = op->GetLocations();
3081 Location first = locations->InAt(0);
3082 Location second = locations->InAt(1);
3083 DCHECK(first.Equals(locations->Out()));
3084
3085 switch (op->GetResultType()) {
3086 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003087 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003088 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003089 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003090 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003091 DCHECK_EQ(ECX, second_reg);
3092 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003093 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003094 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003095 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003096 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003097 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003098 }
3099 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003100 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3101 if (shift == 0) {
3102 return;
3103 }
3104 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003105 if (op->IsShl()) {
3106 __ shll(first_reg, imm);
3107 } else if (op->IsShr()) {
3108 __ sarl(first_reg, imm);
3109 } else {
3110 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003111 }
3112 }
3113 break;
3114 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003115 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003116 if (second.IsRegister()) {
3117 Register second_reg = second.AsRegister<Register>();
3118 DCHECK_EQ(ECX, second_reg);
3119 if (op->IsShl()) {
3120 GenerateShlLong(first, second_reg);
3121 } else if (op->IsShr()) {
3122 GenerateShrLong(first, second_reg);
3123 } else {
3124 GenerateUShrLong(first, second_reg);
3125 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003126 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003127 // Shift by a constant.
3128 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3129 // Nothing to do if the shift is 0, as the input is already the output.
3130 if (shift != 0) {
3131 if (op->IsShl()) {
3132 GenerateShlLong(first, shift);
3133 } else if (op->IsShr()) {
3134 GenerateShrLong(first, shift);
3135 } else {
3136 GenerateUShrLong(first, shift);
3137 }
3138 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003139 }
3140 break;
3141 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003142 default:
3143 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3144 }
3145}
3146
Mark P Mendell73945692015-04-29 14:56:17 +00003147void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3148 Register low = loc.AsRegisterPairLow<Register>();
3149 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003150 if (shift == 1) {
3151 // This is just an addition.
3152 __ addl(low, low);
3153 __ adcl(high, high);
3154 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003155 // Shift by 32 is easy. High gets low, and low gets 0.
3156 codegen_->EmitParallelMoves(
3157 loc.ToLow(),
3158 loc.ToHigh(),
3159 Primitive::kPrimInt,
3160 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3161 loc.ToLow(),
3162 Primitive::kPrimInt);
3163 } else if (shift > 32) {
3164 // Low part becomes 0. High part is low part << (shift-32).
3165 __ movl(high, low);
3166 __ shll(high, Immediate(shift - 32));
3167 __ xorl(low, low);
3168 } else {
3169 // Between 1 and 31.
3170 __ shld(high, low, Immediate(shift));
3171 __ shll(low, Immediate(shift));
3172 }
3173}
3174
Calin Juravle9aec02f2014-11-18 23:06:35 +00003175void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
3176 Label done;
3177 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3178 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3179 __ testl(shifter, Immediate(32));
3180 __ j(kEqual, &done);
3181 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3182 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3183 __ Bind(&done);
3184}
3185
Mark P Mendell73945692015-04-29 14:56:17 +00003186void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3187 Register low = loc.AsRegisterPairLow<Register>();
3188 Register high = loc.AsRegisterPairHigh<Register>();
3189 if (shift == 32) {
3190 // Need to copy the sign.
3191 DCHECK_NE(low, high);
3192 __ movl(low, high);
3193 __ sarl(high, Immediate(31));
3194 } else if (shift > 32) {
3195 DCHECK_NE(low, high);
3196 // High part becomes sign. Low part is shifted by shift - 32.
3197 __ movl(low, high);
3198 __ sarl(high, Immediate(31));
3199 __ sarl(low, Immediate(shift - 32));
3200 } else {
3201 // Between 1 and 31.
3202 __ shrd(low, high, Immediate(shift));
3203 __ sarl(high, Immediate(shift));
3204 }
3205}
3206
Calin Juravle9aec02f2014-11-18 23:06:35 +00003207void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
3208 Label done;
3209 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3210 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3211 __ testl(shifter, Immediate(32));
3212 __ j(kEqual, &done);
3213 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3214 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3215 __ Bind(&done);
3216}
3217
Mark P Mendell73945692015-04-29 14:56:17 +00003218void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3219 Register low = loc.AsRegisterPairLow<Register>();
3220 Register high = loc.AsRegisterPairHigh<Register>();
3221 if (shift == 32) {
3222 // Shift by 32 is easy. Low gets high, and high gets 0.
3223 codegen_->EmitParallelMoves(
3224 loc.ToHigh(),
3225 loc.ToLow(),
3226 Primitive::kPrimInt,
3227 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3228 loc.ToHigh(),
3229 Primitive::kPrimInt);
3230 } else if (shift > 32) {
3231 // Low part is high >> (shift - 32). High part becomes 0.
3232 __ movl(low, high);
3233 __ shrl(low, Immediate(shift - 32));
3234 __ xorl(high, high);
3235 } else {
3236 // Between 1 and 31.
3237 __ shrd(low, high, Immediate(shift));
3238 __ shrl(high, Immediate(shift));
3239 }
3240}
3241
Calin Juravle9aec02f2014-11-18 23:06:35 +00003242void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
3243 Label done;
3244 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3245 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3246 __ testl(shifter, Immediate(32));
3247 __ j(kEqual, &done);
3248 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3249 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3250 __ Bind(&done);
3251}
3252
3253void LocationsBuilderX86::VisitShl(HShl* shl) {
3254 HandleShift(shl);
3255}
3256
3257void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3258 HandleShift(shl);
3259}
3260
3261void LocationsBuilderX86::VisitShr(HShr* shr) {
3262 HandleShift(shr);
3263}
3264
3265void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3266 HandleShift(shr);
3267}
3268
3269void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3270 HandleShift(ushr);
3271}
3272
3273void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3274 HandleShift(ushr);
3275}
3276
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003277void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003278 LocationSummary* locations =
3279 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003280 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003281 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003282 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003283 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003284}
3285
3286void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3287 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003288 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003289 // Note: if heap poisoning is enabled, the entry point takes cares
3290 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003291 codegen_->InvokeRuntime(
3292 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3293 instruction,
3294 instruction->GetDexPc(),
3295 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003296 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003297}
3298
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003299void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3300 LocationSummary* locations =
3301 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3302 locations->SetOut(Location::RegisterLocation(EAX));
3303 InvokeRuntimeCallingConvention calling_convention;
3304 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003305 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003306 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003307}
3308
3309void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3310 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003311 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3312
Roland Levillain4d027112015-07-01 15:41:14 +01003313 // Note: if heap poisoning is enabled, the entry point takes cares
3314 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003315 codegen_->InvokeRuntime(
3316 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3317 instruction,
3318 instruction->GetDexPc(),
3319 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003320 DCHECK(!codegen_->IsLeafMethod());
3321}
3322
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003323void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003324 LocationSummary* locations =
3325 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003326 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3327 if (location.IsStackSlot()) {
3328 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3329 } else if (location.IsDoubleStackSlot()) {
3330 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003331 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003332 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003333}
3334
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003335void InstructionCodeGeneratorX86::VisitParameterValue(
3336 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3337}
3338
3339void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3340 LocationSummary* locations =
3341 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3342 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3343}
3344
3345void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003346}
3347
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003348void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003349 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003350 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003351 locations->SetInAt(0, Location::RequiresRegister());
3352 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003353}
3354
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003355void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3356 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003357 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003358 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003359 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003360 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003361 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003362 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003363 break;
3364
3365 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003366 __ notl(out.AsRegisterPairLow<Register>());
3367 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003368 break;
3369
3370 default:
3371 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3372 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003373}
3374
David Brazdil66d126e2015-04-03 16:02:44 +01003375void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3376 LocationSummary* locations =
3377 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3378 locations->SetInAt(0, Location::RequiresRegister());
3379 locations->SetOut(Location::SameAsFirstInput());
3380}
3381
3382void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003383 LocationSummary* locations = bool_not->GetLocations();
3384 Location in = locations->InAt(0);
3385 Location out = locations->Out();
3386 DCHECK(in.Equals(out));
3387 __ xorl(out.AsRegister<Register>(), Immediate(1));
3388}
3389
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003390void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003391 LocationSummary* locations =
3392 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003393 switch (compare->InputAt(0)->GetType()) {
3394 case Primitive::kPrimLong: {
3395 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003396 locations->SetInAt(1, Location::Any());
3397 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3398 break;
3399 }
3400 case Primitive::kPrimFloat:
3401 case Primitive::kPrimDouble: {
3402 locations->SetInAt(0, Location::RequiresFpuRegister());
3403 locations->SetInAt(1, Location::RequiresFpuRegister());
3404 locations->SetOut(Location::RequiresRegister());
3405 break;
3406 }
3407 default:
3408 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3409 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003410}
3411
3412void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003413 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003414 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003415 Location left = locations->InAt(0);
3416 Location right = locations->InAt(1);
3417
3418 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003419 switch (compare->InputAt(0)->GetType()) {
3420 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003421 Register left_low = left.AsRegisterPairLow<Register>();
3422 Register left_high = left.AsRegisterPairHigh<Register>();
3423 int32_t val_low = 0;
3424 int32_t val_high = 0;
3425 bool right_is_const = false;
3426
3427 if (right.IsConstant()) {
3428 DCHECK(right.GetConstant()->IsLongConstant());
3429 right_is_const = true;
3430 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3431 val_low = Low32Bits(val);
3432 val_high = High32Bits(val);
3433 }
3434
Calin Juravleddb7df22014-11-25 20:56:51 +00003435 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003436 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003437 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003438 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003439 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003440 DCHECK(right_is_const) << right;
3441 if (val_high == 0) {
3442 __ testl(left_high, left_high);
3443 } else {
3444 __ cmpl(left_high, Immediate(val_high));
3445 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003446 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003447 __ j(kLess, &less); // Signed compare.
3448 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003449 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003450 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003451 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003452 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003453 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003454 DCHECK(right_is_const) << right;
3455 if (val_low == 0) {
3456 __ testl(left_low, left_low);
3457 } else {
3458 __ cmpl(left_low, Immediate(val_low));
3459 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003460 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003461 break;
3462 }
3463 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003464 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003465 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3466 break;
3467 }
3468 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003469 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003470 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003471 break;
3472 }
3473 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003474 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003475 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003476 __ movl(out, Immediate(0));
3477 __ j(kEqual, &done);
3478 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3479
3480 __ Bind(&greater);
3481 __ movl(out, Immediate(1));
3482 __ jmp(&done);
3483
3484 __ Bind(&less);
3485 __ movl(out, Immediate(-1));
3486
3487 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003488}
3489
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003490void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003491 LocationSummary* locations =
3492 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003493 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3494 locations->SetInAt(i, Location::Any());
3495 }
3496 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003497}
3498
3499void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003500 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003501 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003502}
3503
Calin Juravle52c48962014-12-16 17:02:57 +00003504void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3505 /*
3506 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3507 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3508 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3509 */
3510 switch (kind) {
3511 case MemBarrierKind::kAnyAny: {
3512 __ mfence();
3513 break;
3514 }
3515 case MemBarrierKind::kAnyStore:
3516 case MemBarrierKind::kLoadAny:
3517 case MemBarrierKind::kStoreStore: {
3518 // nop
3519 break;
3520 }
3521 default:
3522 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003523 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003524}
3525
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003526
Vladimir Marko9b688a02015-05-06 14:12:42 +01003527void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3528 switch (invoke->GetMethodLoadKind()) {
3529 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3530 // temp = thread->string_init_entrypoint
3531 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3532 break;
3533 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3534 // Nothing to do.
3535 break;
3536 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3537 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3538 break;
3539 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3540 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3541 method_patches_.emplace_back(invoke->GetTargetMethod());
3542 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3543 break;
3544 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3545 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3546 FALLTHROUGH_INTENDED;
3547 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3548 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3549 Register method_reg;
3550 Register reg = temp.AsRegister<Register>();
3551 if (current_method.IsRegister()) {
3552 method_reg = current_method.AsRegister<Register>();
3553 } else {
3554 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3555 DCHECK(!current_method.IsValid());
3556 method_reg = reg;
3557 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3558 }
3559 // temp = temp->dex_cache_resolved_methods_;
3560 __ movl(reg, Address(method_reg, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3561 // temp = temp[index_in_cache]
3562 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3563 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3564 break;
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003565 }
Vladimir Marko9b688a02015-05-06 14:12:42 +01003566 }
3567
3568 switch (invoke->GetCodePtrLocation()) {
3569 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3570 __ call(GetFrameEntryLabel());
3571 break;
3572 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3573 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3574 Label* label = &relative_call_patches_.back().label;
3575 __ call(label); // Bind to the patch label, override at link time.
3576 __ Bind(label); // Bind the label at the end of the "call" insn.
3577 break;
3578 }
3579 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3580 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3581 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3582 // (Though the direct CALL ptr16:32 is available for consideration).
3583 FALLTHROUGH_INTENDED;
3584 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3585 // (temp + offset_of_quick_compiled_code)()
3586 __ call(Address(temp.AsRegister<Register>(), ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3587 kX86WordSize).Int32Value()));
3588 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003589 }
3590
3591 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003592}
3593
Vladimir Marko9b688a02015-05-06 14:12:42 +01003594void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3595 DCHECK(linker_patches->empty());
3596 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3597 for (const MethodPatchInfo<Label>& info : method_patches_) {
3598 // The label points to the end of the "movl" insn but the literal offset for method
3599 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3600 uint32_t literal_offset = info.label.Position() - 4;
3601 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3602 info.target_method.dex_file,
3603 info.target_method.dex_method_index));
3604 }
3605 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3606 // The label points to the end of the "call" insn but the literal offset for method
3607 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3608 uint32_t literal_offset = info.label.Position() - 4;
3609 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3610 info.target_method.dex_file,
3611 info.target_method.dex_method_index));
3612 }
3613}
3614
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003615void CodeGeneratorX86::MarkGCCard(Register temp,
3616 Register card,
3617 Register object,
3618 Register value,
3619 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003620 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003621 if (value_can_be_null) {
3622 __ testl(value, value);
3623 __ j(kEqual, &is_null);
3624 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003625 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3626 __ movl(temp, object);
3627 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003628 __ movb(Address(temp, card, TIMES_1, 0),
3629 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003630 if (value_can_be_null) {
3631 __ Bind(&is_null);
3632 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003633}
3634
Calin Juravle52c48962014-12-16 17:02:57 +00003635void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3636 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003637 LocationSummary* locations =
3638 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003639 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003640
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003641 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3642 locations->SetOut(Location::RequiresFpuRegister());
3643 } else {
3644 // The output overlaps in case of long: we don't want the low move to overwrite
3645 // the object's location.
3646 locations->SetOut(Location::RequiresRegister(),
3647 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3648 : Location::kNoOutputOverlap);
3649 }
Calin Juravle52c48962014-12-16 17:02:57 +00003650
3651 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3652 // Long values can be loaded atomically into an XMM using movsd.
3653 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3654 // and then copy the XMM into the output 32bits at a time).
3655 locations->AddTemp(Location::RequiresFpuRegister());
3656 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003657}
3658
Calin Juravle52c48962014-12-16 17:02:57 +00003659void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3660 const FieldInfo& field_info) {
3661 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003662
Calin Juravle52c48962014-12-16 17:02:57 +00003663 LocationSummary* locations = instruction->GetLocations();
3664 Register base = locations->InAt(0).AsRegister<Register>();
3665 Location out = locations->Out();
3666 bool is_volatile = field_info.IsVolatile();
3667 Primitive::Type field_type = field_info.GetFieldType();
3668 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3669
3670 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003671 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003672 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003673 break;
3674 }
3675
3676 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003677 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003678 break;
3679 }
3680
3681 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003682 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003683 break;
3684 }
3685
3686 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003687 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003688 break;
3689 }
3690
3691 case Primitive::kPrimInt:
3692 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003693 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003694 break;
3695 }
3696
3697 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003698 if (is_volatile) {
3699 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3700 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003701 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003702 __ movd(out.AsRegisterPairLow<Register>(), temp);
3703 __ psrlq(temp, Immediate(32));
3704 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3705 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003706 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003707 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003708 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003709 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3710 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003711 break;
3712 }
3713
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003714 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003715 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003716 break;
3717 }
3718
3719 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003720 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003721 break;
3722 }
3723
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003724 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003725 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003726 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003727 }
Calin Juravle52c48962014-12-16 17:02:57 +00003728
Calin Juravle77520bc2015-01-12 18:45:46 +00003729 // Longs are handled in the switch.
3730 if (field_type != Primitive::kPrimLong) {
3731 codegen_->MaybeRecordImplicitNullCheck(instruction);
3732 }
3733
Calin Juravle52c48962014-12-16 17:02:57 +00003734 if (is_volatile) {
3735 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3736 }
Roland Levillain4d027112015-07-01 15:41:14 +01003737
3738 if (field_type == Primitive::kPrimNot) {
3739 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3740 }
Calin Juravle52c48962014-12-16 17:02:57 +00003741}
3742
3743void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3744 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3745
3746 LocationSummary* locations =
3747 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3748 locations->SetInAt(0, Location::RequiresRegister());
3749 bool is_volatile = field_info.IsVolatile();
3750 Primitive::Type field_type = field_info.GetFieldType();
3751 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3752 || (field_type == Primitive::kPrimByte);
3753
3754 // The register allocator does not support multiple
3755 // inputs that die at entry with one in a specific register.
3756 if (is_byte_type) {
3757 // Ensure the value is in a byte register.
3758 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003759 } else if (Primitive::IsFloatingPointType(field_type)) {
3760 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003761 } else {
3762 locations->SetInAt(1, Location::RequiresRegister());
3763 }
Calin Juravle52c48962014-12-16 17:02:57 +00003764 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003765 // Temporary registers for the write barrier.
3766 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003767 // Ensure the card is in a byte register.
3768 locations->AddTemp(Location::RegisterLocation(ECX));
3769 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3770 // 64bits value can be atomically written to an address with movsd and an XMM register.
3771 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3772 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3773 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3774 // isolated cases when we need this it isn't worth adding the extra complexity.
3775 locations->AddTemp(Location::RequiresFpuRegister());
3776 locations->AddTemp(Location::RequiresFpuRegister());
3777 }
3778}
3779
3780void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003781 const FieldInfo& field_info,
3782 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003783 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3784
3785 LocationSummary* locations = instruction->GetLocations();
3786 Register base = locations->InAt(0).AsRegister<Register>();
3787 Location value = locations->InAt(1);
3788 bool is_volatile = field_info.IsVolatile();
3789 Primitive::Type field_type = field_info.GetFieldType();
3790 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003791 bool needs_write_barrier =
3792 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003793
3794 if (is_volatile) {
3795 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3796 }
3797
3798 switch (field_type) {
3799 case Primitive::kPrimBoolean:
3800 case Primitive::kPrimByte: {
3801 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3802 break;
3803 }
3804
3805 case Primitive::kPrimShort:
3806 case Primitive::kPrimChar: {
3807 __ movw(Address(base, offset), value.AsRegister<Register>());
3808 break;
3809 }
3810
3811 case Primitive::kPrimInt:
3812 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003813 if (kPoisonHeapReferences && needs_write_barrier) {
3814 // Note that in the case where `value` is a null reference,
3815 // we do not enter this block, as the reference does not
3816 // need poisoning.
3817 DCHECK_EQ(field_type, Primitive::kPrimNot);
3818 Register temp = locations->GetTemp(0).AsRegister<Register>();
3819 __ movl(temp, value.AsRegister<Register>());
3820 __ PoisonHeapReference(temp);
3821 __ movl(Address(base, offset), temp);
3822 } else {
3823 __ movl(Address(base, offset), value.AsRegister<Register>());
3824 }
Calin Juravle52c48962014-12-16 17:02:57 +00003825 break;
3826 }
3827
3828 case Primitive::kPrimLong: {
3829 if (is_volatile) {
3830 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3831 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3832 __ movd(temp1, value.AsRegisterPairLow<Register>());
3833 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3834 __ punpckldq(temp1, temp2);
3835 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003836 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003837 } else {
3838 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003839 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003840 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3841 }
3842 break;
3843 }
3844
3845 case Primitive::kPrimFloat: {
3846 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3847 break;
3848 }
3849
3850 case Primitive::kPrimDouble: {
3851 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3852 break;
3853 }
3854
3855 case Primitive::kPrimVoid:
3856 LOG(FATAL) << "Unreachable type " << field_type;
3857 UNREACHABLE();
3858 }
3859
Calin Juravle77520bc2015-01-12 18:45:46 +00003860 // Longs are handled in the switch.
3861 if (field_type != Primitive::kPrimLong) {
3862 codegen_->MaybeRecordImplicitNullCheck(instruction);
3863 }
3864
Roland Levillain4d027112015-07-01 15:41:14 +01003865 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003866 Register temp = locations->GetTemp(0).AsRegister<Register>();
3867 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003868 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003869 }
3870
Calin Juravle52c48962014-12-16 17:02:57 +00003871 if (is_volatile) {
3872 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3873 }
3874}
3875
3876void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3877 HandleFieldGet(instruction, instruction->GetFieldInfo());
3878}
3879
3880void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3881 HandleFieldGet(instruction, instruction->GetFieldInfo());
3882}
3883
3884void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3885 HandleFieldSet(instruction, instruction->GetFieldInfo());
3886}
3887
3888void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003889 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003890}
3891
3892void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3893 HandleFieldSet(instruction, instruction->GetFieldInfo());
3894}
3895
3896void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003897 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003898}
3899
3900void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3901 HandleFieldGet(instruction, instruction->GetFieldInfo());
3902}
3903
3904void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3905 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003906}
3907
3908void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003909 LocationSummary* locations =
3910 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003911 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3912 ? Location::RequiresRegister()
3913 : Location::Any();
3914 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003915 if (instruction->HasUses()) {
3916 locations->SetOut(Location::SameAsFirstInput());
3917 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003918}
3919
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003920void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003921 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3922 return;
3923 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003924 LocationSummary* locations = instruction->GetLocations();
3925 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003926
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003927 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3928 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3929}
3930
3931void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003932 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003933 codegen_->AddSlowPath(slow_path);
3934
3935 LocationSummary* locations = instruction->GetLocations();
3936 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003937
3938 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003939 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003940 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003941 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003942 } else {
3943 DCHECK(obj.IsConstant()) << obj;
3944 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3945 __ jmp(slow_path->GetEntryLabel());
3946 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003947 }
3948 __ j(kEqual, slow_path->GetEntryLabel());
3949}
3950
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003951void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3952 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3953 GenerateImplicitNullCheck(instruction);
3954 } else {
3955 GenerateExplicitNullCheck(instruction);
3956 }
3957}
3958
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003959void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003960 LocationSummary* locations =
3961 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003962 locations->SetInAt(0, Location::RequiresRegister());
3963 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003964 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3965 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3966 } else {
3967 // The output overlaps in case of long: we don't want the low move to overwrite
3968 // the array's location.
3969 locations->SetOut(Location::RequiresRegister(),
3970 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3971 : Location::kNoOutputOverlap);
3972 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003973}
3974
3975void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3976 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003977 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003978 Location index = locations->InAt(1);
3979
Calin Juravle77520bc2015-01-12 18:45:46 +00003980 Primitive::Type type = instruction->GetType();
3981 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003982 case Primitive::kPrimBoolean: {
3983 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003984 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003985 if (index.IsConstant()) {
3986 __ movzxb(out, Address(obj,
3987 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3988 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003989 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003990 }
3991 break;
3992 }
3993
3994 case Primitive::kPrimByte: {
3995 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003996 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003997 if (index.IsConstant()) {
3998 __ movsxb(out, Address(obj,
3999 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4000 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004001 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004002 }
4003 break;
4004 }
4005
4006 case Primitive::kPrimShort: {
4007 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004008 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004009 if (index.IsConstant()) {
4010 __ movsxw(out, Address(obj,
4011 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4012 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004013 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004014 }
4015 break;
4016 }
4017
4018 case Primitive::kPrimChar: {
4019 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004020 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004021 if (index.IsConstant()) {
4022 __ movzxw(out, Address(obj,
4023 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4024 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004025 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004026 }
4027 break;
4028 }
4029
4030 case Primitive::kPrimInt:
4031 case Primitive::kPrimNot: {
4032 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004033 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004034 if (index.IsConstant()) {
4035 __ movl(out, Address(obj,
4036 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4037 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004038 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004039 }
4040 break;
4041 }
4042
4043 case Primitive::kPrimLong: {
4044 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004045 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004046 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004047 if (index.IsConstant()) {
4048 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004049 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004050 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004051 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004052 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004053 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004054 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004055 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004056 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004057 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004058 }
4059 break;
4060 }
4061
Mark Mendell7c8d0092015-01-26 11:21:33 -05004062 case Primitive::kPrimFloat: {
4063 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4064 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4065 if (index.IsConstant()) {
4066 __ movss(out, Address(obj,
4067 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4068 } else {
4069 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4070 }
4071 break;
4072 }
4073
4074 case Primitive::kPrimDouble: {
4075 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4076 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4077 if (index.IsConstant()) {
4078 __ movsd(out, Address(obj,
4079 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4080 } else {
4081 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4082 }
4083 break;
4084 }
4085
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004086 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004087 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004088 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004089 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004090
4091 if (type != Primitive::kPrimLong) {
4092 codegen_->MaybeRecordImplicitNullCheck(instruction);
4093 }
Roland Levillain4d027112015-07-01 15:41:14 +01004094
4095 if (type == Primitive::kPrimNot) {
4096 Register out = locations->Out().AsRegister<Register>();
4097 __ MaybeUnpoisonHeapReference(out);
4098 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004099}
4100
4101void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004102 // This location builder might end up asking to up to four registers, which is
4103 // not currently possible for baseline. The situation in which we need four
4104 // registers cannot be met by baseline though, because it has not run any
4105 // optimization.
4106
Nicolas Geoffray39468442014-09-02 15:17:15 +01004107 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004108 bool needs_write_barrier =
4109 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4110
Mark Mendell5f874182015-03-04 15:42:45 -05004111 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004112
Nicolas Geoffray39468442014-09-02 15:17:15 +01004113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4114 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004115 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004116
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004117 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004118 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004119 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4120 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4121 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004122 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004123 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4124 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004125 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004126 // In case of a byte operation, the register allocator does not support multiple
4127 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004128 locations->SetInAt(0, Location::RequiresRegister());
4129 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004130 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004131 // Ensure the value is in a byte register.
4132 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004133 } else if (Primitive::IsFloatingPointType(value_type)) {
4134 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004135 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004136 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004137 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004138 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004139 // Temporary registers for the write barrier.
4140 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004141 // Ensure the card is in a byte register.
4142 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004143 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004144 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004145}
4146
4147void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4148 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004149 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004150 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004151 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004152 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004153 bool needs_runtime_call = locations->WillCall();
4154 bool needs_write_barrier =
4155 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004156
4157 switch (value_type) {
4158 case Primitive::kPrimBoolean:
4159 case Primitive::kPrimByte: {
4160 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004161 if (index.IsConstant()) {
4162 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004163 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004164 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004165 } else {
4166 __ movb(Address(obj, offset),
4167 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4168 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004169 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004170 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004171 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004172 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004173 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004174 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004175 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4176 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004177 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004178 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004179 break;
4180 }
4181
4182 case Primitive::kPrimShort:
4183 case Primitive::kPrimChar: {
4184 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004185 if (index.IsConstant()) {
4186 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004187 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004188 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004189 } else {
4190 __ movw(Address(obj, offset),
4191 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4192 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004193 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004194 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004195 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4196 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004197 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004198 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004199 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4200 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004201 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004202 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004203 break;
4204 }
4205
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004206 case Primitive::kPrimInt:
4207 case Primitive::kPrimNot: {
4208 if (!needs_runtime_call) {
4209 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4210 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004211 size_t offset =
4212 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004213 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004214 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4215 Register temp = locations->GetTemp(0).AsRegister<Register>();
4216 __ movl(temp, value.AsRegister<Register>());
4217 __ PoisonHeapReference(temp);
4218 __ movl(Address(obj, offset), temp);
4219 } else {
4220 __ movl(Address(obj, offset), value.AsRegister<Register>());
4221 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004222 } else {
4223 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004224 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4225 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4226 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4227 // Note: if heap poisoning is enabled, no need to poison
4228 // (negate) `v` if it is a reference, as it would be null.
4229 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004230 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004231 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004232 DCHECK(index.IsRegister()) << index;
4233 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004234 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4235 Register temp = locations->GetTemp(0).AsRegister<Register>();
4236 __ movl(temp, value.AsRegister<Register>());
4237 __ PoisonHeapReference(temp);
4238 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4239 } else {
4240 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4241 value.AsRegister<Register>());
4242 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004243 } else {
4244 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004245 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4246 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4247 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4248 // Note: if heap poisoning is enabled, no need to poison
4249 // (negate) `v` if it is a reference, as it would be null.
4250 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004251 }
4252 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004253 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004254
4255 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004256 Register temp = locations->GetTemp(0).AsRegister<Register>();
4257 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004258 codegen_->MarkGCCard(
4259 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004260 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004261 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004262 DCHECK_EQ(value_type, Primitive::kPrimNot);
4263 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004264 // Note: if heap poisoning is enabled, pAputObject takes cares
4265 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004266 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4267 instruction,
4268 instruction->GetDexPc(),
4269 nullptr);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004270 }
4271 break;
4272 }
4273
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004274 case Primitive::kPrimLong: {
4275 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004276 if (index.IsConstant()) {
4277 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004278 if (value.IsRegisterPair()) {
4279 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004280 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004281 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004282 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004283 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004284 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4285 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004286 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004287 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4288 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004289 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004290 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004291 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004292 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004293 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004294 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004295 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004296 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004297 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004298 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004299 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004300 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004301 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004302 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004303 Immediate(High32Bits(val)));
4304 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004305 }
4306 break;
4307 }
4308
Mark Mendell7c8d0092015-01-26 11:21:33 -05004309 case Primitive::kPrimFloat: {
4310 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4311 DCHECK(value.IsFpuRegister());
4312 if (index.IsConstant()) {
4313 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4314 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4315 } else {
4316 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4317 value.AsFpuRegister<XmmRegister>());
4318 }
4319 break;
4320 }
4321
4322 case Primitive::kPrimDouble: {
4323 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4324 DCHECK(value.IsFpuRegister());
4325 if (index.IsConstant()) {
4326 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4327 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4328 } else {
4329 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4330 value.AsFpuRegister<XmmRegister>());
4331 }
4332 break;
4333 }
4334
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004335 case Primitive::kPrimVoid:
4336 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004337 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004338 }
4339}
4340
4341void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4342 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004343 locations->SetInAt(0, Location::RequiresRegister());
4344 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004345}
4346
4347void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4348 LocationSummary* locations = instruction->GetLocations();
4349 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004350 Register obj = locations->InAt(0).AsRegister<Register>();
4351 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004352 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004353 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004354}
4355
4356void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004357 LocationSummary* locations =
4358 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004359 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004360 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004361 if (instruction->HasUses()) {
4362 locations->SetOut(Location::SameAsFirstInput());
4363 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004364}
4365
4366void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4367 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004368 Location index_loc = locations->InAt(0);
4369 Location length_loc = locations->InAt(1);
4370 SlowPathCodeX86* slow_path =
4371 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004372
Mark Mendell99dbd682015-04-22 16:18:52 -04004373 if (length_loc.IsConstant()) {
4374 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4375 if (index_loc.IsConstant()) {
4376 // BCE will remove the bounds check if we are guarenteed to pass.
4377 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4378 if (index < 0 || index >= length) {
4379 codegen_->AddSlowPath(slow_path);
4380 __ jmp(slow_path->GetEntryLabel());
4381 } else {
4382 // Some optimization after BCE may have generated this, and we should not
4383 // generate a bounds check if it is a valid range.
4384 }
4385 return;
4386 }
4387
4388 // We have to reverse the jump condition because the length is the constant.
4389 Register index_reg = index_loc.AsRegister<Register>();
4390 __ cmpl(index_reg, Immediate(length));
4391 codegen_->AddSlowPath(slow_path);
4392 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004393 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004394 Register length = length_loc.AsRegister<Register>();
4395 if (index_loc.IsConstant()) {
4396 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4397 __ cmpl(length, Immediate(value));
4398 } else {
4399 __ cmpl(length, index_loc.AsRegister<Register>());
4400 }
4401 codegen_->AddSlowPath(slow_path);
4402 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004403 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004404}
4405
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004406void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4407 temp->SetLocations(nullptr);
4408}
4409
4410void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4411 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004412 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004413}
4414
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004415void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004416 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004417 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004418}
4419
4420void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004421 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4422}
4423
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004424void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4425 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4426}
4427
4428void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004429 HBasicBlock* block = instruction->GetBlock();
4430 if (block->GetLoopInformation() != nullptr) {
4431 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4432 // The back edge will generate the suspend check.
4433 return;
4434 }
4435 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4436 // The goto will generate the suspend check.
4437 return;
4438 }
4439 GenerateSuspendCheck(instruction, nullptr);
4440}
4441
4442void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4443 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004444 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004445 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4446 if (slow_path == nullptr) {
4447 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4448 instruction->SetSlowPath(slow_path);
4449 codegen_->AddSlowPath(slow_path);
4450 if (successor != nullptr) {
4451 DCHECK(successor->IsLoopHeader());
4452 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4453 }
4454 } else {
4455 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4456 }
4457
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004458 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004459 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004460 if (successor == nullptr) {
4461 __ j(kNotEqual, slow_path->GetEntryLabel());
4462 __ Bind(slow_path->GetReturnLabel());
4463 } else {
4464 __ j(kEqual, codegen_->GetLabelOf(successor));
4465 __ jmp(slow_path->GetEntryLabel());
4466 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004467}
4468
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004469X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4470 return codegen_->GetAssembler();
4471}
4472
Mark Mendell7c8d0092015-01-26 11:21:33 -05004473void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004474 ScratchRegisterScope ensure_scratch(
4475 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4476 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4477 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4478 __ movl(temp_reg, Address(ESP, src + stack_offset));
4479 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004480}
4481
4482void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004483 ScratchRegisterScope ensure_scratch(
4484 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4485 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4486 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4487 __ movl(temp_reg, Address(ESP, src + stack_offset));
4488 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4489 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4490 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004491}
4492
4493void ParallelMoveResolverX86::EmitMove(size_t index) {
4494 MoveOperands* move = moves_.Get(index);
4495 Location source = move->GetSource();
4496 Location destination = move->GetDestination();
4497
4498 if (source.IsRegister()) {
4499 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004500 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004501 } else {
4502 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004503 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004504 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004505 } else if (source.IsFpuRegister()) {
4506 if (destination.IsFpuRegister()) {
4507 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4508 } else if (destination.IsStackSlot()) {
4509 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4510 } else {
4511 DCHECK(destination.IsDoubleStackSlot());
4512 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4513 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004514 } else if (source.IsStackSlot()) {
4515 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004516 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004517 } else if (destination.IsFpuRegister()) {
4518 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004519 } else {
4520 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004521 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4522 }
4523 } else if (source.IsDoubleStackSlot()) {
4524 if (destination.IsFpuRegister()) {
4525 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4526 } else {
4527 DCHECK(destination.IsDoubleStackSlot()) << destination;
4528 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004529 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004530 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004531 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004532 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004533 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004534 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004535 if (value == 0) {
4536 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4537 } else {
4538 __ movl(destination.AsRegister<Register>(), Immediate(value));
4539 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004540 } else {
4541 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004542 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004543 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004544 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004545 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004546 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004547 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004548 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004549 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4550 if (value == 0) {
4551 // Easy handling of 0.0.
4552 __ xorps(dest, dest);
4553 } else {
4554 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004555 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4556 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4557 __ movl(temp, Immediate(value));
4558 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004559 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004560 } else {
4561 DCHECK(destination.IsStackSlot()) << destination;
4562 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4563 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004564 } else if (constant->IsLongConstant()) {
4565 int64_t value = constant->AsLongConstant()->GetValue();
4566 int32_t low_value = Low32Bits(value);
4567 int32_t high_value = High32Bits(value);
4568 Immediate low(low_value);
4569 Immediate high(high_value);
4570 if (destination.IsDoubleStackSlot()) {
4571 __ movl(Address(ESP, destination.GetStackIndex()), low);
4572 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4573 } else {
4574 __ movl(destination.AsRegisterPairLow<Register>(), low);
4575 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4576 }
4577 } else {
4578 DCHECK(constant->IsDoubleConstant());
4579 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004580 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004581 int32_t low_value = Low32Bits(value);
4582 int32_t high_value = High32Bits(value);
4583 Immediate low(low_value);
4584 Immediate high(high_value);
4585 if (destination.IsFpuRegister()) {
4586 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4587 if (value == 0) {
4588 // Easy handling of 0.0.
4589 __ xorpd(dest, dest);
4590 } else {
4591 __ pushl(high);
4592 __ pushl(low);
4593 __ movsd(dest, Address(ESP, 0));
4594 __ addl(ESP, Immediate(8));
4595 }
4596 } else {
4597 DCHECK(destination.IsDoubleStackSlot()) << destination;
4598 __ movl(Address(ESP, destination.GetStackIndex()), low);
4599 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4600 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004601 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004602 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004603 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004604 }
4605}
4606
Mark Mendella5c19ce2015-04-01 12:51:05 -04004607void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004608 Register suggested_scratch = reg == EAX ? EBX : EAX;
4609 ScratchRegisterScope ensure_scratch(
4610 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4611
4612 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4613 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4614 __ movl(Address(ESP, mem + stack_offset), reg);
4615 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004616}
4617
Mark Mendell7c8d0092015-01-26 11:21:33 -05004618void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004619 ScratchRegisterScope ensure_scratch(
4620 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4621
4622 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4623 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4624 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4625 __ movss(Address(ESP, mem + stack_offset), reg);
4626 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004627}
4628
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004629void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004630 ScratchRegisterScope ensure_scratch1(
4631 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004632
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004633 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4634 ScratchRegisterScope ensure_scratch2(
4635 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004636
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004637 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4638 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4639 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4640 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4641 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4642 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004643}
4644
4645void ParallelMoveResolverX86::EmitSwap(size_t index) {
4646 MoveOperands* move = moves_.Get(index);
4647 Location source = move->GetSource();
4648 Location destination = move->GetDestination();
4649
4650 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004651 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4652 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4653 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4654 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4655 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004656 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004657 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004658 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004659 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004660 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4661 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004662 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4663 // Use XOR Swap algorithm to avoid a temporary.
4664 DCHECK_NE(source.reg(), destination.reg());
4665 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4666 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4667 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4668 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4669 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4670 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4671 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004672 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4673 // Take advantage of the 16 bytes in the XMM register.
4674 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4675 Address stack(ESP, destination.GetStackIndex());
4676 // Load the double into the high doubleword.
4677 __ movhpd(reg, stack);
4678
4679 // Store the low double into the destination.
4680 __ movsd(stack, reg);
4681
4682 // Move the high double to the low double.
4683 __ psrldq(reg, Immediate(8));
4684 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4685 // Take advantage of the 16 bytes in the XMM register.
4686 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4687 Address stack(ESP, source.GetStackIndex());
4688 // Load the double into the high doubleword.
4689 __ movhpd(reg, stack);
4690
4691 // Store the low double into the destination.
4692 __ movsd(stack, reg);
4693
4694 // Move the high double to the low double.
4695 __ psrldq(reg, Immediate(8));
4696 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4697 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4698 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004699 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004700 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004701 }
4702}
4703
4704void ParallelMoveResolverX86::SpillScratch(int reg) {
4705 __ pushl(static_cast<Register>(reg));
4706}
4707
4708void ParallelMoveResolverX86::RestoreScratch(int reg) {
4709 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004710}
4711
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004712void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004713 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4714 ? LocationSummary::kCallOnSlowPath
4715 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004716 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004717 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004718 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004719 locations->SetOut(Location::RequiresRegister());
4720}
4721
4722void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004723 LocationSummary* locations = cls->GetLocations();
4724 Register out = locations->Out().AsRegister<Register>();
4725 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004726 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004727 DCHECK(!cls->CanCallRuntime());
4728 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004729 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004730 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004731 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004732 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004733 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004734 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004735 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004736
4737 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4738 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4739 codegen_->AddSlowPath(slow_path);
4740 __ testl(out, out);
4741 __ j(kEqual, slow_path->GetEntryLabel());
4742 if (cls->MustGenerateClinitCheck()) {
4743 GenerateClassInitializationCheck(slow_path, out);
4744 } else {
4745 __ Bind(slow_path->GetExitLabel());
4746 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004747 }
4748}
4749
4750void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4751 LocationSummary* locations =
4752 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4753 locations->SetInAt(0, Location::RequiresRegister());
4754 if (check->HasUses()) {
4755 locations->SetOut(Location::SameAsFirstInput());
4756 }
4757}
4758
4759void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004760 // We assume the class to not be null.
4761 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4762 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004763 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004764 GenerateClassInitializationCheck(slow_path,
4765 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004766}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004767
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004768void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4769 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004770 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4771 Immediate(mirror::Class::kStatusInitialized));
4772 __ j(kLess, slow_path->GetEntryLabel());
4773 __ Bind(slow_path->GetExitLabel());
4774 // No need for memory fence, thanks to the X86 memory model.
4775}
4776
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004777void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4778 LocationSummary* locations =
4779 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004780 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004781 locations->SetOut(Location::RequiresRegister());
4782}
4783
4784void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4785 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4786 codegen_->AddSlowPath(slow_path);
4787
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004788 LocationSummary* locations = load->GetLocations();
4789 Register out = locations->Out().AsRegister<Register>();
4790 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004791 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004792 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004793 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004794 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004795 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004796 __ testl(out, out);
4797 __ j(kEqual, slow_path->GetEntryLabel());
4798 __ Bind(slow_path->GetExitLabel());
4799}
4800
David Brazdilcb1c0552015-08-04 16:22:25 +01004801static Address GetExceptionTlsAddress() {
4802 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
4803}
4804
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004805void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4806 LocationSummary* locations =
4807 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4808 locations->SetOut(Location::RequiresRegister());
4809}
4810
4811void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004812 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
4813}
4814
4815void LocationsBuilderX86::VisitClearException(HClearException* clear) {
4816 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4817}
4818
4819void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4820 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004821}
4822
4823void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4824 LocationSummary* locations =
4825 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4826 InvokeRuntimeCallingConvention calling_convention;
4827 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4828}
4829
4830void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004831 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4832 instruction,
4833 instruction->GetDexPc(),
4834 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004835}
4836
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004837void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004838 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4839 ? LocationSummary::kNoCall
4840 : LocationSummary::kCallOnSlowPath;
4841 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4842 locations->SetInAt(0, Location::RequiresRegister());
4843 locations->SetInAt(1, Location::Any());
4844 locations->SetOut(Location::RequiresRegister());
4845}
4846
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004847void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004848 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004849 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004850 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004851 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004852 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4853 Label done, zero;
4854 SlowPathCodeX86* slow_path = nullptr;
4855
4856 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004857 // Avoid null check if we know obj is not null.
4858 if (instruction->MustDoNullCheck()) {
4859 __ testl(obj, obj);
4860 __ j(kEqual, &zero);
4861 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004862 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004863 __ movl(out, Address(obj, class_offset));
4864 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004865 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004866 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004867 } else {
4868 DCHECK(cls.IsStackSlot()) << cls;
4869 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4870 }
4871
4872 if (instruction->IsClassFinal()) {
4873 // Classes must be equal for the instanceof to succeed.
4874 __ j(kNotEqual, &zero);
4875 __ movl(out, Immediate(1));
4876 __ jmp(&done);
4877 } else {
4878 // If the classes are not equal, we go into a slow path.
4879 DCHECK(locations->OnlyCallsOnSlowPath());
4880 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004881 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004882 codegen_->AddSlowPath(slow_path);
4883 __ j(kNotEqual, slow_path->GetEntryLabel());
4884 __ movl(out, Immediate(1));
4885 __ jmp(&done);
4886 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004887
4888 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4889 __ Bind(&zero);
4890 __ movl(out, Immediate(0));
4891 }
4892
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004893 if (slow_path != nullptr) {
4894 __ Bind(slow_path->GetExitLabel());
4895 }
4896 __ Bind(&done);
4897}
4898
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004899void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4900 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4901 instruction, LocationSummary::kCallOnSlowPath);
4902 locations->SetInAt(0, Location::RequiresRegister());
4903 locations->SetInAt(1, Location::Any());
4904 locations->AddTemp(Location::RequiresRegister());
4905}
4906
4907void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4908 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004909 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004910 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004911 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004912 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4913 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4914 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4915 codegen_->AddSlowPath(slow_path);
4916
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004917 // Avoid null check if we know obj is not null.
4918 if (instruction->MustDoNullCheck()) {
4919 __ testl(obj, obj);
4920 __ j(kEqual, slow_path->GetExitLabel());
4921 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004922 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004923 __ movl(temp, Address(obj, class_offset));
4924 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004925 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004926 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004927 } else {
4928 DCHECK(cls.IsStackSlot()) << cls;
4929 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4930 }
Roland Levillain4d027112015-07-01 15:41:14 +01004931 // The checkcast succeeds if the classes are equal (fast path).
4932 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004933 __ j(kNotEqual, slow_path->GetEntryLabel());
4934 __ Bind(slow_path->GetExitLabel());
4935}
4936
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004937void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4938 LocationSummary* locations =
4939 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4940 InvokeRuntimeCallingConvention calling_convention;
4941 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4942}
4943
4944void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004945 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4946 : QUICK_ENTRY_POINT(pUnlockObject),
4947 instruction,
4948 instruction->GetDexPc(),
4949 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004950}
4951
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004952void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4953void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4954void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4955
4956void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4957 LocationSummary* locations =
4958 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4959 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4960 || instruction->GetResultType() == Primitive::kPrimLong);
4961 locations->SetInAt(0, Location::RequiresRegister());
4962 locations->SetInAt(1, Location::Any());
4963 locations->SetOut(Location::SameAsFirstInput());
4964}
4965
4966void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4967 HandleBitwiseOperation(instruction);
4968}
4969
4970void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4971 HandleBitwiseOperation(instruction);
4972}
4973
4974void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4975 HandleBitwiseOperation(instruction);
4976}
4977
4978void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4979 LocationSummary* locations = instruction->GetLocations();
4980 Location first = locations->InAt(0);
4981 Location second = locations->InAt(1);
4982 DCHECK(first.Equals(locations->Out()));
4983
4984 if (instruction->GetResultType() == Primitive::kPrimInt) {
4985 if (second.IsRegister()) {
4986 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004987 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004988 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004989 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004990 } else {
4991 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004992 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004993 }
4994 } else if (second.IsConstant()) {
4995 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004996 __ andl(first.AsRegister<Register>(),
4997 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004998 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004999 __ orl(first.AsRegister<Register>(),
5000 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005001 } else {
5002 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00005003 __ xorl(first.AsRegister<Register>(),
5004 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005005 }
5006 } else {
5007 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005008 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005009 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005010 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005011 } else {
5012 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005013 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005014 }
5015 }
5016 } else {
5017 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5018 if (second.IsRegisterPair()) {
5019 if (instruction->IsAnd()) {
5020 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5021 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5022 } else if (instruction->IsOr()) {
5023 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5024 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5025 } else {
5026 DCHECK(instruction->IsXor());
5027 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5028 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5029 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005030 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005031 if (instruction->IsAnd()) {
5032 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5033 __ andl(first.AsRegisterPairHigh<Register>(),
5034 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5035 } else if (instruction->IsOr()) {
5036 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5037 __ orl(first.AsRegisterPairHigh<Register>(),
5038 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5039 } else {
5040 DCHECK(instruction->IsXor());
5041 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5042 __ xorl(first.AsRegisterPairHigh<Register>(),
5043 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5044 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005045 } else {
5046 DCHECK(second.IsConstant()) << second;
5047 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005048 int32_t low_value = Low32Bits(value);
5049 int32_t high_value = High32Bits(value);
5050 Immediate low(low_value);
5051 Immediate high(high_value);
5052 Register first_low = first.AsRegisterPairLow<Register>();
5053 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005054 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005055 if (low_value == 0) {
5056 __ xorl(first_low, first_low);
5057 } else if (low_value != -1) {
5058 __ andl(first_low, low);
5059 }
5060 if (high_value == 0) {
5061 __ xorl(first_high, first_high);
5062 } else if (high_value != -1) {
5063 __ andl(first_high, high);
5064 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005065 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005066 if (low_value != 0) {
5067 __ orl(first_low, low);
5068 }
5069 if (high_value != 0) {
5070 __ orl(first_high, high);
5071 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005072 } else {
5073 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005074 if (low_value != 0) {
5075 __ xorl(first_low, low);
5076 }
5077 if (high_value != 0) {
5078 __ xorl(first_high, high);
5079 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005080 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005081 }
5082 }
5083}
5084
Calin Juravleb1498f62015-02-16 13:13:29 +00005085void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5086 // Nothing to do, this should be removed during prepare for register allocator.
5087 UNUSED(instruction);
5088 LOG(FATAL) << "Unreachable";
5089}
5090
5091void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5092 // Nothing to do, this should be removed during prepare for register allocator.
5093 UNUSED(instruction);
5094 LOG(FATAL) << "Unreachable";
5095}
5096
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005097void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5098 DCHECK(codegen_->IsBaseline());
5099 LocationSummary* locations =
5100 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5101 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5102}
5103
5104void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5105 DCHECK(codegen_->IsBaseline());
5106 // Will be generated at use site.
5107}
5108
Roland Levillain4d027112015-07-01 15:41:14 +01005109#undef __
5110
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005111} // namespace x86
5112} // namespace art